Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.L1Loss

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.

How it works

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.

Syntax

torch.nn.modules.loss.L1Loss(size_average=None, reduce=None, reduction='mean')

Parameters

NameTypeDescription
size_averagebool, optionalDeprecated compatibility option controlling averaging behavior; default is None.
reducebool, optionalDeprecated compatibility option controlling reduction behavior; default is None.
reductionstr, optionalOutput aggregation: 'none', 'mean', or 'sum'; default is 'mean'.

Returns

A scalar loss for reduction='mean' or 'sum', or the unreduced loss tensor for reduction='none'.

Key points

  • L1Loss is a criterion for mean absolute error between corresponding input and target elements.
  • With reduction set to none, the module exposes the individual absolute-error values instead of aggregating them.
  • The mean mode divides the sum of output losses by the number of output elements, while none performs no aggregation.
  • The sum mode adds all output loss elements into one result.
  • The constructor defaults to averaging loss elements across the batch.
  • Calling the module with input and target invokes its forward computation.

Common mistakes

  • The legacy reduce argument changes the output when set to False and takes precedence over size_average.

Example: Compute mean absolute error

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.5

Creates an L1 loss, computes the mean absolute difference, and backpropagates through the result.

When to use it

Measure absolute prediction error

Regression with continuous targets

Inspect unreduced errors

Per-element diagnostics or custom weighting

Aggregate with sum before external scaling

Custom batch normalization

Interview questions

QWhat does L1Loss calculate, and what is its default output behavior?

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.

QHow do the reduction modes differ?

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.

QHow is L1Loss used in a training step?

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_loss
  • torch.nn.SmoothL1Loss
  • torch.nn.MSELoss

Source: L1Loss — PyTorch 2.11 documentation. Checked 2026-10-16.