class
SmoothL1Loss
extends
ModuleSmoothL1Loss(reduction: str = 'mean', beta: float = 1.0)Smooth L1 loss — a -parameterised Huber loss.
This loss is equivalent to HuberLoss with
but uses a slightly different normalisation convention. The per-element
form is:
When this coincides exactly with the Huber loss.
Parameters
reductionstr= 'mean''none' | 'mean' (default) | 'sum'.betafloat= 1.0Transition threshold between the quadratic and linear regions.
Default
1.0.Attributes
reductionstrThe reduction mode.
betafloatThe threshold .
Notes
- Input
x: . - Target
y: — same shape asx. - 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 approaches MAE; 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)Initialise the SmoothL1Loss module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor, target: Tensor)Compute the loss between predictions and targets.
Parameters
xTensorInput tensor.
targetTensorInput tensor.
Returns
TensorScalar loss (or unreduced tensor depending on reduction).
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.