Overview
Curated: · Written: · Reviewed:
Regularization (L1/L2, Dropout)
In one line
Regularization adds a complexity penalty or a capacity limit to training so the fitted model generalizes better. Reach for it when a flexible model overfits, when features are numerous or correlated, or when the training error is low but validation error is not.
Key definitions
- Penalty strength (λ): the multiplier on the complexity term. In scikit-learn
LogisticRegression,Cis inverse strength, so a largerCmeans a weaker penalty. - L2 penalty: the sum of squared coefficients. It shrinks coefficients smoothly and rarely makes them exactly zero.
- L1 penalty: the sum of absolute coefficients. It can set coefficients exactly to zero.
- Elastic net: a mix of L1 and L2, with total strength and mixing ratio (
l1_ratio) both tuned. - Dropout: randomly masks hidden activations during training. Inference is deterministic and uses no mask.
- Early stopping: halting training at the validation checkpoint chosen by a declared metric, patience and maximum budget.
Formulas and complexity
One feature, no intercept, one observation $(x,y)=(1,3)$: L2 gives $\beta = 3/(1+\lambda)$ and L1 gives $\beta=\max(3-\lambda/2,,0)$. L1 reaches exactly zero at $\lambda=6$, while L2 at $\lambda=8$ is still $0.333$.
For gradient descent with the L2 term, each update is $w \leftarrow w - \eta(g + \lambda w)$, where $g$ is the data-loss gradient. With inverted dropout and drop probability $p$, survivors are scaled by $1/(1-p)$ during training so the expected activation matches inference.
How it works
The optimizer minimizes data loss plus a penalty on coefficient size, so each coefficient is pulled toward zero by a force set by λ. L2 pulls all coefficients proportionally and shares weight across correlated predictors. L1 applies a constant pull, so small coefficients hit zero and drop out. Dropout instead trains a random subnetwork on each step, which discourages units from relying on each other. Early stopping caps how far the optimizer moves from initialization.
When to use and when not to
| Situation | Prefer | Be wary of |
|---|---|---|
| Many correlated predictors, prediction is the goal | L2 | L1 alone, since selection among near-duplicates is arbitrary |
| Many features, most believed irrelevant, sparse model needed | L1 or elastic net | Reading a nonzero coefficient as causal importance |
| Correlated groups plus sparsity wanted | Elastic net | Choosing the mix ratio without CV |
| Deep network overfits | Dropout, weight decay, early stopping | Stacking every regularizer at default strength |
| Goal is an effect estimate with uncertainty | Unpenalized model with controls | Regularized standard errors or p-values |
| Training error high and validation also poor | Less regularization | More penalty |
Traps interviewers probe
- Unscaled features. The penalty tracks units, so a feature in small units gets a larger coefficient and is penalized more. Fix: standardize, and fit the scaler inside each CV fold.
- Reading L1 selection as causal. Correlated predictors enter or leave across resamples. Fix: check selected features across bootstraps or folds and say the selection is predictive, not causal.
- Leaking the test set. Tuning λ on test data or fitting the scaler on all rows makes the score optimistic. Fix: tune inside a pipeline with time or group-aware CV, then evaluate the locked model once.
- Calling a noisy early-stopping curve regularization. A bad learning rate or nonconvergence can look like it. Fix: declare the metric, patience and budget, and confirm the optimizer converged.
- Reporting p-values for penalized coefficients. Regularized estimates are biased and the ordinary standard errors do not apply. Fix: use an unpenalized specification for inference.
Interview tips
Open by naming the penalty and what it changes: "L2 shrinks coefficients smoothly, L1 can zero them, and both trade variance for bias through λ." Then state the assumption that matters, usually scale.
Follow-ups to expect: why standardization is required, what C means in logistic regression, how to pick λ without leakage, how elastic net handles correlated groups, and why dropout is disabled at serving.
30-second outline:
- Objective: data loss plus λ times a penalty on coefficients.
- L2 for correlated predictors, L1 for sparsity, elastic net for both.
- Penalty depends on scale, so standardize inside the fold.
- Tune λ with CV on a pipeline, then score the test set once.
- Diagnose with train and validation curves: high training error with poor validation means too much penalty.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.