Skip to contents

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

Format

S4 object.

Arguments

lossFun

(function)
R function to calculate the loss.

gradientFun

(function)
R function to calculate the gradient.

initFun

(function)
R function to calculate the constant initialization.

Usage


LossCustom$new(lossFun, gradientFun, initFun)

Inherited methods from Loss

  • $loss(): matrix(), matrix() -> matrix()

  • $gradient(): matrix(), matrix() -> matrix()

  • $constInit(): matrix() -> matrix()

  • $calculatePseudoResiduals(): matrix(), matrix() -> matrix()

  • $getLossType(): () -> character(1)

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)