Regularization (Dropout, BatchNorm, Weight Decay)
Stabilize and generalize deep nets with dropout, normalization, and weight decay.
In plain words
Regularisation stops a network from just memorising its homework. It does this by randomly switching off parts of the network, nudging weights to stay small and keeping the numbers flowing through it on a steady scale.
Think of it like…
Training for a quiz with distractions. Weight decay is a rule against over-specialised notes, dropout is practising with random team members missing so everyone learns the whole material, and batch norm is re-calibrating the scoreboard every round so no single round's scale distorts the lesson.
Where you’ve seen it
A fraud network trains to a training loss of 0.12 while validation loss sits at 0.58, a clear overfit gap. The team applies three changes. First, dropout with p = 0.3 after each hidden layer: kept activations are scaled by 1 / 0.7 = 1.43 during training (inverted dropout) so the expected value is unchanged and nothing needs rescaling at inference. Second, weight decay 0.01 through AdamW, excluding biases and normalisation parameters. Third, early stopping on validation loss with patience 5. The result (illustrative) is a training loss of 0.24 and a validation loss of 0.41: the gap shrinks from 0.46 to 0.17 and validation improves. BatchNorm on a batch of four values (2, 4, 6, 8) has mean 5 and variance 5, so the normalised values are (−1.342, −0.447, 0.447, 1.342) before γ and β rescale them. During evaluation, it uses running statistics instead of the batch: with momentum 0.1 a running mean of 4.0 and a batch mean of 5 updates to 0.9 × 4.0 + 0.1 × 5 = 4.1. A bug where the evaluation function forgot model.eval() produced noisy validation loss because dropout was still active; adding the call fixed it.
Words you’ll hear
- Inverted dropout
- Scale kept activations by 1/(1 − p) at training time so inference needs no change.
- Weight decay
- Shrinking weights towards zero each step, equivalent to L2 in plain SGD.
- Batch normalisation
- Standardising features over the minibatch, then applying learned scale and shift.
- Early stopping
- Halting training when validation loss stops improving.
Visual diagram
Theory & deep dive
Dropout and weight decay
Dropout with rate p multiplies each activation by a Bernoulli mask and, in the inverted form used by frameworks, divides the kept values by 1 − p. For p = 0.5 an activation vector (1, 2, 3, 4) with mask (1, 0, 1, 0) becomes (2, 0, 6, 0); individual outputs are noisy but the expected value of each unit is unchanged. This forces redundancy, since no unit can count on another being present, and acts like averaging many thinned networks. Typical rates are 0.1 to 0.5 for dense layers (lower for the input and for transformers, whose attention and feed-forward dropout is often 0.1). Too much dropout underfits; check that the training loss can still fall.
Weight decay adds λw to each gradient (L2) or shrinks weights directly (AdamW). In plain SGD with lr = 0.1 and λ = 0.01 every step multiplies a weight by 1 − 0.001, so 1,000 steps of pure decay leave 0.999¹⁰⁰⁰ ≈ 0.368 of its size; the data gradient pushes back, and the equilibrium is a smaller-norm solution. Typical values are 1e-4 to 1e-2 for SGD and 0.01 to 0.1 for AdamW. Apply it to weight matrices but not to biases or the γ and β of normalisation layers.
p = 0.5, activations (1, 2, 3, 4), mask (1, 0, 1, 0), scale 1 / (1 − 0.5) = 2 → output (2, 0, 6, 0). Over many random masks each unit's expected value stays 1, 2, 3 and 4. At inference no mask and no scaling are applied.
Normalisation, early stopping and practical order
Batch normalisation computes the batch mean μ and variance σ² for each feature and outputs γ(x − μ)/√(σ² + ε) + β. For the batch (2, 4, 6, 8): μ = 5, σ² = (9 + 1 + 1 + 9) / 4 = 5 and the normalised values are (−3, −1, 1, 3) / 2.236 = (−1.342, −0.447, 0.447, 1.342). During training it also keeps exponential running averages (running = 0.9 × running + 0.1 × batch), which are used at evaluation, so evaluation output does not depend on the other items in a batch. BatchNorm needs reasonably large batches (a batch of 2 gives noisy statistics); LayerNorm normalises across features within one example and is the standard in transformers and with small batches.
Early stopping monitors validation loss and restores the best checkpoint after patience epochs without improvement. Data augmentation (random crops and flips for images, noise or mixup for others) is often the most effective regulariser when data is scarce. A common block order is Linear → BatchNorm → ReLU → Dropout, and some practitioners avoid combining dropout directly before BatchNorm because the variance shift can hurt. Diagnose before regularising: a small train–validation gap with poor performance is underfitting, which more regularisation will worsen.
Batch (2, 4, 6, 8): μ = 5, σ² = 5, σ = 2.236. Normalised: −1.342, −0.447, 0.447, 1.342. With γ = 2 and β = 1 the outputs are −1.684, 0.106, 1.894 and 3.684. Running mean update: 0.9 × 4.0 + 0.1 × 5 = 4.1.
How it works, step by step
Step 1 of 41. Diagnose the gap
Plot training and validation curves; a large gap signals overfitting, a high loss on both signals underfitting.
- Compare
- both curves
When to use it & trade-offs
What it is
Regularisation reduces the gap between training and validation performance in neural networks. Weight decay (L2) pulls every weight gently towards zero, which favours smoother functions. Dropout randomly zeroes a fraction p of activations during training, so no neuron can rely on a specific partner and the network behaves like an ensemble of thinned sub-networks; at inference all units are active. Batch normalisation standardises each feature across the minibatch (then rescales with learned γ and β), which stabilises and speeds up training and adds a mild regularising noise because batch statistics vary. Related tools are early stopping, data augmentation, label smoothing and simply a smaller model. Each one trades a little training fit for better generalisation.
Why it matters: Deep networks have enough capacity to memorise a training set, so a falling training loss alone is meaningless. A model that reaches 0.12 training loss but 0.58 validation loss has learned noise. The right regulariser depends on the failure: dropout and weight decay for fully connected layers, batch or layer normalisation for optimisation stability, augmentation when data is scarce. Misusing them is common: forgetting to switch to evaluation mode leaves dropout active at inference, batch norm with tiny batches is unstable and applying weight decay to biases and normalisation parameters can hurt.
When to reach for it
- Dropout and weight decay on overfitting MLPs
- BatchNorm in CNNs, LayerNorm in transformers
- Early stopping on every run
When to avoid or be careful
- Evaluating without switching to eval mode
- Weight decay on biases and normalisation parameters
- Adding regularisation to a model that is underfitting
Regularisation tools
Strengths: Simple; ensemble-like effect.
Watch out: Slows training; too much underfits.
Strengths: Cheap; improves generalisation.
Watch out: Needs tuning; skip biases and norms.
Strengths: Faster, more stable training.
Watch out: Depends on batch size; train and eval differ.
Strengths: Adds effective data.
Watch out: Must preserve labels.
Interview signals & numbers to know
When this topic comes up
- “How does dropout work at inference?”
- BatchNorm train versus eval behaviour
- Weight decay versus L2
- Large train–validation gap
- LayerNorm versus BatchNorm
Benchmarks & rules of thumb
- 1 / (1 − p)
- scale applied to kept activations in inverted dropout
- 0.999¹⁰⁰⁰ ≈ 0.368
- decay of a weight after 1,000 steps at lr 0.1, λ 0.01
- 0.9 / 0.1
- default BatchNorm running-average weights (momentum 0.1)
Common mistakes & follow-ups
Common interview mistakes
- Leaving the model in training mode during validation.
- Using BatchNorm with a batch size of one or two.
- Stacking every regulariser at maximum strength at once.
Likely interviewer follow-ups
- Why scale activations in dropout during training?
- What are the running statistics in BatchNorm for?
- How do you choose a weight decay value?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Regularization (Dropout, BatchNorm, Weight Decay) with data science interview Q&A and hands-on practice.