AI State of the Art
Fundamentals/Machine Learning: The Basics/26 min
Section 14

Regularization: L1, L2, Dropout, Early Stopping

Listen as a podcast →

Introduction: Keeping Discipline

Here's the classic deep learning trap: you train a model, your training loss drops to 0.001, you're happy... and then in production, performance is catastrophic. What happened? The model memorized the training data instead of extracting general patterns. It learned that image number 4,327 is a cat, but it didn't learn to recognize a cat in general. A network large enough can memorize anything: every photo, every email, every price, including noise and labeling errors.

Regularization adds "brakes" to the model: constraints, penalties, and perturbations that force it to find simple and generalizable solutions, not just brute memorization. A penalty added to the loss is exactly a constraint rewritten as a Lagrange multiplier, as constrained optimization explains.

It's the practical application of Occam's razor: between two models that perform equally well on training data, prefer the simpler one, it's the one most likely to work on new data.

This principle has an elegant formalization in information theory: MDL (Minimum Description Length). The idea, explored as early as 1993 by Hinton & van Camp, is that a good model is one you can describe with few bits. A model that memorizes noise needs an enormous number of bits to precisely encode all its weights. A model that captures the true underlying patterns can be described much more compactly. L1 and L2 regularization, viewed from this angle, are ways of penalizing the "description length" of the model: small weights (L2) or sparse weights (L1) require fewer bits to encode. Fewer bits = simpler model = better generalization.

A Tutorial Introduction to the Minimum Description Length Principle (Grünwald, 2004)

L2: Pushing Weights Toward Zero

L2 regularization (also called Ridge or weight decay) adds a penalty term to the loss function: the sum of squares of all model weights.

Ltotal=Lerror+λ∑iwi2\mathcal{L}_{\text{total}} = \mathcal{L}_{\text{error}} + \lambda \sum_{i} w_i^2

Symbol by symbol: Lerror\mathcal{L}_{\text{error}} is the usual loss, the one measuring prediction mistakes; wiw_i is the model's ii-th weight and ∑i\sum_{i} means "add across every weight, no exception"; the exponent 2 squares them, which makes the sum blind to a weight's sign and punishes big ones harder; λ\lambda ("lambda") is the weight given to this second demand.

In one sentence: the model must both predict well and keep its weights small, and lambda arbitrates between the two.

The parameter λ\lambda (lambda) controls the strength of the penalty. The larger lambda is, the more the model is forced to keep its weights small.

The intuition: why do small weights reduce overfitting? Think of a function that passes through noisy data. With huge weights, the function makes violent zigzags to pass through each point, it memorizes the noise. With small weights, the function is forced to be "smooth": it captures the general trend without sticking to the noise. Small weights = smooth and stable decisions.

In practice with the SGD optimizer, L2 amounts to multiplying each weight by (1−λ⋅η)(1 - \lambda \cdot \eta) at each step, before applying the gradient, where η\eta is the learning rate. Since that factor is just under 1, every weight loses a small percentage of itself at every step; hence the name weight decay. With Adam this equivalence breaks, which is exactly why AdamW exists, see Modern optimizers.

L2 is the baseline tool

L2 / weight decay is used almost everywhere in deep learning. A lambda between 0.0001 and 0.1 is typical (0.01 is a good starting point). It's your first line of defense against overfitting, simple, effective, and with no additional computational cost.

L1: Forcing Weights to Exactly Zero

L1 regularization (also called Lasso) penalizes the absolute value of the weights instead of the square:

Ltotal=Lerror+λ∑i∣wi∣\mathcal{L}_{\text{total}} = \mathcal{L}_{\text{error}} + \lambda \sum_{i} |w_i|

Same symbols as L2, one difference: the vertical bars ∣wi∣|w_i| are the absolute value, the weight counted without its sign, instead of being squared.

The big question: why does L1 drive weights to exactly zero, and L2 does not? The answer lies in the shape of the penalty's gradient.

With L2, the penalty is w2w^2: its gradient is 2w2w, which becomes nearly zero when the weight approaches zero. It's like a restoring force that weakens as you get closer to the center. Result: the weight approaches zero but never really gets there.

