chainer.functions.huber_loss¶
-
chainer.functions.huber_loss(x, t, delta, reduce='sum_along_second_axis')[source]¶ Computes the Huber loss.
The Huber loss is similar to the
mean_squared_error()but is less sensitive to outliers in the data. It is defined as\[\begin{split}L_{\delta}(a) = \left \{ \begin{array}{cc} \frac{1}{2} a^2 & {\rm if~|a| \leq \delta} \\ \delta (|a| - \frac{1}{2} \delta) & {\rm otherwise,} \end{array} \right.\end{split}\]where \(a = x - t\) is the difference between the input \(x\) and the target \(t\).
The loss is a variable whose value depends on the value of the option
reduce. If it is'no', it holds the elementwise loss values. If it is'sum_along_second_axis', loss values are summed up along the second axis (i.e.axis=1).See: Huber loss - Wikipedia.
- Parameters
x (
Variableor N-dimensional array) – Input variable. The shape ofxshould be (\(N\), \(K\), …) ifreduce='sum_along_second_axis'.t (
Variableor N-dimensional array) – Target variable for regression. The shape oftshould be (\(N\), \(K\), …) ifreduce='sum_along_second_axis'.delta (float) – Constant variable for Huber loss function as used in definition.
reduce (str) – Reduction option. Its value must be either
'sum_along_second_axis'or'no'. Otherwise,ValueErroris raised.
- Returns
A variable object holding a scalar array of the Huber loss \(L_{\delta}\). If
reduceis'no', the output variable holds array whose shape is same as one of (hence both of) input variables. If it is'sum_along_second_axis', the shape of the array is same as the input variables, except the second axis is removed.- Return type
Example
Example without reduction, in which case the output
ywill have the same shape as the inputsxandt.>>> import numpy as np >>> from chainer import functions as F >>> x = np.array([[-2.0, 3.0, 0.5], [5.0, 2.0, -0.5]]).astype(np.float32) >>> x.shape (2, 3) >>> t = np.array([[-2.0, 3.0, 0.0], [10.0, 2.0, -0.5]]).astype(np.float32) >>> t.shape (2, 3) >>> y = F.huber_loss(x, t, delta=1.0, reduce='no') >>> y.shape (2, 3) >>> y variable([[0. , 0. , 0.125], [4.5 , 0. , 0. ]])
Example with reduction along the second axis.
>>> y = F.huber_loss(x, t, delta=1.0, reduce='sum_along_second_axis') >>> y.shape (2,) >>> y variable([0.125, 4.5 ])