/
LossCustom.Rd
76 lines (64 loc) · 2.04 KB
/
LossCustom.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{LossCustom}
\alias{LossCustom}
\title{Create LossCustom by using R functions.}
\format{
\code{\link{S4}} object.
}
\description{
\code{LossCustom} creates a custom loss by using
\code{Rcpp::Function} to set \code{R} functions.
}
\section{Usage}{
\preformatted{
LossCustom$new(lossFun, gradientFun, initFun)
}
}
\section{Arguments}{
\describe{
\item{\code{lossFun} [\code{function}]}{
\code{R} function to calculate the loss. For details see the
\code{Details}.
}
\item{\code{gradientFun} [\code{function}]}{
\code{R} function to calculate the gradient. For details see the
\code{Details}.
}
\item{\code{initFun} [\code{function}]}{
\code{R} function to calculate the constant initialization. For
details see the \code{Details}.
}
}
}
\section{Details}{
The functions must have the following structure:
\code{lossFun(truth, prediction) { ... return (loss) }} With a vector
argument \code{truth} containing the real values and a vector of
predictions \code{prediction}. The function must return a vector
containing the loss for each component.
\code{gradientFun(truth, prediction) { ... return (grad) }} With a vector
argument \code{truth} containing the real values and a vector of
predictions \code{prediction}. The function must return a vector
containing the gradient of the loss for each component.
\code{initFun(truth) { ... return (init) }} With a vector
argument \code{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)
}