fn

smooth_l1_loss

Tensor
smooth_l1_loss(x: Tensor, target: Tensor, beta: float = 1.0, reduction: str = 'mean')
source

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

xTensor
Predicted values, any shape.
targetTensor
Target values; broadcast-compatible with x.
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.
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

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

Notes

Per-element loss:

Li={12(xiyi)2/βxiyi<βxiyi12β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 (xiyi)/β(x_i - y_i)/\beta inside the quadratic region and sign(xiyi)\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.3125)