chainer.functions.huber_loss

chainer.functions.huber_loss(x, t, delta, reduce='sum_along_second_axis')[source]

Loss function which is less sensitive to outliers in data than MSE.

\[a = x - t\]

and

\[\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}\]

The output 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).

Parameters:
  • x (Variable or numpy.ndarray or cupy.ndarray) – Input variable. The shape of x should be (\(N\), \(K\)).
  • t (Variable or numpy.ndarray or cupy.ndarray) – Target variable for regression. The shape of t should be (\(N\), \(K\)).
  • 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, ValueError is raised.
Returns:

A variable object holding a scalar array of the huber loss \(L_{\delta}\). If reduce is '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:

Variable

See:
Huber loss - Wikipedia.

Example

>>> x = np.array([[-2.0, 3.0, 0.5], [5.0, 2.0, -0.5]]).astype('f')
>>> x
array([[-2. ,  3. ,  0.5],
       [ 5. ,  2. , -0.5]], dtype=float32)
>>> t = np.array([[-2.0, 3.0, 0.0], [10.0, 2.0, -0.5]]).astype('f')
>>> t
array([[-2. ,  3. ,  0. ],
       [10. ,  2. , -0.5]], dtype=float32)
>>> y = F.huber_loss(x, t, 1.0)
>>> y.shape
(2,)
>>> y.data
array([0.125, 4.5  ], dtype=float32)
>>> y = F.huber_loss(x, t, 1.0, reduce='no')
>>> y.shape
(2, 3)
>>> y.data
array([[0.   , 0.   , 0.125],
       [4.5  , 0.   , 0.   ]], dtype=float32)