A · One neuron
Loss Functions
One number that says how wrong a model is: the choice of that number decides what the model learns.
Try it: A3 · Linear Regression, A5 · Logistic Regression, B4 · Softmax & Multi-class
Intuition
A model makes predictions; a loss function turns "how wrong were they" into one number. Training is then nothing but making that number small. This sounds like bookkeeping, but it is the most important design decision you make, because the model will become exactly as good as the loss says, and no better. A loss that ignores some kind of mistake produces a model that makes it.
Compare two losses for predicting a number. Squared error \(e^2\) barely notices small errors and is furious about large ones: an error of 10 costs 100 times an error of 1. Absolute error \(|e|\) charges every unit the same. Fit a line to data with one wild outlier and the squared-error line swings toward it, while the absolute-error line mostly ignores it. Neither is "right"; they answer different questions.
The math
Regression losses
For a target \(y\) and a prediction \(\hat{y}\), with error \(e = y - \hat{y}\), averaged over \(n\) examples:
\[ \text{MSE} = \frac{1}{n}\sum_i e_i^2, \qquad \text{MAE} = \frac{1}{n}\sum_i |e_i|, \qquad \text{Huber}_\delta(e) = \begin{cases} \tfrac12 e^2 & |e| \le \delta \\ \delta\,(|e| - \tfrac12\delta) & |e| > \delta \end{cases} \]What each one is minimised by tells you what it means. If a model has to predict one constant \(c\) for a set of values, MSE is minimised by their mean, MAE by their median. That is the whole outlier story in one line: one huge value drags a mean, but not a median. Huber is a compromise between the two that stays smooth at zero.
Classification: cross-entropy
A classifier that outputs a probability \(p = P(y = 1)\) is scored by the probability it gave to what actually happened. Taking the negative logarithm, so that "very likely" becomes "small loss":
\[ \text{CE}(y, p) = -\big(y \log p + (1 - y)\log(1 - p)\big) \]If the true class is 1, the loss is \(-\log p\): 0.105 at \(p = 0.9\), 0.69 at \(p = 0.5\), 4.6 at \(p = 0.01\). Confident and wrong is punished without limit. With many classes and a probability vector \(\mathbf{p}\), it is \(-\log p_{y}\), the log of the probability assigned to the correct class; see Softmax.
Where losses come from: maximum likelihood
Most standard losses are not invented; they fall out of one principle. Assume a model of the noise, and choose the parameters that make the observed data most probable. Minimising the negative log-probability of the data then gives:
| Assume the target is… | and the loss becomes |
|---|---|
| prediction + Gaussian noise | squared error (MSE) |
| prediction + Laplace noise | absolute error (MAE) |
| a coin flip with probability \(p\) | binary cross-entropy |
| a die roll with probabilities \(\mathbf{p}\) | categorical cross-entropy |
So choosing a loss is choosing what you believe about the errors. Cross-entropy also has an information-theory reading: it is the average number of bits needed to encode the true labels using the model's probabilities, and it is smallest when those probabilities match reality.
Worked example. Targets \(1, 2, 3, 10\); a model predicts the constant \(c\).
MSE is minimised at the mean \(c = 4\), with errors \(-3, -2, -1, 6\) and MSE \((9 + 4 + 1 + 36)/4 = 12.5\).
MAE is minimised anywhere between the two middle values, \(2 \le c \le 3\); at \(c = 2.5\) the errors are \(-1.5, -0.5, 0.5, 7.5\) and MAE \(= 10/4 = 2.5\). The outlier 10 pulled the MSE answer from 2.5 to 4.
Why the shape matters to training
Gradient descent follows the slope of the loss, so the loss's derivative is how each example pushes on the weights. Squared error's slope is \(-2e\): big errors push hard, small ones gently, and the push fades to nothing at the target, which makes convergence smooth. Absolute error's slope is \(\pm 1\) whatever the error, so it never fades and the steps keep overshooting unless the learning rate shrinks. Cross-entropy paired with a sigmoid has the slope \(p - y\): proportional to how wrong the probability is, never stuck. Pairing a sigmoid with squared error instead multiplies in the sigmoid's slope, which is nearly zero exactly when the model is confidently wrong, and learning stalls.
Pitfalls
- The loss is not the goal. You may care about accuracy, revenue or fairness; the loss is a smooth stand-in that gradient descent can use. Check the real metric too.
- Imbalanced classes. If 99% of examples are negative, predicting "negative" always gives a low loss. Weighting the classes, or a focal loss that down-weights easy examples, fixes the incentive.
- \(\log 0\). A probability of exactly 0 for the true class gives an infinite loss. Real code clips probabilities or computes cross-entropy directly from scores.
History
Squared error is the oldest, from Legendre and Gauss's least squares (1805–1809); Pierre-Simon Laplace had argued for absolute errors decades earlier but they were harder to compute by hand. Peter Huber proposed his loss in 1964, founding robust statistics. Cross-entropy comes from Claude Shannon's information theory (1948); it became the default loss for neural classifiers in the 1990s, once people saw how badly squared error trained sigmoid outputs.
Connections
Builds on
- Linear RegressionMean squared error, the loss of linear regression, is the first loss function; this Topic asks why that one, and what else.
Leads to
- Gradient DescentGradient descent minimises a loss function: the loss is the landscape, gradient descent is the walk down it.
- Logistic RegressionIts loss is cross-entropy, the loss that follows from treating the output as a probability.
- Softmax & Multi-class ClassificationIts loss is cross-entropy over all classes, the multi-class form of the logistic loss.
Further reading
- Visual Information Theory (Chris Olah)Builds entropy and cross-entropy from pictures of codes, so the log in the loss stops being mysterious.
- Linear regression: Loss (Google Machine Learning Crash Course)MSE against MAE on the same data, with how each treats outliers.
- Huber loss (Wikipedia)The loss that is squared near zero and linear far away: robust to outliers and still smooth.