π Regularization
Descriptionβ
< What is it? >β
- [The idea]
-
Regularization is any deliberate constraint on a model that trades a little bias for a large drop in variance β see Bias & Variance. It is the standard answer to overfitting: the model class stays flexible, but the training objective is changed so that flexibility has a price.
-
Unregularized training minimizes the fit alone. Regularized training minimizes the fit plus a penalty on the parameters:
J(ΞΈ) = L(ΞΈ) + Ξ» Β· R(ΞΈ)β β βdata loss β penalty on the parametersstrength knob (Ξ» β₯ 0) -
Ξ» = 0 recovers the unregularized model (low bias, high variance). Ξ» β β crushes every weight toward zero (high bias, low variance). The useful value sits in between and is found by cross-validation, never by training error.
-
Weight decay
Weight decay is a regularization technique that penalizes large weights in a neural network to prevent overfitting. It adds an extra term to the loss functionβtypically the squared magnitude (L2 norm) of the weights. During optimization, this effectively shrinks the weights by a small factor at each update step (multiplying them by (1 - learning_rate * decay_factor) before applying the gradient). In PyTorch, you set it directly in the optimizer, e.g.:optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-4)
-
- [Why it works]
- A model overfits by using large, finely balanced weights to thread every training point, including the noise. Penalizing weight magnitude removes that option: the model can only spend "weight budget" on structure that pays for itself across many examples.
- Equivalently, the penalty is a prior. L2 corresponds to a Gaussian prior on the weights, L1 to a Laplace prior; regularized training is MAP estimation rather than maximum likelihood.
- Why regularization reduces overfitting (Andrew Ng, deeplearning.ai)
- Shrinking weights β a simpler network
- Small weights β near-linear layers

A large Ξ» pushes W[l] β 0, which effectively removes hidden units β the network collapses toward a much smaller one, sliding the fit from "high variance" back through "just right".

