Implementation:Fastai Fastbook Loss Functions
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep Learning, Classification, Loss Functions |
| Last Updated | 2026-02-09 17:00 GMT |
Overview
Concrete tool for measuring distance between tensors provided by torch.nn.functional, wrapping L1 loss (mean absolute error) and MSE loss (mean squared error) as standard PyTorch loss functions.
Description
PyTorch provides F.l1_loss and F.mse_loss as built-in implementations of the two most common distance metrics. In the fastbook Chapter 4 "pixel similarity" baseline, these functions measure how far a single digit image is from the per-class mean template. The image is classified by determining which template yields a smaller distance.
Usage
Import these functions when you need to:
- Compute L1 or L2 distance between two tensors of the same shape.
- Establish a baseline classifier using template matching.
- Use a standard loss function in a training loop (MSE loss is differentiable and commonly used for regression).
Code Reference
Source Location
- Repository: fastbook
- File: 04_mnist_basics.ipynb (Chapter 4), "Pixel Similarity" section
Signature
torch.nn.functional.l1_loss(input, target, reduction='mean') -> Tensor
torch.nn.functional.mse_loss(input, target, reduction='mean') -> Tensor
# Manual equivalents from the book:
dist_l1 = (a_3 - mean3).abs().mean()
dist_mse = ((a_3 - mean3)**2).mean().sqrt()
Import
import torch.nn.functional as F
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | Tensor, shape (H, W) or any shape |
Yes | The image tensor (or prediction tensor) to measure distance from |
| target | Tensor, same shape as input |
Yes | The template tensor (or ground-truth tensor) to measure distance to |
| reduction | str |
No | How to reduce the output: 'mean' (default), 'sum', or 'none'
|
Outputs
| Name | Type | Description |
|---|---|---|
| loss | Tensor (scalar when reduction='mean') |
The computed L1 or MSE distance between input and target |
Usage Examples
Basic Usage
import torch
import torch.nn.functional as F
from fastai.vision.all import *
# Load data and compute mean templates
path = untar_data(URLs.MNIST_SAMPLE)
threes = (path/'train'/'3').ls().sorted()
sevens = (path/'train'/'7').ls().sorted()
stacked_threes = torch.stack([tensor(Image.open(o)) for o in threes]).float() / 255
stacked_sevens = torch.stack([tensor(Image.open(o)) for o in sevens]).float() / 255
mean3 = stacked_threes.mean(0)
mean7 = stacked_sevens.mean(0)
# Pick a sample digit 3
a_3 = stacked_threes[1]
# Measure L1 distance to each template
dist_3_l1 = F.l1_loss(a_3, mean3)
dist_7_l1 = F.l1_loss(a_3, mean7)
print(f"L1 to mean3: {dist_3_l1:.4f}, L1 to mean7: {dist_7_l1:.4f}")
# L1 to mean3: 0.1114, L1 to mean7: 0.1586
# Measure MSE distance to each template
dist_3_mse = F.mse_loss(a_3, mean3).sqrt()
dist_7_mse = F.mse_loss(a_3, mean7).sqrt()
print(f"RMSE to mean3: {dist_3_mse:.4f}, RMSE to mean7: {dist_7_mse:.4f}")
# RMSE to mean3: 0.2021, RMSE to mean7: 0.3021
# Classification: closer to mean3 -> predict 3
if dist_3_l1 < dist_7_l1:
print("Predicted: 3")
else:
print("Predicted: 7")
Manual Computation (Without F.l1_loss / F.mse_loss)
# These are equivalent to the PyTorch built-in functions:
dist_3_abs = (a_3 - mean3).abs().mean() # same as F.l1_loss(a_3, mean3)
dist_3_sqr = ((a_3 - mean3)**2).mean().sqrt() # same as F.mse_loss(a_3, mean3).sqrt()
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment