class
HuberLoss
extends
ModuleHuberLoss(reduction: str = 'mean', delta: float = 1.0)Huber loss — a smooth interpolation between MSE and MAE.
For each element the per-element loss is:
The scalar loss is the mean (or sum, or element-wise) of over all elements in the batch.
For small residuals the loss is quadratic (same as MSE) and for large residuals it is linear (same as MAE), with a smooth join at .
Parameters
reductionstr= 'mean''none' | 'mean' (default) | 'sum'.deltafloat= 1.0Threshold that separates the quadratic and linear regions.
Default
1.0.Attributes
reductionstrThe reduction mode.
deltafloatThe threshold .
Notes
- Input
x: . - Target
y: — same shape asx. - Output : scalar for
'mean'/'sum'; for'none'.
- Choosing controls the robustness/sensitivity trade-off: larger behaves more like MSE, smaller behaves more like MAE.
SmoothL1Lossis identical toHuberLossbut parameterised by and normalised so that the two formulations match when .
Examples
Default :math:`\delta = 1`:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.HuberLoss()
>>> x = lucid.tensor([1.0, 3.0, -1.5])
>>> y = lucid.tensor([1.5, -1.0, 0.0])
>>> loss = criterion(x, y)
Custom threshold to tolerate larger outliers:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.HuberLoss(delta=2.0)
>>> x = lucid.tensor([0.0, 5.0, -4.0])
>>> y = lucid.tensor([0.0, 1.0, 0.0])
>>> loss = criterion(x, y)Methods (3)
dunder
__init__
→None__init__(reduction: str = 'mean', delta: float = 1.0)Initialise the HuberLoss 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.