HuberLoss

class HuberLoss(rho=1, weight=None, batch_axis=0, **kwargs)

Calculates smoothed L1 loss that is equal to L1 loss if absolute error exceeds rho but is equal to L2 loss otherwise. Also called SmoothedL1 loss.

(43)\[\begin{split}L = \sum_i \begin{cases} \frac{1}{2 {rho}} ({label}_i - {pred}_i)^2 & \text{ if } |{label}_i - {pred}_i| < {rho} \\ |{label}_i - {pred}_i| - \frac{{rho}}{2} & \text{ otherwise } \end{cases}\end{split}\]

label and pred can have arbitrary shape as long as they have the same number of elements.

Parameters:
rho : float, default 1

Threshold for trimmed mean estimator.

weight : float or None

Global scalar weight for loss.

batch_axis : int, default 0

The axis that represents mini-batch.

Inputs:
  • pred: prediction tensor with arbitrary shape
  • label: target tensor with the same size as pred.
  • sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.