0
0
PytorchHow-ToBeginner · 3 min read

How to Use L1Loss in PyTorch: Simple Guide and Example

Use torch.nn.L1Loss() to create an L1 loss function in PyTorch, which calculates the mean absolute error between predictions and targets. Call it with model outputs and true values to get the loss value for training or evaluation.
📐

Syntax

The torch.nn.L1Loss class creates a loss function that computes the mean absolute error between predicted and target tensors.

  • reduction: Specifies how to reduce the loss over the batch. Options are 'mean' (default), 'sum', or 'none'.
python
loss_fn = torch.nn.L1Loss(reduction='mean')
loss = loss_fn(predictions, targets)
💻

Example

This example shows how to create an L1 loss function, compute the loss between predicted and true values, and print the result.

python
import torch

# Create example tensors for predictions and targets
predictions = torch.tensor([2.5, 0.0, 2.0, 8.0])
targets = torch.tensor([3.0, -0.5, 2.0, 7.0])

# Initialize L1Loss with default reduction='mean'
loss_fn = torch.nn.L1Loss()

# Calculate the loss
loss = loss_fn(predictions, targets)

print(f'L1 Loss: {loss.item():.4f}')
Output
L1 Loss: 0.5000
⚠️

Common Pitfalls

Common mistakes when using L1Loss include:

  • Passing inputs and targets with different shapes, causing runtime errors.
  • Forgetting to convert data to torch.Tensor type.
  • Using the wrong reduction mode without understanding its effect on the loss output.

Always ensure predictions and targets have the same shape and type.

python
import torch

# Wrong: different shapes
pred = torch.tensor([1.0, 2.0])
target = torch.tensor([1.0, 2.0, 3.0])
loss_fn = torch.nn.L1Loss()

try:
    loss = loss_fn(pred, target)
except Exception as e:
    print(f'Error: {e}')

# Right: matching shapes
pred = torch.tensor([1.0, 2.0, 3.0])
target = torch.tensor([1.0, 2.0, 3.0])
loss = loss_fn(pred, target)
print(f'Correct loss: {loss.item()}')
Output
Error: Target and input must have the same number of elements. target nelement (3) != input nelement (2) Correct loss: 0.0
📊

Quick Reference

L1Loss key points:

  • Computes mean absolute error between predictions and targets.
  • Use reduction='mean' for average loss, 'sum' for total loss, or 'none' for element-wise loss.
  • Inputs and targets must be same shape and type torch.Tensor.
  • Commonly used for regression problems.

Key Takeaways

Use torch.nn.L1Loss() to compute mean absolute error between predictions and targets.
Ensure predictions and targets are torch.Tensors with the same shape.
Choose the reduction mode ('mean', 'sum', or 'none') based on your training needs.
L1Loss is useful for regression tasks where absolute differences matter.
Watch out for shape mismatches to avoid runtime errors.