LossCustom {compboost} | R Documentation |
LossCustom
creates a custom loss by using
Rcpp::Function
to set R
functions.
S4
object.
LossCustom$new(lossFun, gradientFun, initFun)
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
.
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.
# 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)