Imagine a student who prepares for an exam by memorizing the exact answers to last year’s paper. On last year’s questions they score perfectly. On this year’s, which test the same ideas with different numbers, they fail. They learned the examples instead of the subject.
Models do the same thing. Doing well on the training data is easy if the model is flexible enough. What matters is GeneralizationHow well a model performs on new data it was not trained on. It is the goal of machine learning; doing well on the training data is only a means to it.Open in glossary, how well it does on data it has never seen. This lesson makes the gap between the two visible.
A model you can make more flexible
We will fit a polynomial to 15 noisy points:
The degree controls flexibility. Degree 1 is a straight line. Degree 3 can make two bends. Degree 14 has 15 coefficients, as many as there are training points, enough to come close to passing through every one. The coefficients are fit by least squares, exactly as for a line. Another 200 points from the same source are held back as a test set.
Overfitting
A polynomial is fit to 15 noisy training points. 200 more points from the same source are held back for testing.
- Training points
- Test points
- Fitted degree 3 polynomial
About right: it follows the real trend without chasing every wiggle.
Try this
- Start at degree 0 and raise it one step at a time. Watch both curves in the error chart, the solid training error and the dashed test error.
- At degree 1 the line cannot follow the curve: both errors are high. That is UnderfittingWhen a model is too simple to capture the real pattern, so it does poorly on both the training data and new data.Open in glossary.
- Around degree 3 to 5 the fit follows the real trend. Turn on Show the true curve to compare.
- Push past degree 8. The curve threads between the training points and swings wildly between them. Training error keeps falling while test error shoots up. That is OverfittingWhen a model fits the quirks and noise of its training data so closely that it does worse on new data. Training error keeps falling while test error rises.Open in glossary.
- Press New sample of data a few times at degree 12. The wild fit changes completely each time, while a degree 3 fit barely moves.
The chart tells the whole story. Training error keeps going down as the degree rises, because a more flexible model can always fit its own training points at least as well. (At the very highest degrees you may see a tiny uptick on some samples; that comes from a small safety term the demo adds to keep the arithmetic stable.) Test error falls at first, while extra flexibility captures the real shape, and then rises, once the extra flexibility is spent on noise. The best model sits at the bottom of that U, and you can only find it by measuring on data the model did not train on.
Fix one: regularization
One cure is to keep the flexible model but make wild fits expensive. Overfit polynomials need enormous coefficients that nearly cancel each other out. RegularizationAny technique that discourages a model from fitting noise, for example adding a penalty on large parameter values to the loss.Open in glossary adds a penalty for large coefficients to the loss:
The first part rewards fitting the training data. The second part, with strength (lambda), rewards small coefficients. This is called L2 regularization, or ridge regression; in neural networks the same idea is usually called weight decay.
Try regularization
- Set the degree to 14, then turn on L2 regularization. The wild swings calm down, and the test error drops sharply.
- With regularization on, sweep the degree. The test error curve flattens out: high degrees no longer hurt much.
- Raise the penalty strength to its maximum. Now the fit is too stiff and starts to underfit. The penalty is a setting you have to tune.
The penalty strength and the degree are HyperparameterA setting chosen by people rather than learned from data, such as the learning rate, the number of layers, or the strength of regularization.Open in glossary: settings you choose rather than values the model learns. Choosing them well is a large part of practical machine learning.
Fix two: more data
Fifteen points leave a lot of room for a flexible curve to wander. With thousands of points from the same source, the noise in individual points averages out, and the only way for a curve to fit all of them well is to follow the true trend. More data is the most reliable cure for overfitting, and much of the progress in modern AI has come from training on far more data.
Choosing fairly: the validation set
There is a subtle trap in what you just did. If you try every degree and keep the one with the lowest test error, the test set has influenced your choice, and its error is no longer an unbiased estimate for genuinely new data. The fix is a third split, a Validation setA second held-back set used while building a model to choose settings such as the learning rate or model size, so the test set stays untouched until the final check.Open in glossary, used for all of these choices. The test set is used once, at the very end, to report how the final model does.
Bias and varianceOptional
The two failure modes have classic names. An underfit model has high bias: its assumptions are too rigid to represent the truth, so it is wrong in the same way on any sample of data. An overfit model has high variance: it is so sensitive to the particular training points that a new sample gives a very different fit, as you saw with New sample of data at degree 12. For squared error, the expected test error splits exactly into bias squared, plus variance, plus the irreducible noise in the data. Here the noise has standard deviation 0.2, so no model can get the test error much below 0.04.
Very large neural networks complicate this classic picture. They often have far more parameters than training examples and still generalize well, and making them even larger can improve test error again past the point where they fit the training data perfectly, an effect called double descent. Why this happens is an active research area; regularization, the training procedure itself, and the structure of real data all appear to play a part.
Key ideas
- The goal is generalization: good performance on new data, not on the training data.
- Underfitting: the model is too simple, so training and test error are both high.
- Overfitting: the model is so flexible it learns the noise, so training error is low but test error is high.
- Regularization penalizes extreme parameter values; more data leaves less room to memorize noise.
- Make choices such as model size on a validation set, and keep the test set for the final check.