Small W[l] keeps z[l] in the central, near-linear stretch of tanh, so every layer behaves roughly linearly β and a stack of near-linear layers cannot carve out the wildly contorted boundary that overfitting needs.
- Why does drop-out work (Andrew Ng, deeplearning.ai)
< The two classic penalties >β
L2 (Ridge / weight decay) R(ΞΈ) = Ξ£ ΞΈβ±ΌΒ² β shrinks all weights smoothly toward 0
L1 (Lasso) R(ΞΈ) = Ξ£ |ΞΈβ±Ό| β drives many weights exactly to 0
Elastic Net R(ΞΈ) = Ξ± Ξ£|ΞΈβ±Ό| + (1βΞ±) Ξ£ ΞΈβ±ΌΒ²
- L2 has a smooth gradient (
2ΞΈ), so each update multiplies the weights by a factor slightly below 1 β hence the name weight decay. Correlated features share the weight between them rather than one winning outright. - L1 has a constant-magnitude gradient (
sign(ΞΈ)), so a weight is pushed toward zero at the same rate no matter how small it already is, and it lands exactly on zero. The result is feature selection: the fitted model ignores whole inputs. - Elastic Net keeps L1's sparsity while L2 stabilizes the choice among correlated features, which L1 alone makes arbitrarily.
< Why L1 produces sparsity >β
- Read the penalty as a constraint region: minimize the loss subject to
Ξ£|ΞΈβ±Ό| β€ t(L1) orΣθⱼ² β€ t(L2). The solution is where the loss contours first touch that region. - The L1 region is a diamond with corners on the axes; the L2 region is a circle with no corners. Contours coming in from an arbitrary direction usually touch a diamond at a corner β and a corner means some coordinate is exactly zero. A circle is touched at a generic point, where every coordinate is small but nonzero.
Key pointsβ
< Choosing a penalty >β
| Penalty | Effect on weights | Use when |
|---|---|---|
| L2 / ridge | All shrunk, none removed | Default. Many weakly-informative features; correlated inputs |
| L1 / lasso | Many set to exactly 0 | You want a sparse, interpretable model or automatic feature selection |
| Elastic Net | Sparse, but stable under correlation | Wide data (p β« n) with correlated feature groups |
| None | β | The model is already underfitting, or data volume vastly exceeds capacity |
< Practical rules that are easy to get wrong >β
- [Standardize first] the penalty is scale-sensitive β a feature measured in millimetres gets a larger weight, and so a larger penalty, than the same feature in metres. Standardize (or normalize) before fitting, or the penalty silently encodes your unit choices.
- [Do not penalize the intercept] the bias term
ΞΈβonly shifts the output; shrinking it biases predictions toward zero for no benefit. Standard libraries exclude it β custom loops often forget. - [Tune Ξ» on validation data] a log-spaced sweep (
1e-4 β¦ 1e2) under k-fold CV. Selecting Ξ» by test error turns the test set into a training signal. - [Ξ» scales with the data] a penalty tuned on 10k rows is usually too strong on 1M rows, because the data loss term grows while the penalty does not.
< Regularization beyond the penalty term >β
| Technique | Where it applies | Mechanism |
|---|---|---|
| Early stopping | Any iterative training | Stop at the validation-error minimum; less training time = less capacity actually used. Provably similar to L2 for linear models. For many loss landscapes (especially in the linear or neural tangent kernel regimes), early stopping is mathematically equivalent to L2 regularization (Tikhonov regularization). In L2 regularization, you explicitly penalize large weights by adding β₯ΞΈβ₯Β² to the loss function, forcing weights to stay small. In early stopping, you take fewer gradient descent steps. Because the gradient is multiplied by the learning rate at each step, the total movement of the weights is bounded. This implicitly restricts the norm (size) of the weight vector. Smaller weights produce smoother, less sensitive decision boundaries, which directly combats overfitting. |
| Dropout | Neural networks | Randomly zero a fraction of units per step, so no unit can depend on a specific co-adapted partner. Acts like averaging an ensemble of thinned networks |
| Data augmentation | Vision, audio, text | Encodes invariances (a rotated cat is a cat) as extra data β lowers variance without adding bias (rotation, zoom in/out, crop, etc) |
| More data | Everything | The one "regularizer" with no bias cost at all |
| Ensembling / bagging | Trees, any high-variance model | Averaging independent errors β see Random Forest |
| Label smoothing | Classification | Targets of 0.9/0.1 instead of 1/0 stop the network from driving logits to extremes |
| Pruning / max depth | Decision trees | Structural capacity limit; XGBoost adds explicit Ξ», Ξ± and Ξ³ terms to the split objective |
< In deep learning >β
- Weight decay β L2 under Adam. Adding
Ξ»βΞΈβΒ²to the loss makes the penalty pass through Adam's per-parameter scaling, so heavily-updated weights get decayed less. AdamW applies the decay directly to the weights instead, outside the adaptive step β which is why it is the default optimizer for transformers. - Typical starting points: weight decay
1e-2(AdamW, transformers) or1e-4(SGD, vision), dropout0.1in transformer blocks and0.5in wide fully-connected layers. - Batch norm / layer normalization is not regularization by design, but batch norm's dependence on the minibatch injects noise that has a mild regularizing side effect β one reason it and dropout are often not both needed.
- At very large scale the picture shifts: with enough data relative to parameters the model no longer has spare capacity to memorize, and heavy explicit regularization mostly costs accuracy. Scale is itself a variance reducer.
< Symptoms of getting Ξ» wrong >β
| Observation | Likely cause | Move |
|---|---|---|
| Train error low, validation much higher | Too little regularization | Raise Ξ», add dropout, get more data |
| Train and validation error both high, close together | Too much regularization | Lower Ξ» β you have induced bias |
| Validation error dips then climbs during training | Overfitting in progress | Early stopping at the dip |
| Weights all near zero, predictions near the mean | Ξ» far too large | Lower by an order of magnitude |
Referenceβ
- Deep Learning, ch. 7 β Regularization (Goodfellow, Bengio, Courville)
- Linear models and regularization in scikit-learn (scikit-learn docs)
- The Elements of Statistical Learning, ch. 3 β Shrinkage Methods (Hastie, Tibshirani, Friedman)
- Regression Shrinkage and Selection via the Lasso (Tibshirani, 1996)
- Dropout: A Simple Way to Prevent Neural Networks from Overfitting (Srivastava et al., 2014)
- Decoupled Weight Decay Regularization β the AdamW paper (Loshchilov & Hutter, 2019)