Roadmap / Loss Functions

✓

Mean Squared Error

easylossesgradients

The problem

When a model predicts real numbers (a price, a reward, the brightness of a pixel), the standard loss is the MSE (mean squared error): for every prediction, take the error, square it, and average all the squares. It is the negative log-likelihood of Gaussian noise (see the theory page), and its gradient is the simplest one in this section.

The loss

For predictions and targets of the same shape, with nn numbers in total:

MSE(y^,y)=1n∑i=1n(y^i−yi)2\mathrm{MSE}(\hat y, y) = \frac{1}{n} \sum_{i=1}^{n} (\hat y_i - y_i)^2

where:

  • y^\hat y ("y hat") is the prediction pred, and y^i\hat y_i is its number ii;
  • yy is the target, and yiy_i is its number ii;
  • y^i−yi\hat y_i - y_i is the error on number ii, and ii runs over all nn numbers, whatever the shape.

Squaring does two things: every error becomes positive, so errors in opposite directions cannot cancel out; and large errors count much more than small ones (an error of 2 costs 4, an error of 10 costs 100).

Example. pred = [1.5, 2, 2], target = [1, 2, 3].

  1. Errors: 1.5−1=0.51.5 - 1 = 0.5, 2−2=02 - 2 = 0, 2−3=−12 - 3 = -1.
  2. Squares: 0.250.25, 00, 11.
  3. Average: (0.25+0+1)/3≈0.417(0.25 + 0 + 1) / 3 \approx 0.417.

The gradient

The gradient says, for every prediction, how the loss changes when that prediction moves. Only one term of the sum contains y^i\hat y_i, and the derivative of (y^i−yi)2(\hat y_i - y_i)^2 is 2(y^i−yi)2(\hat y_i - y_i). With the 1/n1/n in front:

∂ MSE∂y^i=2n(y^i−yi)\frac{\partial\,\mathrm{MSE}}{\partial \hat y_i} = \frac{2}{n} (\hat y_i - y_i)

where the symbols are the same as above.

Example. Same numbers: errors (0.5,0,−1)(0.5, 0, -1), n=3n = 3, so the gradient is 23(0.5,0,−1)≈(0.333,0,−0.667)\tfrac{2}{3}(0.5, 0, -1) \approx (0.333, 0, -0.667).

Training moves against the gradient: the first prediction goes down (it was too high), the second stays (it was right), the third goes up (it was too low).

What to implement

  • mse(pred, target): the loss, a single number (a Python float or a 0-d array).
  • mse_backward(pred, target): the gradient, an array shaped like pred.

Requirements

  • Any shape: the mean is over all numbers, not over one axis.
  • If the shapes differ, raise a ValueError. Otherwise broadcasting hides a classic bug: pred of shape (N,1)(N, 1) minus target of shape (N,)(N,) is an N×NN \times N table, and the loss is wrong without any error. For example, [[1], [2]] - [1, 2] is [[0, -1], [1, 0]], whose MSE is 0.50.5, although the predictions are perfect.
  • No Python loops.

Hint

pred.size is the total number of elements nn, whatever the shape.

solution.py
Python idle

Press Run (⌘/Ctrl + Enter) to test your code.