smooth_l1_loss
→Tensorsmooth_l1_loss(x: Tensor, target: Tensor, beta: float = 1.0, reduction: str = 'mean')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
xTensortargetTensorx.betafloat= 1.01.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
TensorScalar ("mean"/"sum") or full-shape ("none").
Notes
Per-element loss:
Continuously differentiable everywhere; gradient is inside the quadratic region and 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)