With L1, the penalty is ∣w∣|w|: its gradient is ±λ\pm\lambda, constant regardless of the weight's value. Even a weight at 0.0001 gets the same push toward zero as a weight at 10. It's this constant push, like a wind that always blows just as hard, that crosses the threshold and eliminates the weight completely.

Practical result: L1 produces sparse models where only a few weights are non-zero. This is extremely useful for feature selection, L1 implicitly tells you "these features matter, the others don't". In deep learning, L1 is less common than L2, but it remains important in classical ML and for linear models.

Use L1 when you suspect that only a fraction of your features is truly important and you want the model to discover this on its own.

Dropout: Deactivating Neurons

Dropout is simple in principle: during training, at each forward pass, you randomly deactivate 10 to 50% of neurons. Their outputs become zero for that step.

Comparison of neural network with and without dropout
The image illustrates the principle of dropout in a neural network. On the left, the full network is active, while on the right, some neurons are randomly deactivated during training to prevent overfitting.Source : Vitality Learning, Medium

Why It Works

Imagine a network that memorizes training data: certain neurons learn to cooperate very precisely to recognize a particular example. Neuron A activates B which activates C, together they memorize a specific pattern. With dropout, A, B, or C can be turned off at any moment. This precise cooperation becomes impossible to maintain. The network is forced to learn redundant and distributed representations: each useful feature is encoded in multiple different ways, and everything becomes more robust.

Another angle: dropout is equivalent to implicitly training a gigantic ensemble of sub-networks. With 100 neurons, you get potentially 21002^{100} possible configurations. At inference, all neurons are active, it's like averaging the votes of all these sub-networks simultaneously.

It's like training 2n2^n different sub-networks implicitly (where nn is the number of neurons).

At Inference

All neurons are active. But this needs compensating: since more neurons contribute than during training, the sum reaching the next layer would mechanically be larger. So the outputs are multiplied by (1−p)(1 - p), where pp is the fraction of neurons that had been switched off. In practice libraries do the reverse, they divide by (1−p)(1 - p) during training, which leaves inference untouched. Same arithmetic, moved.

Dropout rate by layer

Input: 0.1-0.2 (not too much on raw data) Hidden layers: 0.3-0.5 common Modern Transformers: often just 0.1

Early Stopping: Stop Before Things Go Wrong

Ridiculously simple but terribly effective. It's probably the regularization technique with the best effort-to-result ratio.

Training and validation error curves
This graph illustrates the error of a machine learning model as a function of the number of iterations, showing the value of early stopping to prevent overfitting. The validation curve goes back up after a minimum, indicating the optimal moment to stop training.

The principle: monitor the validation error at each epoch (one complete pass over the training data). As soon as it stops decreasing (or starts going back up), stop everything.

The key implementation: always save the best weights (the "checkpoint" at the best validation score). When you decide to stop, you go back to those weights, not the latest ones. It's the model from before the overfitting started.

Patience: in practice, you don't stop at the first epoch without improvement, validation can fluctuate. You tolerate 5 to 20 epochs without improvement before cutting off. This patience parameter should be adjusted based on the task.

Why it works: training goes through two distinct phases. Phase 1: training and validation errors decrease together, the model is learning true and general patterns. Phase 2: training error continues to drop but validation error plateaus or goes back up, the model is starting to memorize noise specific to the training data. Early stopping captures you right at the transition point between these two phases.

Data Augmentation: Inventing Variants

You don't really create new data, you create variants that remain valid.

Images: rotation, left-right mirroring, random zoom, crop, added grain, contrast and brightness shifts. More elaborate techniques: mixup, which manufactures an image halfway between two existing ones; cutout, which masks random squares to stop the model depending on a single detail; RandAugment and AutoAugment, which search for the most useful transformations themselves.

Examples of image augmentation for AI
This image illustrates different data augmentation techniques for training artificial intelligence models. It shows the original image and its variants modified by common transformations such as rotation, flipping, cropping, adding noise or blur.

