Skip to content
fn

smooth_l1_loss

→Tensor
smooth_l1_loss(input: Tensor, target: Tensor, size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean', beta: float = 1.0)

Smooth L1 loss — a quadratic-near-zero, linear-far-from-zero hybrid.

Combines the best of mse_loss and l1_loss: the quadratic region near the origin gives a smooth gradient and fast convergence, while the linear tails make the loss robust to outliers. This is the standard regression head used in Fast R-CNN-style object detection (bounding-box regression), where outlier bounding boxes would otherwise dominate the training signal.

The function is a thin wrapper around huber_loss with delta = beta and an additional 1/beta scaling inside the quadratic region (so the loss has unit slope at the transition).

Parameters

inputTensor
Predicted values, any shape.
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".
size_averagestr= 'mean'
"mean" (default), "sum", or "none".
reducestr= 'mean'
"mean" (default), "sum", or "none".
betafloat= 1.0
Transition point between quadratic and linear regions (default 1.0). Smaller beta makes the loss behave more like l1_loss; larger beta makes it behave more like mse_loss. A negative (or NaN) beta raises ValueError.

Returns

Tensor

Scalar ("mean"/"sum") or full-shape ("none").

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 / \beta & |x_i - y_i| < \beta \\ |x_i - y_i| - \tfrac{1}{2}\beta & \text{otherwise} \end{cases}

Continuously differentiable everywhere; gradient is (xi−yi)/β(x_i - y_i)/\beta inside the quadratic region and sign⁡(xi−yi)\operatorname{sign}(x_i - y_i) outside.

Examples

>>> import lucid
>>> from lucid.nn.functional import smooth_l1_loss
>>> pred = lucid.tensor([0.0, 2.0])
>>> target = lucid.tensor([0.5, 5.0])
>>> smooth_l1_loss(pred, target, beta=1.0)
tensor(1.312)

Used by 2