Q&A-1Explain regularization and when it helps.
Answer
The 90-second answer. Regularization helps when training loss keeps falling while validation loss flattens or turns up: a widening generalization gap. If both curves are high and close together, you're underfitting, and every penalty you add makes it worse. So the first move is to plot both curves on the same axes and read the gap, not to reach for a knob.
Why it works: two mechanisms worth being able to derive.
L2 / weight decay. Add λ‖w‖² to the loss. The gradient picks up 2λw, so the update becomes w ← w(1 − 2ηλ) − η∇L: each step multiplies the weights by a factor just under 1 before the data gradient pulls them back. The fixed point is a shrunk version of the unregularized optimum. Minimize (w − 3)² + λw²: setting the derivative to zero gives w* = 3/(1 + λ). At λ = 0.5 the optimum moves from 3 to 2: a 33% shrink. That is the whole effect: you trade bias for variance, and the bias is bounded by how far the true optimum sits from zero.
Dropout. At train time each unit is zeroed with probability p and the survivors scaled by 1/(1 − p) (inverted dropout), so activations keep their expected magnitude. That samples one of 2ⁿ subnetworks per step; at test time you run the full network with no scaling, which approximates the geometric mean of those subnetworks' predictions. The variance argument: a unit can't count on a specific partner being present, so co-adaptation is penalized. p = 0.5 is the nn.Dropout default and is right for wide fully-connected layers; 0.1–0.2 is typical for conv stacks and transformer blocks, where 0.5 destroys too much signal.
| penalty | what it does | typical value |
|---|---|---|
| L2 / weight decay | shrinks weights toward 0 | SGD ResNet-50: 1e-4; AdamW transformers: 0.01–0.1 |
| L1 | soft-thresholds weights to exactly 0 | 1e-5–1e-4; use when you want sparsity |
| dropout | stochastic subnetwork sampling | 0.5 FC, 0.1–0.2 conv/transformer |
| label smoothing | softens targets | 0.1 |
| early stopping | keeps the best validation checkpoint | patience 5–20 epochs |
Failure modes and how you'd catch each.
- Over-regularization. Train and validation loss both sit above the unregularized baseline. Detect by sweeping λ over a log grid (1e-6 … 1e-1) and plotting validation loss: the curve is U-shaped, and if validation loss is still falling at your smallest λ you are under-regularized, not over.
- Dropout left on at inference. Predictions become stochastic: the same input gives different outputs. Detect by running one batch twice in eval mode; outputs must be identical. In PyTorch this is a missing
model.eval(). - Weight decay applied to biases and norm parameters. Usually hurts. Detect by ablating: exclude 1-D parameters from the decay group and compare validation loss.
- L2 inside Adam is not weight decay. Adam's adaptive denominator scales the penalty by 1/√v̂, so low-gradient weights get decayed hardest. Detect by running
Adam(weight_decay=λ)againstAdamW(weight_decay=λ): on transformers AdamW wins, which is whytorch.optim.AdamWdefaults to 0.01. - Early stopping with too little patience. Stops inside a plateau. Detect by extending the run past the stop point and checking whether validation loss resumes falling.
When not to regularize. Underfitting (both curves high and close), a model already constrained by size or heavy augmentation, or a run whose bottleneck is optimization rather than generalization: a learning-rate schedule or better initialization will do more than a penalty. And regularization is not a substitute for fixing leakage or label noise; those show up as a validation curve that is too good, not too bad.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.