Regularization & Generalization
Networks that can memorize anything yet predict well: regularizers and the generalization puzzle.
A modern image classifier has enough weights to memorize every training image paired with a random label, and training will happily do it. Yet the same network trained on the real labels predicts well on images it has never seen. This topic is about the tools that keep success on the training data from being an illusion, and about the puzzle of why deep networks generalize at all.
What you’ll learn
- Diagnose overfitting by comparing training, validation and test error, and say what each data split is for.
- Explain in plain terms what weight decay, dropout, data augmentation and early stopping do to a network.
- Describe the random-labels experiment and the double descent curve, and why both strain the classical picture of overfitting.
- Distinguish generalization to new data from the same source from robustness to adversarial perturbations and to distribution shift.
Fitting is not predicting
Training makes a network's loss small on its training set. But nobody needs predictions for examples whose answers are already known: what matters is how the network does on new examples from the same source. That is generalization, and the difference between the error on new data and the error on the training data is the generalization gap.
Two things can go wrong. A model too simple to capture the pattern does badly everywhere: underfitting. A model flexible enough to also fit the accidents of its particular sample (noise in the labels, coincidences among a few points) does well on the training data and worse elsewhere: overfitting. Try both with a curve-fitting model below.
A network reaches 2% error on its training set and 25% on its validation set. Both numbers were measured after training finished. What is the most likely diagnosis?
- Underfitting: the network is too small to learn the task
- Overfitting: it has fitted details of the training set that do not carry over
- The validation set must be mislabeled, since the network clearly learned the task
- The learning rate was too small for training to converge
Show the answer
B. Overfitting: it has fitted details of the training set that do not carry over
A low training error together with a much higher validation error is a large generalization gap, the signature of overfitting. The remedies are more data, regularization, or a less flexible model. Underfitting would show up as high error on both sets.
Networks can memorize anything
The classical rule of thumb says a model with more parameters than training examples will overfit. Deep networks break it routinely: AlexNet had about 60 million parameters and was trained on about 1.2 million images (Krizhevsky, Sutskever & Hinton, 2012), and networks since then have grown far larger.
In 2017, Zhang, Bengio, Hardt, Recht and Vinyals tested how much such networks can memorize. They replaced every label of the CIFAR-10 training images with a random label, destroying any relation between image and class. Standard convolutional networks, trained with unchanged hyperparameters, still drove the training error to zero, and training took only a small constant factor longer than with the true labels. They even fitted images whose pixels were replaced by random noise.
A network reaches 100% training accuracy on the 50,000 CIFAR-10 training images whose labels were drawn uniformly at random from the 10 classes. What test accuracy do you expect on the real test set, in percent?
Show the answer
10 %
About 10%, chance level. The random labels carry no information about the images, so whatever the network memorized cannot help on new images: Zhang et al. saw the test error approach 90% as the fraction of random labels approached 100%. The network learned a lookup table, perfect on the training set and useless elsewhere.
The same architectures, trained on the true labels, generalize well. And switching off the explicit regularizers (weight decay, dropout, data augmentation) cost some accuracy but did not destroy generalization; Zhang et al. concluded that explicit regularization may improve generalization but is neither necessary nor by itself sufficient to control it.
So the number of parameters cannot be the explanation. The same network memorizes when the data have no structure and generalizes when they do. Whatever makes the difference lives in the data and in which of the many perfect fits training finds.
Think first If a network can fit random labels, are there weight settings that fit the real training set perfectly and still do badly on test data? Does training ever find them?
Yes, they exist, and training can be steered into them. Liu, Papailiopoulos and Achlioptas (2020) first trained networks on randomly labeled copies of the data, then on the correct labels: plain SGD ended at solutions that fit the training set yet generalized poorly. With explicit regularization (weight decay, data augmentation) the random-label pretraining did no harm. So the training procedure, not the architecture alone, determines which perfect fit you get, and regularizers can matter more than the 2017 experiment suggested.
The regularizer toolbox
Regularization means anything that makes training prefer some fits over others for reasons other than the training loss: usually smaller, smoother or more stable ones. Four tools appear in nearly every deep-learning recipe:
| Tool | What it does during training | The intuition |
|---|---|---|
| Weight decay | Shrinks every weight by a tiny fraction at each step | Large weights must keep earning their size; smaller weights tend to mean smoother functions |
| Dropout | Sets a random subset of units to zero at each step | No unit can rely on one particular partner; the network behaves like an average of many thinned networks |
| Data augmentation | Trains on randomly transformed copies: crops, flips, color shifts | Teaches the network that the label does not depend on those changes |
| Early stopping | Watches the validation error and keeps the weights from its best point | Networks fit broad patterns before they fit noise, so stopping in time keeps the patterns |
Data augmentation is the easiest to see. Each random transformation of a training image is a new example with the same label, so one image becomes many. The catch: the transformation must not change the answer.
You train a handwritten-digit classifier. Which augmentation is most likely to hurt?
- Shifting each image by one or two pixels
- Slight changes of stroke thickness and brightness
- Small elastic distortions of the strokes
- Random rotations by up to 180 degrees
Show the answer
D. Random rotations by up to 180 degrees
Large rotations turn some digits into others (6 into 9) or into ambiguous shapes, so the augmented example carries the wrong label. Small shifts, thickness changes and gentle distortions are things real handwriting does, which is exactly what an augmentation should mimic.
Early stopping needs no change to the model at all. As training goes on, the training loss keeps falling, while the validation loss usually falls, levels off and starts to rise once the network begins fitting noise. Keep the weights from the validation minimum, and allow some patience, a number of epochs without improvement, before giving up, because validation curves are noisy and can dip again.
Double descent
The classical picture says test error against model size is U-shaped: too small underfits, too large overfits, and the best size is in between. Belkin, Hsu, Ma and Mandal (2019) showed that across many model families the curve continues past the U. Test error peaks near the interpolation threshold, the size at which the model can just barely fit every training point exactly, and then falls again as the model keeps growing. This is double descent.
Nakkiran et al. (2020) found it in deep networks, not only as a function of model size but also of training time (epoch-wise double descent) and of the amount of data: near the threshold, adding training examples can make a model worse.
Think first Why does test error spike exactly where the number of features equals the number of training points, and why does it fall again beyond that point?
With there is (generically) exactly one way to pass through all noisy points, and hitting every bit of noise exactly needs enormous coefficients, so the curve swings wildly between the points. With there are infinitely many exact fits, and least squares picks the one with the smallest coefficients, which spreads the noise thinly over many features and stays smooth. More parameters bought more freedom to be gentle. A ridge penalty does the same job at any size, which is why it flattens the peak.
Robustness is a different question
Generalization, as measured by a test set, is about new examples drawn the same way as the training data. Two failures fall outside it. The first: Szegedy et al. (2014) found that tiny, deliberately chosen changes to an image, invisible to people, can flip a network's prediction. These adversarial examples often fool other networks too, even ones trained on different data.
The second failure is distribution shift: the data the model meets in use differ from its training data. Even a careful replication of a benchmark counts. Recht, Roelofs, Schmidt and Shankar (2019) collected new test sets for CIFAR-10 and ImageNet following the original procedures as closely as they could; accuracy dropped by 3 to 15 percentage points on CIFAR-10 and by 11 to 14 on ImageNet. Models that ranked higher on the old test set still tended to rank higher on the new one, but none was immune.
A classifier scores 95% on a held-out test set drawn from the same source as its training data. Which conclusion is justified?
- It will be about 95% accurate on new images drawn the same way, but this says little about attacked or shifted inputs
- It will be about 95% accurate on any images of the same classes
- It is robust to small perturbations, since it generalizes well
- Its training accuracy must also be about 95%
Show the answer
A. It will be about 95% accurate on new images drawn the same way, but this says little about attacked or shifted inputs
A test set estimates accuracy on the distribution it was drawn from. Adversarial inputs are chosen to be worst-case, and shifted data come from a different distribution, so neither is covered by the estimate.
Where this topic goes
The Representation lessons open up each piece:
- Weight Decay, Dropout & Early Stopping: why weight decay is an penalty for SGD but not inside Adam, how inverted dropout and its ensemble view work, and why stopping early acts like a penalty on the weights.
- Data Augmentation: label-preserving transformations for images, text and speech, automated policies, mixup and CutMix, and when augmentation backfires.
- The Generalization Puzzle: random labels and double descent in detail, and the candidate explanations for why huge networks generalize.
- Robustness & Distribution Shift: how adversarial examples are built and why they exist, corruptions and natural shifts, and models that latch onto shortcuts.
The Emergence lessons then go to the research frontier: dropout as an adaptive penalty, what weight decay does to the effective learning rate, a theory of augmentation, model collapse from synthetic data, generalization bounds that are not vacuous, grokking, memorization of rare examples, adversarial training and certified robustness.
Key takeaways
- Generalization is performance on new data from the same source. A large gap between training and validation error signals overfitting; validation data make decisions, test data give the final estimate.
- Networks with far more parameters than examples can memorize even random labels (Zhang et al., 2017), so parameter counts alone cannot explain why they generalize on real data.
- Regularizers bias training toward simpler fits: weight decay keeps weights small, dropout makes units robust to missing partners, augmentation teaches invariances, early stopping halts before the noise is fitted.
- Test error against model size can show double descent: a peak at the interpolation threshold and a second descent beyond it.
- A good test score says nothing about worst-case (adversarial) inputs or shifted data; robustness is a separate question.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
You are choosing the dropout rate for a network. On which data should you compare the candidate rates?
- The test set, because it gives the most honest estimate
- The training set, because that is where the network learns
- A validation set held out from training
- Whichever split gives the highest accuracy
Show the answer
C. A validation set held out from training
Hyperparameters are chosen on a validation set. The test set must stay untouched until the end; if it is used to make choices, its error becomes an optimistic estimate.
What did the random-labels experiment of Zhang et al. (2017) show?
- Networks cannot fit labels that have no relation to the images
- Weight decay and dropout prevent networks from fitting random labels
- Random labels are learned exactly as fast as real labels
- Standard networks have enough capacity to memorize even random labels, so capacity alone cannot explain why they generalize on real data
Show the answer
D. Standard networks have enough capacity to memorize even random labels, so capacity alone cannot explain why they generalize on real data
The networks reached zero training error on random labels, and explicit regularizers did not stop them. Since the same networks generalize on real labels, any explanation must involve the data and the training procedure, not just the number of parameters.
In the double descent curve, where does the test error peak, and why?
- Near the interpolation threshold, where the model can just barely fit every training point and needs wild coefficients to hit the noise
- At the smallest models, which cannot fit the training data
- At the largest models, which have the most parameters to overfit with
- It never peaks; test error always falls as models grow
Show the answer
A. Near the interpolation threshold, where the model can just barely fit every training point and needs wild coefficients to hit the noise
At the threshold there is essentially one exact fit, and it must contort itself through every noisy point. Larger models have many exact fits, and training picks a smoother one, so test error falls again.
During training, the validation loss is lowest at epoch 12 and rises steadily afterward, while the training loss keeps falling until epoch 50, when you stop. Which weights should you keep?
- The weights from epoch 50, since they have the lowest training loss
- The weights from epoch 12, the validation minimum
- The average of the weights from all 50 epochs
- Whichever epoch scores best on the test set
Show the answer
B. The weights from epoch 12, the validation minimum
That is early stopping: the rising validation loss after epoch 12 says the extra fitting does not carry over to new data. Report the test error of the epoch-12 weights once, at the end.
An attack flips a classifier's prediction with a perturbation of size per pixel, but random noise of the same size almost never does. Why?
- The attack changes many more pixels than the random noise
- Random noise is filtered out by the classifier's first layer
- The attack pushes every pixel in the direction that hurts the score, so the effects add up; random signs mostly cancel
- The classifier was trained on random noise, so it is robust to it
Show the answer
C. The attack pushes every pixel in the direction that hurts the score, so the effects add up; random signs mostly cancel
Both perturbations have the same size per pixel. For a linear score , the attack's change grows with the number of pixels, while random signs give a sum of terms with random signs that largely cancel.
Go deeper
- Weight Decay, Dropout & Early Stopping · The classic regularizers, and what each one actually does to a deep network.
- Data Augmentation · Teaching invariances by transforming data: crops, flips, color, mixup and more.
- The Generalization Puzzle · Random labels, double descent and why classical theory struggles with deep networks.
- Robustness & Distribution Shift · Adversarial examples, shortcuts and models that fail outside their training data.
Further reading
- C. Zhang, S. Bengio, M. Hardt, B. Recht & O. Vinyals, "Understanding deep learning requires rethinking generalization," ICLR (2017).
- M. Belkin, D. Hsu, S. Ma & S. Mandal, "Reconciling modern machine-learning practice and the classical bias–variance trade-off," PNAS 116(32), 15849–15854 (2019).
- I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, ch. 7 "Regularization for Deep Learning" (MIT Press, 2016), free at deeplearningbook.org.