Skip to content
fn

huber_loss

→Tensor
huber_loss(input: Tensor, target: Tensor, reduction: Reduction = 'mean', delta: float = 1.0, weight: Tensor | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

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 ∣x−y∣<δ|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

inputTensor
Predicted values.
targetTensor
Target values that broadcast with input. A target of another shape is broadcast with a UserWarning, as the reference does; one that does not broadcast raises ValueError.
reductionstr= 'mean'
"mean" (default), "sum", or "none".
deltafloat= 1.0
Threshold at which the loss transitions from quadratic to linear (default 1.0).
weightTensor or None= None
Optional element-wise weight tensor of the same shape as input.

Returns

Tensor

Scalar or full-shape, per reduction.

Notes

Per-element loss:

Li={12(xi−yi)2∣xi−yi∣≤δδ (∣xi−yi∣−12δ)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.312)

Used by 2