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

Format

S4 object.

Usage

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.

Examples

# 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)