Skip to content
class

SmoothL1Loss

extends_Loss
SmoothL1Loss(size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean', beta: float = 1.0)

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)={(x−y)22 βif ∣x−y∣<β∣x−y∣−β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 \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 (\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)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean', beta: float = 1.0)

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