Measure absolute prediction error
Regression with continuous targets
PyTorch · Class
torch.nn.L1Loss measures mean absolute error between corresponding input and target elements. Its reduction can be disabled, averaged, or summed; mean is the default.
L1Loss compares corresponding elements of input and target using absolute differences. It can leave those differences unreduced, average them across the output, or sum them, with the default being a mean.
torch.nn.modules.loss.L1Loss(size_average=None, reduce=None, reduction='mean')| Name | Type | Description |
|---|---|---|
size_average | bool, optional | Deprecated compatibility option controlling averaging behavior; default is None. |
reduce | bool, optional | Deprecated compatibility option controlling reduction behavior; default is None. |
reduction | str, optional | Output aggregation: 'none', 'mean', or 'sum'; default is 'mean'. |
A scalar loss for reduction='mean' or 'sum', or the unreduced loss tensor for reduction='none'.
import torch
from torch import nn
loss_fn = nn.L1Loss(reduction="mean")
input = torch.tensor([[1.0, 2.0]], requires_grad=True)
target = torch.tensor([[0.0, 4.0]])
loss = loss_fn(input, target)
loss.backward()
print(loss.item()) # 1.5Creates an L1 loss, computes the mean absolute difference, and backpropagates through the result.
Regression with continuous targets
Per-element diagnostics or custom weighting
Custom batch normalization
L1Loss computes an absolute difference for each corresponding input and target element. With the default reduction, it averages the resulting values.
Follow-up: How do you preserve every elementwise loss? Set reduction to none, which disables output reduction.
reduction controls aggregation: none keeps the individual losses, mean divides their sum by the number of output elements, and sum adds them. The default is mean.
Follow-up: When would sum be useful? It avoids the division by the number of elements, so it can be appropriate when the training code performs its own normalization.
The module accepts input and target tensors and applies the forward pass to them. The documented example enables gradients on input, computes the loss, and calls backward.
Follow-up: What shape relationship should be used? The loss is elementwise, so input and target should provide corresponding elements for comparison.
torch.nn.functional.l1_losstorch.nn.SmoothL1Losstorch.nn.MSELoss