What is regularization and how do the different techniques compare?
Written by the mlprep team · Updated
What is regularization and how do the different techniques compare? When would you pick one over another?
Regularization is any technique that reduces a model's tendency to overfit by constraining its capacity or adding noise during training. The goal is to improve generalization — performance on data the model hasn't seen.
L2 regularization (weight decay)#
Add a penalty λ‖w‖² to the loss. The gradient update becomes:
w ← w - η(∂L/∂w + 2λw) = w(1 - 2ηλ) - η·∂L/∂w
Every weight is shrunk toward zero by a constant factor each step — "weight decay." Large weights are penalized proportionally to their magnitude, so the model is pushed toward solutions with many small weights rather than a few large ones. Smooth, doesn't zero things out.
L1 regularization (Lasso)#
Add λ‖w‖₁ (sum of absolute values) to the loss. The gradient is ±λ — a constant regardless of weight size. This means small weights get the same shrinkage as large ones and are pushed all the way to zero. L1 produces sparse solutions: most weights become exactly zero, which is implicit feature selection.
The geometric intuition: the L1 ball (diamond) has corners at the axes. When the loss contours intersect the constraint set, they tend to hit the corners — where many weights are exactly zero.
Dropout#
During training, randomly zero out each neuron with probability p. The network can't rely on any single neuron because it might not be there. This forces the network to learn redundant representations across many neurons. At test time, all neurons are active but their outputs are scaled by (1-p) to compensate for the expected value during training.
Dropout is most effective in fully connected layers. It's rarely used in convolutional layers (spatial correlations make it less effective) and doesn't work well with batch normalization (they interact poorly).
Batch normalization#
Normalizes layer inputs to zero mean and unit variance during training. While not designed as a regularizer, it reduces internal covariate shift (the changing distribution of inputs to each layer), which allows higher learning rates and acts as a mild regularizer by adding noise (the batch statistics are noisy estimates of the population statistics).
When to use what#
L2 / weight decay: default choice for most neural networks, applied as AdamW's decoupled weight decay. Always on.
L1: when you want sparse feature selection — logistic regression on high-dimensional tabular data, or explicitly want to know which features matter.
Dropout: large fully connected networks, especially language model heads. Not in conv layers.
Batch norm: almost always in CNNs and in transformers (as layer norm). Primarily for training stability, regularization is a side benefit.
Interview red flags#
- Saying L1 and L2 both "shrink weights" without explaining why L1 produces exact zeros and L2 doesn't.
- Using Adam with L2 in the loss and calling it weight decay, without knowing why AdamW decouples them.
- Leaving dropout active at inference, or not knowing how inverted dropout scales activations.
- Reaching for regularization before checking whether the model is actually overfitting.
You said L1 drives weights to exactly zero but L2 doesn't. Intuitively, why does the math produce that difference?
The gradient behavior near zero#
For L2, the gradient contribution is 2λw. As w → 0, the gradient → 0. The penalty becomes negligible, so small weights stay small but never reach exactly zero — there's no force pushing them all the way there.
For L1, the gradient contribution is λ · sign(w) — a constant ±λ regardless of how small w is. A weight of 0.001 gets the same shrinkage pressure as a weight of 100. If the data gradient isn't strong enough to overcome λ, the weight gets pushed to zero and stays there (at zero, the L1 gradient is undefined, but subgradient methods set it to zero).
The practical consequence#
With L2: if a feature has weak but nonzero signal, the model keeps a small nonzero weight for it — the weight settles where the data gradient balances the decay pressure.
With L1: the same feature's weight gets the same constant push toward zero every step. If the data signal is weaker than λ, the weight reaches zero and the feature is effectively removed from the model.
This is why L1 is used for feature selection — it's a hard gate. If a feature doesn't carry enough predictive signal to overcome the regularization pressure, its weight is zeroed out entirely. L2 never fully ignores any feature; it just downweights everything proportionally.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Regularization constrains model capacity to reduce overfitting. L2 shrinks weights proportionally (never to zero). L1 shrinks by a constant (pushes small weights to exactly zero — implicit feature selection). Dropout forces redundant representations by randomly silencing neurons. Batch norm stabilizes training distributions. In practice: L2/AdamW by default, L1 when sparsity matters, dropout in fully connected layers.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How does early stopping relate to L2 regularization, and in what sense are they equivalent?
- Why does dropout not work well with batch normalization, and how does layer normalization compare?
- How would you diagnose whether a model is underfitting vs. overfitting and choose the right regularization response?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions