Skip to content
fn

l1_loss

→Tensor
l1_loss(input: Tensor, target: Tensor, size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean', weight: Tensor | None = None)

Mean-absolute-error (L1) loss between input and target.

Robust regression loss whose gradient is constant in magnitude (±1\pm 1) and therefore insensitive to outliers — extreme residuals do not dominate the update direction as they do under mse_loss. The trade-off is a non-smooth optimisation surface at x=yx = y (sub-gradient at zero), which can produce slightly slower convergence for small errors. Often paired with smooth_l1_loss to recover smoothness while keeping outlier robustness.

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".
weightTensor or None= None
Optional element-wise weight tensor of the same shape as input.

Returns

Tensor

Scalar loss for "mean" / "sum", or a per-element tensor of input and target's broadcast shape for "none".

Notes

Per-element loss:

Li=∣xi−yi∣L_i = |x_i - y_i|

Gradient w.r.t. input is sign⁡(xi−yi)\operatorname{sign}(x_i - y_i) (zero at the origin). Used heavily in image-to-image regression (super-resolution, denoising) where outlier-robustness matters.

Examples

>>> import lucid
>>> from lucid.nn.functional import l1_loss
>>> pred = lucid.tensor([1.0, 2.0, 3.0])
>>> target = lucid.tensor([1.5, 2.5, 2.5])
>>> l1_loss(pred, target)
tensor(0.5)

Used by 2