# Mean squared error (MSE)

> The average squared residual. It is the Gaussian negative log-likelihood, its minimizer is the conditional mean, and its gradient grows linearly with the error, so a single outlier can dominate training.

Source: https://www.mlfoundry.in/learn/mean-squared-error-mse  
Section: Deep Learning › Loss Functions  
Level: core · 10 min read · updated 2026-10-11

**Mean squared error:** is the average of the squared residuals, $\mathrm{MSE}=\frac1n\sum_i (y_i-\hat y_i)^2$. 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

$$
\mathcal L_{\text{MSE}}=\frac1n\sum_{i=1}^{n}(y_i-\hat y_i)^2\qquad\qquad \frac{\partial\mathcal L_{\text{MSE}}}{\partial\hat y_i}=\frac{2}{n}(\hat y_i-y_i)\qquad\qquad \frac{\partial^2\mathcal L_{\text{MSE}}}{\partial\hat y_i^2}=\frac2n
$$

- $y_i,\ \hat y_i$: target and network output for element $i$
- $r_i=y_i-\hat y_i$: residual; the per-element loss is $r_i^2$
- $n$: number of elements averaged over (with `reduction="mean"` PyTorch divides by **all** elements, batch × outputs)
- $\tfrac2n(\hat y_i-y_i)$: 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 $2/n$, so any ill-conditioning in training comes from the network, not from the loss. Many write $\tfrac12 r^2$ instead of $r^2$ so the gradient is simply $\hat y-y$; that only rescales the learning rate.

**Properties**

- **Range:** $[0,\infty)$; units are the target's units squared (report $\mathrm{RMSE}=\sqrt{\mathrm{MSE}}$ to get back to the original units)
- **Minimizer:** the mean of the targets; for a model, the conditional mean $\mathbb E[y\mid x]$
- **Noise model:** Gaussian with fixed variance: $\mathrm{MSE}$ is $2\sigma^2\times$ the NLL plus a constant
- **Smoothness:** infinitely differentiable, convex in $\hat y$, constant curvature $2/n$
- **Influence of one point:** unbounded: the gradient grows linearly with the residual
- **PyTorch:** `nn.MSELoss()`, `F.mse_loss`; `nn.GaussianNLLLoss` when the variance is predicted too

## Why the mean: the minimizer of squared error

Suppose the model can only output one number $c$ for a set of targets (or, for a flexible network, one number per input $x$). Expand the average squared error around the mean $\bar y$:

$$
\frac1n\sum_i (y_i-c)^2=(c-\bar y)^2+\underbrace{\frac1n\sum_i (y_i-\bar y)^2}_{s^2}\qquad\Longrightarrow\qquad \mathbb E\big[(Y-c)^2\mid x\big]=\big(c-\mathbb E[Y\mid x]\big)^2+\operatorname{Var}(Y\mid x)
$$

- $\bar y$: the sample mean; the unique minimizer
- $s^2$: the variance of the targets: the floor no prediction can go below
- $\operatorname{Var}(Y\mid x)$: irreducible noise; a perfect model still pays it

The cross term vanishes because residuals around the mean sum to zero. So $L(c)$ is a parabola with its vertex at the mean and its floor at the variance. A network trained with MSE on enough data learns $\hat y(x)\approx\mathbb E[y\mid x]$: the average of everything that could follow $x$, not the most likely value and not the median.

> Figure: Eight targets and one constant prediction c. The parabola L(c) = (c − ȳ)² + s² has its vertex at the mean (2.50) and its floor at the variance (0.805). At the start, c = 6, the MSE is 13.055 and the slope 2(c − ȳ) = 7.00. Each GD step with η = 0.25 halves the distance to the mean (6 → 4.25 → 3.375 …), η = 0.5 lands on it in one step and η above 1 diverges. Now drag the red outlier y₈ from 2.5 to 12.5: the mean jumps to 3.75, the floor to 11.74, and at the new optimum that one point supplies 81.5% of the squared error. The bump is the Gaussian N(c, 1) whose negative log is the loss.

## Why squares: MSE is the Gaussian negative log-likelihood

Model the target as the network output plus Gaussian noise, $y=\hat y(x)+\varepsilon$ with $\varepsilon\sim\mathcal N(0,\sigma^2)$. The negative log-likelihood of one example is

$$
-\ln p(y\mid x)=\frac{(y-\hat y)^2}{2\sigma^2}+\tfrac12\ln(2\pi\sigma^2)
$$

- $\sigma^2$: noise variance; if it is a fixed constant it only rescales the loss
- $\tfrac12\ln(2\pi\sigma^2)$: does not depend on the network, so it has no gradient. At $\sigma=1$ it is $0.919$

With $\sigma$ 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 $e^{-r^2/2}$, so a residual of 6σ is $e^{-18}\approx1.5\times10^{-8}$ 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 $\sigma^2(x)$ as well and minimize the full NLL. The $\tfrac12\ln\sigma^2$ 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.

$y=(3,\,-0.5,\,2,\,7)$, $\hat y=(2.5,\,0.5,\,0,\,-1)$, $n=4$ (checked numerically in Node)

