PyTorch torch.nn.L1Loss Function
PyTorch torch.nn Reference Manual
torch.nn.L1LossIt is the L1 loss function in PyTorch, also known as Mean Absolute Error (MAE).
Function Definition
torch.nn.L1Loss(reduction='mean')
Usage Example
Example 1: Basic Usage
Example
import torch
import torch.nn as nn
criterion = nn.L1Loss()
pred = torch.tensor([3.0, 4.0, 5.0])
target = torch.tensor([2.0, 4.5, 5.5])
loss = criterion(pred, target)
print("L1 Loss:", loss.item())
# Manual verification
manual = (pred - target).abs().mean()
print("Manual calculation:", manual.item())
import torch.nn as nn
criterion = nn.L1Loss()
pred = torch.tensor([3.0, 4.0, 5.0])
target = torch.tensor([2.0, 4.5, 5.5])
loss = criterion(pred, target)
print("L1 Loss:", loss.item())
# Manual verification
manual = (pred - target).abs().mean()
print("Manual calculation:", manual.item())
Example 2: Comparison with MSE
Example
import torch
import torch.nn as nn
pred = torch.tensor([10.0, 20.0])
target = torch.tensor([0.0, 0.0])
print("L1 Loss (Robust to outliers):", nn.L1Loss()(pred, target).item())
print("MSE Loss (Sensitive to outliers):", nn.MSELoss()(pred, target).item())
import torch.nn as nn
pred = torch.tensor([10.0, 20.0])
target = torch.tensor([0.0, 0.0])
print("L1 Loss (Robust to outliers):", nn.L1Loss()(pred, target).item())
print("MSE Loss (Sensitive to outliers):", nn.MSELoss()(pred, target).item())
Use Cases
- Regression tasks: Require robustness
- Outliers: Noisy data
- L1 regularization: Sparsification
Other Extensions