class

HuberLoss

extendsModule
HuberLoss(reduction: str = 'mean', delta: float = 1.0)
source

Huber loss — a smooth interpolation between MSE and MAE.

For each element the per-element loss is:

δ(x,y)={12(xy)2if xy<δδ ⁣(xyδ2)otherwise\ell_\delta(x, y) = \begin{cases} \tfrac{1}{2}(x - y)^2 & \text{if } |x - y| < \delta \\[4pt] \delta \!\left(|x - y| - \tfrac{\delta}{2}\right) & \text{otherwise} \end{cases}

The scalar loss is the mean (or sum, or element-wise) of δ\ell_\delta over all elements in the batch.

For small residuals the loss is quadratic (same as MSE) and for large residuals it is linear (same as MAE), with a smooth join at xy=δ|x - y| = \delta.

Parameters

reductionstr= 'mean'
'none' | 'mean' (default) | 'sum'.
deltafloat= 1.0
Threshold that separates the quadratic and linear regions. Default 1.0.

Attributes

reductionstr
The reduction mode.
deltafloat
The threshold δ\delta.

Notes

  • Input x : ()(*).
  • Target y : ()(*) — same shape as x.
  • Output : scalar for 'mean' / 'sum'; ()(*) for 'none'.
  • Choosing δ\delta controls the robustness/sensitivity trade-off: larger δ\delta behaves more like MSE, smaller δ\delta behaves more like MAE.
  • SmoothL1Loss is identical to HuberLoss but parameterised by β=δ\beta = \delta and normalised so that the two formulations match when δ=β=1\delta = \beta = 1.

Examples

Default :math:`\delta = 1`:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.HuberLoss()
>>> x = lucid.tensor([1.0,  3.0, -1.5])
>>> y = lucid.tensor([1.5, -1.0,  0.0])
>>> loss = criterion(x, y)
Custom threshold to tolerate larger outliers:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.HuberLoss(delta=2.0)
>>> x = lucid.tensor([0.0, 5.0, -4.0])
>>> y = lucid.tensor([0.0, 1.0,  0.0])
>>> loss = criterion(x, y)

Methods (3)

dunder

__init__

None
__init__(reduction: str = 'mean', delta: float = 1.0)
source

Initialise the HuberLoss module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor, target: Tensor)
source

Compute the loss between predictions and targets.

Parameters

xTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.