class

SmoothL1Loss

extendsModule
SmoothL1Loss(reduction: str = 'mean', beta: float = 1.0)
source

Smooth L1 loss — a β\beta-parameterised Huber loss.

This loss is equivalent to HuberLoss with δ=β\delta = \beta but uses a slightly different normalisation convention. The per-element form is:

β(x,y)={(xy)22βif xy<βxyβ2otherwise\ell_\beta(x, y) = \begin{cases} \dfrac{(x - y)^2}{2\,\beta} & \text{if } |x - y| < \beta \\[6pt] |x - y| - \dfrac{\beta}{2} & \text{otherwise} \end{cases}

When β=1\beta = 1 this coincides exactly with the Huber loss.

Parameters

reductionstr= 'mean'
'none' | 'mean' (default) | 'sum'.
betafloat= 1.0
Transition threshold between the quadratic and linear regions. Default 1.0.

Attributes

reductionstr
The reduction mode.
betafloat
The threshold β\beta.

Notes

  • Input x : ()(*).
  • Target y : ()(*) — same shape as x.
  • Output : scalar for 'mean' / 'sum'; ()(*) for 'none'.
  • Smooth L1 is commonly used in object detection (bounding-box regression) because it is less sensitive to outlier predictions than MSE while still being differentiable everywhere.
  • Setting β0\beta \to 0 approaches MAE; β\beta \to \infty approaches MSE (for bounded residuals).

Examples

Default :math:`\beta = 1`:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.SmoothL1Loss()
>>> x = lucid.tensor([0.5, 2.0, -1.0])
>>> y = lucid.tensor([0.0, 0.0,  0.0])
>>> loss = criterion(x, y)
Tighter quadratic region (:math:`\beta = 0.5`):
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.SmoothL1Loss(beta=0.5)
>>> x = lucid.tensor([0.3, 1.5, -0.2])
>>> y = lucid.tensor([0.0, 0.0,  0.0])
>>> loss = criterion(x, y)

Methods (3)

dunder

__init__

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

Initialise the SmoothL1Loss 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.