Description Format Usage Arguments Details Examples
LossCustom
creates a custom loss by using
Rcpp::Function
to set R
functions.
S4
object.
1 | 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.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.