PyTorch Mean Squared Error
Mean squared error is a loss that averages the squared differences between predictions and targets. PyTorch provides it as the class nn.MSELoss.
The differences
Each difference says how far one prediction sits from its target.
import torch
predictions = torch.tensor([2.0, 4.0, 6.0])
targets = torch.tensor([3.0, 4.0, 5.0])
print(predictions - targets)Square and average by hand
Squaring each difference and averaging the results gives the MSE by hand.
import torch
predictions = torch.tensor([2.0, 4.0, 6.0])
targets = torch.tensor([3.0, 4.0, 5.0])
differences = predictions - targets
print((differences ** 2).mean())The library loss
nn.MSELoss does the same steps in one call.
import torch
predictions = torch.tensor([2.0, 4.0, 6.0])
targets = torch.tensor([3.0, 4.0, 5.0])
loss_fn = torch.nn.MSELoss()
print(loss_fn(predictions, targets))Example
import torch
predictions = torch.tensor([2.0, 4.0, 6.0])
targets = torch.tensor([3.0, 4.0, 5.0])
differences = predictions - targets
hand_mse = (differences ** 2).mean()
loss_fn = torch.nn.MSELoss()
library_mse = loss_fn(predictions, targets)
print(f"Differences: {differences.tolist()}")
print(f"Hand MSE: {hand_mse.item():.4f}")
print(f"Library MSE: {library_mse.item():.4f}")Key points
- Difference: prediction minus target, for each value
- Squaring makes every difference positive
- Squaring punishes one big miss more than several small ones
- Mean: the average of the values, all added up and divided by the count
- Use nn.MSELoss() to create the loss, then call it with predictions and targets
- MSE turns a list of misses into one average score
In this example, the predictions are 2, 4, and 6. The targets are 3, 4, and 5. The differences are -1, 0, and 1. Squaring gives 1, 0, and 1, and the mean of those is 0.6667. The hand calculation prints that number first. The library loss then prints the same number for the same inputs.
Note
The loss returns a tensor, and .item() turns it into a plain Python number
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Python Playground
Exercise
One prediction misses its target by 3. What does that miss become before the averaging step in MSE?