LossCustom: Create LossCustom by using R functions.

Description Format Usage Arguments Details Examples

Description

LossCustom creates a custom loss by using Rcpp::Function to set R functions.

Format

S4 object.

Usage

1
LossCustom$new(lossFun, gradientFun, initFun)

Arguments

lossFun [function]

R function to calculate the loss. For details see the Details.

gradientFun [function]

R function to calculate the gradient. For details see the Details.

initFun [function]

R function to calculate the constant initialization. For details see the Details.

Details

The functions must have the following structure:

lossFun(truth, prediction) { ... return (loss) } With a vector argument truth containing the real values and a vector of predictions prediction. The function must return a vector containing the loss for each component.

gradientFun(truth, prediction) { ... return (grad) } With a vector argument truth containing the real values and a vector of predictions prediction. The function must return a vector containing the gradient of the loss for each component.

initFun(truth) { ... return (init) } With a vector argument truth containing the real values. The function must return a numeric value containing the offset for the constant initialization.

For an example see the Examples.

This class is a wrapper around the pure C++ implementation. To see the functionality of the C++ class visit https://schalkdaniel.github.io/compboost/cpp_man/html/classloss_1_1_custom_loss.html.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Loss function:
myLoss = function (true.values, prediction) {
  return (0.5 * (true.values - prediction)^2)
}
# Gradient of loss function:
myGradient = function (true.values, prediction) {
  return (prediction - true.values)
}
# Constant initialization:
myConstInit = function (true.values) {
  return (mean(true.values))
}

# Create new custom quadratic loss:
my.loss = LossCustom$new(myLoss, myGradient, myConstInit)

compboost documentation built on May 2, 2019, 6:40 a.m.