Overfitting, Underfitting, and Regularization
Recognizing when a model has memorized the training data versus learned generalizable patterns. Dropout, weight decay, early stopping, and cross-validation as defenses.
A student can prepare for an exam by understanding the material or by memorizing last year's answers. On last year's paper the two students score the same. On this year's, they don't.
A model can do either as well. Overfitting is the memorizing: the model fits the particular cases in its training set, noise and quirks included, and does worse on new ones. Underfitting is the opposite problem, where the model hasn't learned even what the training data could teach it. Most of the day-to-day work of training a model is steering between the two, and the tools for discouraging memorization go by the name of regularization.
You've met this before if you've ever fitted a regression with too many predictors for the number of events. The model looks wonderful on the derivation cohort and shrinks on validation. Deep learning has the same problem with a few million more parameters.
Why deep networks overfit so readily
A modern network has far more adjustable parameters than a clinical dataset has cases. That gives it the capacity to store the training set outright. In 2017 Chiyuan Zhang and colleagues demonstrated this in a memorable way: they took a standard image dataset, replaced the labels with random ones, and trained standard networks on it. The networks reached essentially perfect accuracy on the training set. There was nothing to learn, since the labels were random, so the networks had simply memorized a million arbitrary answers.
So a network's ability to fit its training data tells us nothing by itself. What we care about is whether what it learned carries over to new cases, which is called generalization. Small datasets make overfitting more likely, and medical datasets are usually small.
Reading the curves
The standard way to see what is happening is a pair of learning curves. During training, the engineers record the model's error on the training set and on the validation set after each epoch (each pass through the training data) and plot both against time.
There are three typical pictures.
When both curves fall together and level off close to each other, the model is learning something general. This is what we hope to see.
When both curves level off at a high error, the model is underfitting. It can't even fit the cases it is trained on.
When the training error keeps falling while the validation error levels off and then starts to rise, the model is overfitting. From the point where the curves part, additional training is improving the model's recall of the training set and making it worse on everything else.
The distance between the two curves is called the generalization gap. Some gap is normal, since a model nearly always does a bit better on cases it has seen. A wide and growing gap is the thing to watch for.
It's a good habit to ask to see these curves. They are the closest thing a training run has to a vital signs chart, and an engineer can explain a lot about a project by pointing at them. (These curves, which plot error against training time, are different from the learning curve in 2.2 How Much Data Do You Need?, which plots performance against dataset size. The names are unhelpfully similar.)
Underfitting
Underfitting is the less common problem in deep learning, and the easier one to recognize, because performance is poor everywhere, training set included.
The usual causes are a model that is too small for the task, training that was stopped too early or with a badly chosen learning rate, regularization turned up so high the model can't learn, or inputs that don't contain the information. That last one deserves a thought before anyone reaches for a bigger model. If the image has been shrunk until the finding is invisible (see 2.5 Data Preprocessing and Augmentation), or the labels are close to random, or the outcome simply isn't predictable from this input, no model will fit it. Sometimes the finding of a study is that the signal isn't there, and that can be worth knowing.
The bias-variance tradeoff
Statisticians describe the same two failures in terms of bias and variance. A model that is too simple has high bias: it gets things wrong in the same way no matter which sample it was trained on. A model that is too flexible has high variance: what it learns depends heavily on the particular sample, so a different draw of patients would give a noticeably different model.
The classical advice is to pick a model complexity in the middle. Deep learning has complicated that picture a little. Very large networks, trained with the right safeguards, often generalize better than medium-sized ones, an observation that goes by the name of double descent and that researchers are still working out. For our purposes it doesn't change the usual approach, which is to use a network with plenty of capacity and rely on regularization and validation data to keep it from memorizing.
Regularization
Regularization means any technique that makes it harder for the model to memorize, usually at a small cost in how well it fits the training set. Here are the common ones.
More data
It isn't usually listed as a regularizer, but more training data is the most effective defense there is. A set too large to memorize has to be learned. More varied data, from more sites and scanners, helps even more, because it also guards against the model learning one site's quirks (see 2.6 Multi-Site and Multi-Device Data).
Augmentation
If we can't have more data, we can vary the data we have. Random rotations, shifts and intensity changes mean the model never sees exactly the same image twice, which makes memorizing individual images much harder. 2.5 Data Preprocessing and Augmentation covers what is and isn't safe to do to a medical image.
Starting from a pretrained model
A network that already has good general features needs less from your data and has less room to go wrong (see 3.3 Transfer Learning).
Weight decay
Weight decay adds a penalty for large parameter values, which pushes the model toward simpler, smoother solutions. It is the same idea as ridge regression, which you may know as L2 penalization or shrinkage.
Dropout
Dropout, introduced by Nitish Srivastava, Geoffrey Hinton and colleagues in 2014, randomly switches off a fraction of the network's units at each training step. No unit can rely on any other particular unit being present, so the network is pushed to spread what it knows across many units and can't build elaborate special-case detectors for individual training cases. At prediction time all the units are switched back on.
Early stopping
Early stopping is the simplest of the lot. Watch the validation curve, and stop training when it stops improving. In practice the engineers save a copy of the model each time the validation score reaches a new best, and keep the best copy. Since the stopping point is chosen using the validation set, this is one more reason the validation score ends up a bit optimistic and the final number has to come from the test set.
A smaller model
Reducing the number of parameters limits what can be memorized. This is used less than you might expect, for the reason given under bias and variance: a large, well-regularized model usually beats a small one. It is still the right answer for very small datasets, and it is a reason to consider classical methods when you have a few hundred patients and a table of variables.
Ensembles
Training several models and averaging their predictions reduces variance, since their individual quirks partly cancel. It also tends to improve calibration (see 4.4 Calibration).
How much of each to apply is a hyperparameter question, settled on the validation set (see 3.4 Hyperparameter Tuning and Model Selection). Too much regularization produces underfitting, so the engineers are looking for the amount that brings the validation error down without dragging the training error up to meet it.
Overfitting you can't see on the curves
The curves compare training data with validation data from the same source. There are kinds of overfitting they won't show.
A model can overfit to the validation set. This happens through repeated tuning: with enough rounds of trying things and keeping what scores best, the choices come to fit the validation cases specifically. The validation curve looks fine, because the validation set is the thing being fitted. The test set reveals it.
A model can overfit to the site. If all the data came from one hospital, the model may have learned that hospital's scanners, protocols and patient mix. Training, validation and test data all share those features, so every internal number looks healthy. This is the shortcut learning and distribution shift described in 1.6 What AI Cannot Do, and only data from somewhere else will show it (see 5.4 Multi-Site External Validation).
And a whole research community can overfit to a public benchmark, when hundreds of groups tune their methods against the same public test set for years. A method's rank on a well-worn benchmark is weaker evidence than it appears.
What a clinician can check
Most of this is the engineers' territory, but a few checks need no technical knowledge.
- Ask for training, validation and test performance side by side. A model that scores 0.99 on training data and 0.82 on validation data has memorized a good deal.
- Ask to see the learning curves and where training was stopped.
- Compare internal test performance with any external result. A large fall suggests the model fitted its home site.
- Consider the size of the dataset against the size of the claim. A very high score from a few hundred patients at one site deserves a second look, whoever's paper it is.
Key takeaways
- Overfitting is memorizing the training cases. Underfitting is failing to learn what they could teach. Both show up as poor performance on new data.
- Deep networks have enough capacity to memorize a training set outright, so training performance alone means nothing.
- Learning curves show which problem you have: a widening gap between training and validation error is overfitting, and high error on both is underfitting.
- Regularization discourages memorization. The main tools are more data, augmentation, pretraining, weight decay, dropout, early stopping and ensembles.
- Underfitting can mean the model is too small or badly trained, or that the inputs don't contain the signal.
- Models can also overfit to the validation set through repeated tuning and to a single site's characteristics. The test set and external data are what reveal those.
What to read next
- 3.2 Training, Validation, and Test Sets: Why You Need All Three: the data that makes overfitting visible
- 3.4 Hyperparameter Tuning and Model Selection: choosing how much regularization to apply
- 2.5 Data Preprocessing and Augmentation: the most widely used regularizer in imaging
- 4.6 Cross-Validation Strategies for Medical Data: steadier estimates from small datasets
- 5.4 Multi-Site External Validation: finding the overfitting that internal data hides
This article is part of the AI in Clinical Research Knowledge Base.