Overfitting, Bias–Variance and Regularization
A model that fits its training data too well memorizes the noise and does worse on new data. Expected test error splits into bias² + variance + noise, and regularization (L2 weight decay, which is really a Gaussian prior on the weights, or dropout, which averages many thinned networks) gives up a little bias to cut a lot of variance.
What the symbols mean
- Trained model's prediction
Random, because it depends on which training set you happened to get.
- True function
The real pattern you're trying to learn.
- Noise
The variance of the unpredictable noise in y. No model can remove it.
What the symbols mean
- Data loss
The ordinary training loss.
- Regularization strength
How hard large weights are penalized.
What the symbols mean
- Learning rate
The step size of gradient descent.
Explanation
Overfitting is simply when a model learns the quirks of its particular training examples instead of the pattern behind them, so it looks brilliant on data it has seen and disappoints on data it hasn't. The name is literal: it fits the training data over what the signal justifies, chasing noise. The fix comes from understanding where test error comes from. It splits into three pieces: (the model is too simple to capture the pattern, so it's wrong the same way every time), (the model is so flexible that it changes a lot depending on which examples it happened to see) and (noise in the data that nobody can predict). Regularization deliberately adds a little bias to cut a lot of variance. Three pieces and one trade-off; that's the whole lesson.
Training error lies. As a model gets more flexible (more parameters, a higher-degree polynomial, more training steps), its training error only goes down: a flexible enough model can memorize every example. Test error, measured on held-out data, goes down at first and then turns back up. The first picture further down shows this U shape. Training error measures memory; test error measures understanding.
Where the U comes from: the bias–variance decomposition. Say the truth is , with noise of mean 0 and variance , and is what training produced. is random, because a different training set would have given a different model. Write for the average model. Add and subtract and :
Square it and take the expected value. The cross terms all vanish: is independent of the training set and has mean 0, has mean 0 by the definition of , and is just a fixed number. What survives is
Simple models have high bias and low variance; flexible models are the reverse. Test error is their sum, so it's smallest somewhere in the middle. More data shrinks the variance (each model sees more of the pattern and less of any one example's noise), which is why bigger datasets can support bigger models.
L2 regularization, or weight decay. Add a penalty on large weights to the loss: . Its gradient is , so a gradient descent step becomes
Every step first shrinks the weights by the factor , then takes the usual step. That's why it's called weight decay, and it's the same shrink you saw in The Learning Rate and Convergence. Small weights make a smoother function, one that can't bend sharply to hit every noisy point.
Weight decay is a prior. Here's the connection that makes it feel inevitable. By Bayes' rule, the posterior over the weights is proportional to likelihood × prior. Maximizing it, instead of the likelihood alone, is called MAP (maximum a posteriori) estimation. Take :
The first term is the usual loss from Maximum Likelihood and Cross-Entropy. If the prior says each weight is normal with mean 0 and standard deviation , then . That's an L2 penalty! With Gaussian noise of standard deviation , the data term is . Multiply everything by and the objective becomes . So in that form : noisy data or a tight prior means strong regularization. (A Laplace prior gives the L1 penalty instead, which pushes weights all the way to 0.)
Dropout is averaging. Dropout randomly switches off units during training, keeping each one with probability . Every mini-batch then trains a different thinned network, and with units there are of them, all sharing one set of weights. At test time all the units stay on, and the activations are scaled so their expected value matches training (in "inverted dropout", the kept units are multiplied by during training instead). The result behaves like an average over that huge ensemble, and averaging kills variance: the mean of independent models, each with prediction variance , has variance (the same fact behind the central limit theorem). The thinned networks aren't fully independent, so the real gain is smaller, but it goes the same way.
A common mistake. It's tempting to choose the model, the degree or by whichever gives the lowest training error. That always picks the most flexible model and , because training error rewards memorizing. Choose on a held-out validation set instead, and touch the test set only once, at the very end.
See it
Worked examples
To study a model, an engineer trains it on four different training sets and records its prediction at one input : , , and . The true value there is , and the noise variance is . Estimate the bias², the variance and the expected test error at .
- 1
First find the average model's prediction, , by averaging the four: .
Fit to the data by minimizing with . Compare with the unregularized answer from Maximum Likelihood and Cross-Entropy.
- 1
Write the objective out: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A model is trained on five different training sets, and its predictions at one input are . The true value there is 4, and the noise variance is 1. What is the expected test error at that input?
A weight is , its loss gradient is , the learning rate is and the weight decay is . What is after one step of ?
A weight that the data no longer pushes on () is updated with and . By what factor has it shrunk after 1000 steps? Give 4 decimal places.
A regression's noise has standard deviation , and you believe each weight is normal with mean 0 and standard deviation . Written as , what does MAP estimation use?
A network uses inverted dropout with keep probability . A unit's activation is 5. During training, what value does the unit pass on when it's kept?
Five independently trained models each have prediction variance at some input. What is the variance of their averaged prediction?
A network reaches 0.02 training loss but 0.35 validation loss, and its validation loss has been rising for the last 10 epochs. Which is the best diagnosis and fix?
Fit to the points by minimizing with .
The hardest one here. Get it, and you've got this lesson.
For a family of models with parameters trained on examples, suppose , , and the noise is . With examples, find the that minimizes the expected test error, and that minimum expected test error.
Where it's used
Every training pipeline fights overfitting. Weight decay is a default setting in standard optimizers (AdamW is Adam with the decay applied directly to the weights), dropout is standard in many networks, and early stopping, halting when the validation loss starts to rise, is a regularizer too. Machine learning competitions are often won by averaging many models, which is the variance formula at work. Very large networks sometimes show test error falling again past the point where they can memorize everything ("double descent"), which is still an active research question.