Overfitting and Underfitting Explained
Imagine two students preparing for a math quiz. One memorizes the exact answers to last year's quiz. The other learns how to solve the problems. On last year's questions they both get a perfect score. On this year's quiz, with new questions, only one of them does well.
Machine learning models can be either student. A model that memorizes is overfitting. A model that has not learned enough is underfitting. Telling them apart is one of the most important skills in machine learning, and you only need two numbers to do it.
Two scores: training and test
A model learns from the training set. To find out whether it has really learned, you score it on examples it has never seen. During development that is the validation set; at the very end, it is the test set. (The guide to the train, validation and test split explains how to make them, and the Dataset Size Calculator turns a row count and two shares into training, validation and test counts.)
Compare the score on training data with the score on unseen data:
- Training high, unseen low: overfitting. The model memorized its examples, including their accidents and noise, and does not work on new ones.
- Both low: underfitting. The model is too simple, or has not trained enough, to capture the pattern even in the data it learned from.
- Both reasonably high and close together: a good fit. It learned something that carries over to new data.
The scikit-learn user guide puts it the same way: "If the training score and the validation score are both low, the estimator will be underfitting. If the training score is high and the validation score is low, the estimator is overfitting" (scikit-learn: validation and learning curves).
An illustration
Here are three made-up models trained on the same made-up dataset. The numbers are invented to show the pattern, not taken from any real project.
| Model | Training accuracy | Test accuracy | Diagnosis |
|---|---|---|---|
| A: very simple | 0.62 | 0.60 | Underfitting: low on both |
| B: very large, trained a long time | 0.99 | 0.71 | Overfitting: big gap |
| C: in between | 0.88 | 0.85 | Good fit: high and close |
Notice that Model B has the highest training score and still loses. A high training score on its own tells you almost nothing. The score on unseen data is the one that counts.
Why models overfit
A model has adjustable numbers, called parameters, that it sets during training. The more parameters it has compared with the number of examples, the easier it is to fit every training example exactly, including the quirks that will never appear again.
The Neural Network Calculator counts them for fully connected networks. A network with layers 4, 8 and 3 has 67 parameters. One with layers 784, 128, 64 and 10 has 109,386. Give the second one only a few hundred training examples and it has far more than enough room to memorize them. The guide to neural networks shows how those counts are worked out.
Overfitting also creeps in with time. Early in training the model picks up the main pattern; train it far too long on the same small set and it starts fitting the noise.
Validation for choosing, test for the final answer
You will try different models, sizes and settings. Use the validation set to compare them and choose. Look at it as often as you like.
Keep the test set locked away and look at it once, at the end, after every choice is made. Why so strict? Every time you pick something because it scored well on a set, your choices have adapted to that set a little. If you kept checking the test set too, it would stop being "unseen", and your final number would be too optimistic. It is the same as peeking at the quiz before taking it.
Data leakage: when the test is not really unseen
Data leakage is when information from the test set, or from the answer itself, sneaks into training. It makes a model look far better than it is. Common forms:
- The same example in both sets. The same photo, or three photos of the same leaf, appear in training and test. The model recognizes them instead of generalizing.
- A feature that gives the answer away. Suppose you want to predict whether a plant will survive the winter, and one column is "date removed from the garden". Plants that died were removed, so that column is basically the label in disguise. The model scores brilliantly in testing and is useless in real life, where you do not know that date in advance.
- Preparing data using all of it. If you calculate an average for filling in missing values from the whole dataset, including the test rows, a little of the test set has leaked into training. Work those numbers out from the training set only.
A warning sign: a test score that seems too good to be true. Before celebrating, look for leakage.
How to fix overfitting
- More varied data. More examples, covering more kinds of cases, give the model less room to memorize and more pattern to learn. Correctly labeled variety helps more than near-copies.
- A simpler model. Fewer parameters: fewer or smaller layers, or a shallower decision tree. Try the simple version first; only add size if the validation score improves.
- Stop earlier. Score the validation set after each epoch and stop when it stops improving. This is called early stopping, explained in epochs, batches and training time.
- Regularization. A family of techniques that discourage a model from relying too heavily on any one detail. The idea is simply "do not let the model get too complicated".
How to fix underfitting
- Better features. Give the model information that actually relates to the answer. No model can predict leaf type from the color of the paper you wrote on.
- A more flexible model. A straight line cannot fit a curved pattern; something with more parameters might.
- Train longer, if both scores are still rising.
Check yourself
For each case, decide: overfitting, underfitting or good fit?
- Training accuracy 0.97, validation accuracy 0.96.
- Training accuracy 0.55, validation accuracy 0.53, on a two-class problem where half the examples are each class.
- Training accuracy 1.00, validation accuracy 0.64.
(Answers: 1 is a good fit, though check for leakage if it seems too easy. 2 is underfitting, barely better than guessing. 3 is overfitting.)
Accuracy is not the only score you can use, and on its own it can mislead badly when one class is rare. The guide to accuracy, precision and recall explains why.