fn

l1_loss

Tensor
l1_loss(x: Tensor, target: Tensor, reduction: str = 'mean')
source

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

xTensor
Predicted values, any shape.
targetTensor
Target values; broadcast-compatible with x.
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar loss for "mean" / "sum", or a per-element tensor with x's shape for "none".

Notes

Per-element loss:

Li=xiyiL_i = |x_i - y_i|

Gradient w.r.t. x is sign(xiyi)\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)