Definition
Mean squared error is the average of the squared residuals, . It is the default loss for regression: smooth, convex in the prediction, and the loss you get from maximum likelihood when the noise on the targets is Gaussian.
Squaring does two jobs. It makes every error positive, and it makes big errors count much more than small ones: an error of 2 costs 4 times as much as an error of 1, and an error of 10 costs 100 times as much. That is exactly right when errors really are Gaussian, and exactly wrong when the data contain outliers.
The idea is over two centuries old. Legendre published the method of least squares in 1805 for fitting comet orbits. Gauss (1809) justified it probabilistically: least squares gives the most probable parameters when the errors follow what we now call the normal distribution, and he claimed to have used it since 1795. Every linear regression, every autoencoder trained on pixel error and the simplified training objective of DDPM diffusion models (Ho et al., 2020), which regresses the added noise, minimize this same quantity.
Formula and gradient
- target and network output for element
- residual; the per-element loss is
- number of elements averaged over (with
reduction="mean"PyTorch divides by all elements, batch × outputs) - the error signal sent into backprop: proportional to the residual, so it is unbounded
The gradient is linear in the error. Far from the target it is large, so learning is fast; near the target it shrinks to zero, so plain gradient descent slows down and settles smoothly without needing a decaying learning rate. The curvature is a constant , so any ill-conditioning in training comes from the network, not from the loss. Many write instead of so the gradient is simply ; that only rescales the learning rate.
Properties
- Range
- ; units are the target's units squared (report to get back to the original units)
- Minimizer
- the mean of the targets; for a model, the conditional mean
- Noise model
- Gaussian with fixed variance: is the NLL plus a constant
- Smoothness
- infinitely differentiable, convex in , constant curvature
- Influence of one point
- unbounded: the gradient grows linearly with the residual
- PyTorch
nn.MSELoss(),F.mse_loss;nn.GaussianNLLLosswhen the variance is predicted too
Why the mean: the minimizer of squared error
Suppose the model can only output one number for a set of targets (or, for a flexible network, one number per input ). Expand the average squared error around the mean :
- the sample mean; the unique minimizer
- the variance of the targets: the floor no prediction can go below
- irreducible noise; a perfect model still pays it
The cross term vanishes because residuals around the mean sum to zero. So is a parabola with its vertex at the mean and its floor at the variance. A network trained with MSE on enough data learns : the average of everything that could follow , not the most likely value and not the median.
Why squares: MSE is the Gaussian negative log-likelihood
Model the target as the network output plus Gaussian noise, with . The negative log-likelihood of one example is
- noise variance; if it is a fixed constant it only rescales the loss
- does not depend on the network, so it has no gradient. At it is
With fixed, minimizing the summed NLL and minimizing MSE pick the same parameters, which is Gauss's argument. The choice of loss is therefore a modelling assumption: symmetric, light-tailed noise of the same size everywhere. Gaussian tails fall off like , so a residual of 6σ is times less likely than a perfect fit; the only way the model can make it less improbable is to move the fit toward it. That is the probabilistic reading of outlier sensitivity.
If the noise level varies with the input (heteroscedastic data), let the network predict a variance as well and minimize the full NLL. The term then stops the model from claiming infinite uncertainty. PyTorch implements this as nn.GaussianNLLLoss; with the variance fixed at 1 it is exactly half the MSE.
Worked example
Four predictions against four targets; the last one is badly wrong. The residuals are the same ones used in the Loss functions overview.
| i | residual r = y − ŷ | r² | ∂L/∂ŷᵢ = 2(ŷᵢ − yᵢ)/4 | share of loss |
|---|---|---|---|---|
| 1 | 0.5 | 0.25 | −0.25 | 0.4% |
| 2 | −1 | 1 | 0.5 | 1.4% |
| 3 | 2 | 4 | −1 | 5.8% |
| 4 | 8 | 64 | −4 | 92.4% |
- 1Sum of squares , so and .
- 2The outlier's gradient is 16 times the first example's and larger than the other three combined in magnitude (). One update moves the shared weights mostly to please example 4.
- 3Gaussian NLL with : per example.
nn.GaussianNLLLossdrops the constant and reports . - 4Compare (overview article): MAE and Huber on the same residuals, where the outlier's share drops to 70% and 78%.
1import torch, torch.nn as nn, torch.nn.functional as F23y = torch.tensor([3.0, -0.5, 2.0, 7.0])4yhat = torch.tensor([2.5, 0.5, 0.0, -1.0], requires_grad=True)56loss = nn.MSELoss()(yhat, y) # mean over every element7loss.backward()8print(loss.item()) # 17.31259print(yhat.grad) # tensor([-0.2500, 0.5000, -1.0000, -4.0000]) = 2(ŷ − y)/n10F.mse_loss(yhat, y, reduction="sum") # 69.251112# Same thing as a Gaussian likelihood; var may be predicted by the network (must be > 0)13gnll = nn.GaussianNLLLoss() # 0.5 * (log var + (ŷ − y)² / var), constant omitted14gnll(yhat, y, torch.ones(4)) # 8.6562 = MSE / 21516# Shape bug: (N, 1) vs (N,) broadcasts to (N, N) and averages the wrong thing17pred = yhat.detach().unsqueeze(1) # shape (4, 1)18F.mse_loss(pred, y) # UserWarning: target size differs from input size19F.mse_loss(pred.squeeze(1), y) # correctUse when
- +Smooth and convex in the prediction, with constant curvature: easy for every optimizer
- +Gradient shrinks to 0 at the optimum, so training settles without a learning-rate schedule
- +Statistically optimal (maximum likelihood) under Gaussian noise; estimates
- +Closed-form solution for linear models (ordinary least squares)
Avoid when
- −Outliers dominate: influence grows linearly with the residual
- −Squared units: sensitive to target scaling
- −Predicts the mean, which is blurry or unrepresentative for multimodal targets
- −Poor gradient on sigmoid/softmax outputs; use cross-entropy for classification
Check yourself
Targets are 1, 2, 3 and 10. Which single constant prediction minimizes the MSE, and what is the minimum MSE?
Check yourself
You fit a constant by gradient descent on . With which learning rate does one step land exactly on the mean from any starting point?
Remember
- ✓, gradient : linear in the error, constant curvature.
- ✓Minimizer is the mean; a flexible model learns and still pays .
- ✓MSE is the negative log-likelihood of Gaussian noise with fixed variance, up to scale and a constant; predict the variance too with GaussianNLLLoss.
- ✓Unbounded influence: one large residual can supply most of the loss and the gradient. Use MAE or Huber when outliers are real.
- ✓Standardize targets and check that prediction and target shapes match.