Text: paraphrase (rephrase without changing the meaning), word substitution, back-translation to keep things natural.

Examples of text regularization methods
The image illustrates four regularization techniques for improving the robustness of natural language processing models: swapping (exchanging words), deletion (removing words), insertion (adding words), and substitution (replacing words). These methods help generate augmented data to train more performant models.

Tabular data: add slight random noise, or use SMOTE (Synthetic Minority Oversampling Technique), which manufactures new examples of the rare class by interpolating between existing ones.

Often the most effective tool

Data augmentation is often the most powerful regularization technique in computer vision. Use it.

Label Smoothing: Being Honest About Uncertainty

When you hand the model the right answer, you normally write it as a list of zeros with a single 1 in the correct class's slot: [0,0,1,0][0, 0, 1, 0] for "this is class 3". That encoding has a name, one-hot, and it asserts 100% certainty.

Label smoothing replaces the assertion with a tempered version: [0.025,0.025,0.925,0.025][0.025, 0.025, 0.925, 0.025], meaning "this is very probably class 3, but leave the others a chance". Concretely, you take a small quantity ε\varepsilon ("epsilon", typically 0.1) away from the correct class and spread it evenly over all of them.

The effect is subtle but important. To produce a 100% probability, a model has to push its raw score (its logit) toward infinity, since only at infinity does the conversion into a probability reach 1. Asking it for 92.5% instead of 100% spares it that flight, and the logits stay within sane magnitudes. The predicted probabilities are better calibrated as a result (when the model says "80% confidence", it's actually correct in ~80% of cases). A well-calibrated model is much more useful in production than a model that always says "99% sure" even when it's wrong.

Label smoothing has become standard in Transformer training (it was already used in "Attention Is All You Need").

Combined Techniques

In practice you mix them all:

TechniqueWhenCost
Weight decay (L2)AlwaysNone
DropoutDense layers, TransformersSlightly slows training
Early stoppingAlwaysNone
Data augmentationLimited dataPreprocessing cost
Label smoothingClassification, TransformersNone

Modern Architectures Regularize Implicitly

A fascinating point: many components of modern architectures have implicit regularization effects, even if that's not their main purpose.

Residual connections (also called skip connections), present in ResNets and transformers, smooth the loss landscape, making optimization more stable and less prone to overfitting. LayerNorm and BatchNorm normalize activations, preventing weights from diverging. Multi-head attention spreads information over several parallel representations, which creates redundancy as a side effect. And applying dropout inside the attention mechanism itself has become standard in transformers.

Notable fact: in very large language models, trained on thousands of billions of tokens in a single pass, classic dropout often disappears. The reason is mechanical, since each example is seen only once, the model has no material opportunity to memorize it. Corpus size regularizes on its own.

Practical Summary

Here is the baseline recipe, good for the vast majority of situations:

  1. Start with L2 weight decay (lambda = 0.01). It's free in compute and always helps
  2. Add dropout (0.1-0.5) on your dense layers, especially if the model is small relative to the data
  3. Set up early stopping with patience of 10 epochs. It's free and prevents surprises
  4. If you have little data, data augmentation is your best weapon, often more effective than all other techniques combined
  5. For Transformers, add label smoothing (ε\varepsilon = 0.1)

Regularization isn't an exact science. It's a set of complementary tools, and experience tells you which one is most critical for your specific situation. Learn to combine them intelligently and you'll master overfitting.

Why This Matters

Overfitting is the silent death of ML. Your model shows a perfect score in the lab, everyone applauds, and then in production it collapses miserably. Regularization protects you from this scenario. It's a fundamental skill, and the hallmark of a practitioner who knows what they're doing.

Check your understanding0 / 5

1. Why does L1 regularization drive some weights to exactly zero, while L2 never gets them there?

2. How do small weights reduce overfitting?

3. Why does switching neurons off at random during training (dropout) help the model generalize?

4. What do you gain by replacing the label « 100% class 3 » with « 92.5% class 3, the rest spread over the others » (label smoothing)?

5. Why does classic dropout often disappear from the training of very large language models?