fn

huber_loss

Tensor
huber_loss(x: Tensor, target: Tensor, delta: float = 1.0, reduction: str = 'mean')
source

Huber loss — robust regression with a tunable transition point.

Identical in shape to smooth_l1_loss but parameterised by the slope-clip point δ\delta rather than the quadratic-region scale β\beta. Inside the xy<δ|x-y| < \delta region the loss is quadratic; outside, the gradient saturates at ±δ\pm\delta so a single huge residual cannot dominate the update.

Originally proposed by Peter Huber (1964) as the maximum- likelihood estimator for a contaminated Gaussian model — i.e., "mostly Gaussian noise but with occasional outliers". Use it when you suspect a small fraction of your residuals come from a heavy-tailed distribution.

Parameters

xTensor
Predicted values.
targetTensor
Target values; broadcast-compatible with x.
deltafloat= 1.0
Threshold at which the loss transitions from quadratic to linear (default 1.0).
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or full-shape, per reduction.

Notes

Per-element loss:

Li={12(xiyi)2xiyiδδ(xiyi12δ)otherwiseL_i = \begin{cases} \tfrac{1}{2}(x_i - y_i)^2 & |x_i - y_i| \le \delta \\ \delta\,\big(|x_i - y_i| - \tfrac{1}{2}\delta\big) & \text{otherwise} \end{cases}

Unlike smooth_l1_loss, the quadratic region is not rescaled by 1/δ1/\delta, so the loss magnitude itself grows with δ\delta.

Examples

>>> import lucid
>>> from lucid.nn.functional import huber_loss
>>> pred = lucid.tensor([0.0, 5.0])
>>> target = lucid.tensor([0.5, 0.0])
>>> huber_loss(pred, target, delta=1.0)
Tensor(2.3125)