Skip to content
class

HuberLoss

extends_Loss
HuberLoss(reduction: Reduction = 'mean', delta: float = 1.0)

Implementing kernel

C++ engine symbols that back this Python API.

Huber loss — a smooth interpolation between MSE and MAE.

For each element the per-element loss is:

ℓδ(x,y)={12(x−y)2if ∣x−y∣<δδ ⁣(∣x−y∣−δ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 ∣x−y∣=δ|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 \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)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(reduction: Reduction = 'mean', delta: float = 1.0)

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

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor, target: Tensor)

Compute the loss between predictions and targets.

Parameters

inputTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).