| 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% |

1. Sum of squares $0.25+1+4+64=69.25$, so $\mathrm{MSE}=69.25/4=17.3125$ and $\mathrm{RMSE}=4.16$.
2. The outlier's gradient $-4$ is 16 times the first example's and larger than the other three combined in magnitude ($0.25+0.5+1=1.75$). One update moves the shared weights mostly to please example 4.
3. Gaussian NLL with $\sigma=1$: $\tfrac12\cdot17.3125+0.919=9.575$ per example. `nn.GaussianNLLLoss` drops the constant and reports $8.656$.
4. Compare (overview article): MAE $=2.875$ and Huber$_{\delta=1}=2.406$ on the same residuals, where the outlier's share drops to 70% and 78%.

```python
import torch, torch.nn as nn, torch.nn.functional as F

y    = torch.tensor([3.0, -0.5, 2.0,  7.0])
yhat = torch.tensor([2.5,  0.5, 0.0, -1.0], requires_grad=True)

loss = nn.MSELoss()(yhat, y)            # mean over every element
loss.backward()
print(loss.item())                      # 17.3125
print(yhat.grad)                        # tensor([-0.2500,  0.5000, -1.0000, -4.0000])  = 2(ŷ − y)/n
F.mse_loss(yhat, y, reduction="sum")    # 69.25

# Same thing as a Gaussian likelihood; var may be predicted by the network (must be > 0)
gnll = nn.GaussianNLLLoss()             # 0.5 * (log var + (ŷ − y)² / var), constant omitted
gnll(yhat, y, torch.ones(4))            # 8.6562 = MSE / 2

# Shape bug: (N, 1) vs (N,) broadcasts to (N, N) and averages the wrong thing
pred = yhat.detach().unsqueeze(1)       # shape (4, 1)
F.mse_loss(pred, y)                     # UserWarning: target size differs from input size
F.mse_loss(pred.squeeze(1), y)          # correct
```

MSE in PyTorch, its gradient, and the Gaussian NLL it comes from

> **Shapes and scales:** Two silent bugs. **Broadcasting**: a model output of shape `(N, 1)` against targets of shape `(N,)` gives an `(N, N)` matrix of differences; PyTorch only warns. **Target scale**: MSE is in squared units, so targets in the thousands produce gradients a million times larger than targets near 1 and wreck a learning rate tuned elsewhere. Standardize targets (subtract the mean, divide by the standard deviation) and undo it at prediction time.

> **The mean of a multimodal target is blurry:** If the same input can be followed by two very different targets, MSE predicts their average, which may look like neither. This is why pixel-wise MSE gives blurry image and video predictions (Mathieu, Couprie and LeCun, 2016, "Deep multi-scale video prediction beyond mean square error") and why image-to-image models such as pix2pix (Isola et al., 2017) pair an L1 term with an adversarial loss instead.

> **MSE on probabilities:** Using MSE on a sigmoid or softmax output is legal but slow: the gradient picks up a factor $p(1-p)$ that vanishes exactly when the model is confidently wrong. For classification use cross-entropy on the logits (see **Loss functions**). The squared error on probabilities, the **Brier score**, is still a good *evaluation* metric for calibration.

**Strengths**

- 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 $\mathbb E[y\mid x]$
- Closed-form solution for linear models (ordinary least squares)

**Limitations**

- 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

**Question:** Targets are 1, 2, 3 and 10. Which single constant prediction minimizes the MSE, and what is the minimum MSE?

- A. c = 2.5 (the median), MSE 14.75
- B. c = 4 (the mean), MSE 12.5
- C. c = 3, MSE 13.5
- D. c = 5.5 (the midrange), MSE 14.75

**Answer:** B. c = 4 (the mean), MSE 12.5 — $L(c)=(c-\bar y)^2+s^2$ is minimized at $\bar y=16/4=4$, where it equals the variance $s^2=(9+4+1+36)/4=12.5$. The median 2.5 would minimize MAE instead; under MSE it costs $(2.5-4)^2+12.5=14.75$, and so does the midrange 5.5.

**Question:** You fit a constant $c$ by gradient descent on $L(c)=\frac1n\sum_i(y_i-c)^2$. With which learning rate does one step land exactly on the mean from any starting point?

- A. η = 0.25
- B. η = 0.5
- C. η = 1
- D. No fixed η can do that

**Answer:** B. η = 0.5 — $c\leftarrow c-\eta\cdot2(c-\bar y)$ gives $c-\bar y\leftarrow(1-2\eta)(c-\bar y)$. At $\eta=0.5$ the factor is 0. At $\eta=0.25$ the distance halves each step, and for $\eta>1$ the factor exceeds 1 in magnitude and GD diverges: the curvature $2$ sets the step-size limit $\eta<2/2=1$.

**Key points**

- $\mathrm{MSE}=\frac1n\sum(y-\hat y)^2$, gradient $\frac2n(\hat y-y)$: linear in the error, constant curvature.
- Minimizer is the mean; a flexible model learns $\mathbb E[y\mid x]$ and still pays $\operatorname{Var}(y\mid x)$.
- 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.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
