1 Why deep nets overfit
- Overfitting: train loss β 0 while val loss diverges upward. The network memorizes training data (incl. noise) instead of learning patterns.
- Params β« data β enough capacity to memorize anything. ResNet-18 = 11M params on CIFAR-10 (50K) = 220 params per sample.
- Zhang et al. 2017: a deep net hit zero training loss on randomly shuffled labels β it can fit pure noise. Overfitting is the default behavior; regularization constrains it to learn patterns.
- Key diagnostic: the gap between train and val loss. Earliest sign of overfitting = val loss stops decreasing while train loss keeps falling. The minimum val loss = the "sweet spot".
2 L2 regularization / weight decay
Penalize large weights β add a squared-weight penalty to the loss:
L_total = L_original + Ξ» Β· Ξ£α΅’ wα΅’Β²
Ξ»controls penalty strength, typically 10β»β΄ to 10β»Β². Small Ξ» = mild; large Ξ» = aggressive (risk underfitting).- Same idea as Ridge regression from ML β now applied to network weights.
- Intuition (budget constraint): without L2 a few weights blow up β network "bets heavily" on specific features β memorizes. With L2 all weights stay moderate, many features used β robust, smoother decision boundary.
Weight decay in SGD β the penalty turns into a shrink factor:
Standard SGD: w β w β Ξ· Β· βL
With decay: w β (1 β Ξ·Ξ») Β· w β Ξ· Β· βL
ββ decay factor ββ
- Each step weights are multiplied by a number slightly < 1 β they literally decay toward zero (pull the weight vector toward the origin). Hence "weight decay".
- PyTorch:
optim.SGD(params, lr=0.01, weight_decay=0.01).
optim.AdamW(params, lr=0.001, weight_decay=0.01).
3 Dropout
- Dropout: during each training forward pass, randomly zero each neuron's output with probability
p. A different random mask every pass. - Why it works β prevents co-adaptation: neurons can't rely on specific others being present, so each feature must be independently useful. (Team-project analogy: if random members skip every meeting, everyone must understand the whole task β no single point of failure.)
- Dropout = implicit ensemble: each mask is a different sub-network. With
nneurons and p=0.5 there are 2βΏ possible sub-networks trained simultaneously; inference with all neurons β averaging all of them (same principle as Random Forests, but free inside one net).
Training vs inference (inverted dropout):
| Training mode | Inference mode | |
|---|---|---|
| Neurons | drop with prob p | use all neurons |
| Scaling | scale survivors by 1/(1βp) | none needed |
| Output | different mask each pass | deterministic |
- Why scale by 1/(1βp)? If p=0.5 half the neurons are zeroed β output magnitude halves; multiply survivors by 2 so expected output stays the same. (Scaling done at train time = "inverted dropout".)
- Common bug: forgetting
model.eval()at test time β dropout stays active β noisy, non-deterministic predictions.
Practical rates: hidden layers p=0.5 (default); input layer p=0.2 (don't drop too much input); after conv p=0.25 or skip; usually don't combine with BatchNorm. ResNets use dropout sparingly (BN handles regularization); Transformers apply it on attention weights + residuals. Too-high p β underfitting.
4 Early stopping
- Monitor validation loss; stop when val loss starts rising (and keep the best checkpoint). Stops training at the sweet spot before overfitting kicks in.
- Essentially free; the simplest regularizer. Use "patience" (wait N epochs of no improvement) to avoid stopping on noise. PyTorch Lightning has an
EarlyStoppingcallback.
5 Data augmentation
- Generate free synthetic training data by applying label-preserving transforms β effectively more data, less memorization.
- Image transforms:
RandomHorizontalFlip,RandomCrop(32, padding=4),ColorJitter(viatorchvision.transforms/ Albumentations). - Often the single most effective technique on image tasks β listed first in the recommended fix order.
6 Batch normalization
- BatchNorm: normalize each layer's pre-activations over the mini-batch (zero mean, unit variance), then rescale/shift with learnable
Ξ³, Ξ². - Speeds and stabilizes training; the per-batch noise has a mild regularizing effect β less need for dropout (why ResNets lean on BN instead).
- Like dropout, it behaves differently in
train()vseval()(uses running statistics at inference) β always switch modes correctly.
7 The toolkit at a glance
99% train / 72% val? Apply in this order:
- Data augmentation (flips + crops)
- Weight decay (AdamW, Ξ»=10β»β΄)
- Early stopping
- Dropout (p=0.3β0.5)
Still overfitting? β get more data or use a smaller model. Combining techniques usually beats any single one.
β Likely exam questions
Q1. Why can an 11M-param net perfectly fit 50K randomly labeled images?
~220 params per sample β enough degrees of freedom to memorize any arbitrary mapping, including random labels (Zhang et al. 2017).
Q2. Write the SGD update with weight decay; what does the decay term do geometrically?
w β (1 β Ξ·Ξ»)Β·w β Ξ·Β·βL. The (1 β Ξ·Ξ») factor shrinks weights toward zero each step, pulling the weight vector closer to the origin.
Q3. At test time should dropout be active? What if you forget model.eval()?
No. If you forget model.eval(), dropout stays active at inference β each forward pass gives a different, noisy output.
Q4. Why is dropout an ensemble method? How many sub-networks does it approximate?
Each dropout mask = a different sub-network. With n neurons and p=0.5 β 2βΏ sub-networks trained simultaneously; inference with all neurons β averaging them all.
Q5. Why does L2 β weight decay in Adam, and what's the fix?
Adam's adaptive per-parameter scaling breaks the equivalence that holds in SGD. Fix: use AdamW, which decouples weight decay from the gradient update.
Q6. Model is 99% train / 72% val. Which techniques, in order?
(1) Data augmentation, (2) weight decay (AdamW, Ξ»=10β»β΄), (3) early stopping, (4) dropout (p=0.3β0.5). Still overfitting β more data or a smaller model.
Q7. What's the earliest observable sign of overfitting during training?
Validation loss stops decreasing (and starts rising) while training loss keeps falling β the growing train/val gap.