Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Keep the weights small
A flexible network trained on little data can twist itself to pass through every training point, noise included. Large weights are what let it make sharp twists. Adding a cost for large weights to the training loss makes the network prefer smoother, simpler fits. The L2 penalty charges the square of each weight; the L1 penalty charges its absolute value, which pushes many weights all the way to zero.
Weight decay is in almost every training recipe, from image classifiers to language models, and its strength is one of the hyperparameters that most affects generalisation. L1 is used when a sparse model is wanted, for example to prune connections for small onboard computers.
- 1. An L2 penalty charges…
- 2. Which penalty sets many weights exactly to zero?
- 3. Penalising weights mainly fights…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- L2: loss L(w) + (λ/2)‖w‖²; gradient ∇L + λw.
- SGD step: w ← w − η(∇L + λw) = (1 − ηλ)w − η∇L. The factor (1 − ηλ) is weight decay: with η = 0.1, λ = 0.01, it is 0.999 per step and 0.999¹⁰⁰⁰ ≈ 0.37 over 1000 steps of no gradient.
- L1: loss L(w) + λ‖w‖₁; subgradient ∇L + λ·sign(w).
- Proximal step for L1: take the gradient step on L, then soft-threshold: S(w, t) = sign(w)·max(|w| − t, 0) with t = ηλ. Example: S((3, −0.5, 0.2, −2), 1) = (2, 0, 0, −1). Weights smaller than t become exactly zero.
- Bayesian reading: minimising mean NLL + (λ/2)‖w‖² is MAP estimation with a Gaussian prior N(0, σ²) on each weight, with λ = 1/(nσ²). L1 corresponds to a Laplace prior.
- Biases and norm parameters are usually not penalised: they do not create the sharp fits the penalty targets.
- 1. S(3, 1) is…
- 2. For SGD, weight decay with factor (1 − ηλ) equals…
- 3. A Gaussian prior with σ = 1 and n = 100 gives λ =…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Penalties in code
Exercise 1.
- SGD with an L2 penalty and SGD with weight decay gave identical weights after 200 steps.
- On a sparse regression with 2 real and 8 irrelevant features, subgradient descent left the irrelevant weights jittering between about −0.01 and 0.01, never exactly zero.
- Proximal descent set all 8 to exactly zero and shrank the real weights from 3 and −2 to 2.77 and −1.75.
Practice.
- In PyTorch, pass
weight_decayto the optimiser and use parameter groups to exclude biases and normalisation weights. - For Adam use AdamW, whose decay is decoupled (E3-8).
- Typical λ: 10⁻⁴–5 × 10⁻⁴ for SGD on CNNs, 0.01–0.1 for AdamW on transformers.
- With normalisation layers, a weight followed by batch or layer normalisation has a scale-invariant effect on the output. Decay then does not change what the network computes; it shrinks ‖w‖, which raises the effective learning rate (van Laarhoven, 2017).
decay, no_decay = [], []
for n, p in model.named_parameters(): (no_decay if p.ndim == 1 else decay).append(p)
opt = torch.optim.SGD([{"params": decay, "weight_decay": 5e-4}, {"params": no_decay, "weight_decay": 0}], lr=0.1, momentum=0.9)- 1. Biases are usually…
- 2. Subgradient descent for L1 gives weights that are…
- 3. With a normalisation layer after it, decaying a weight mainly…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
L2 in a linear model is ridge regression: it shrinks the solution along each eigendirection of XᵀX by the factor sᵢ/(sᵢ + λ), most where the data say least (D1-5). In networks the same intuition holds locally around a minimum, with the Hessian in place of XᵀX.
Why L1 gives zeros. The proximal operator of λ‖w‖₁ is the soft threshold. Its output is zero whenever the gradient step leaves |w| ≤ ηλ, which happens for any weight whose loss gradient is weaker than λ. The L1 ball has corners on the axes, where solutions land.
Weight decay and scale invariance. If the output does not change when w is scaled (a layer followed by normalisation), then the gradient is orthogonal to w and its size scales as 1/‖w‖. Decay shrinks ‖w‖, so the same learning rate makes larger angular changes. Weight decay then acts as a learning-rate schedule rather than a capacity penalty.
Not a full explanation of generalisation. Large networks generalise even with tiny decay, and SGD has its own implicit regularisation (B4-3). Penalties are one tool among several.
- 1. Ridge shrinks each eigendirection by…
- 2. A weight's proximal L1 output is zero when its gradient step leaves |w| ≤…
- 3. For a scale-invariant weight, the gradient is…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Penalties on a small network
History.
- A. Tikhonov (1943) introduced L2 regularisation for ill-posed problems; A. Hoerl and R. Kennard (1970) applied it as ridge regression.
- R. Tibshirani (1996) introduced the lasso.
- S. Hanson and L. Pratt (1989) and A. Krogh and J. Hertz (1992) studied weight decay in neural networks.
- T. van Laarhoven (2017) showed L2 with normalisation acts on the effective learning rate.
- I. Loshchilov and F. Hutter (2019) introduced decoupled weight decay for Adam.
Exercise 2. A 2–32–32–2 tanh network trained on 60 noisy two-moons points by full-batch SGD with momentum (2000 steps) and tested on 2000 points. Means of three seeds:
| penalty | λ | test accuracy | test cross-entropy | fraction of weights ≈ 0 |
|---|---|---|---|---|
| none | 0 | 0.857 | 1.361 | 0.002 |
| L2 | 0.003 | 0.882 | 0.398 | 0.009 |
| L2 | 0.01 | 0.895 | 0.275 | 0.001 |
| L2 | 0.03 | 0.841 | 0.363 | 0.000 |
| L1 (proximal) | 0.01 | 0.873 | 0.724 | 0.947 |
| L1 (proximal) | 0.03 | 0.878 | 0.516 | 0.989 |
- L2 at λ = 0.01 raised test accuracy by about 3.5 points and cut the test loss fivefold. Without a penalty the network made confident mistakes on test points.
- Too much L2 (0.03) underfitted: accuracy fell below no penalty.
- L1 at 0.03 kept only about 1% of the weights (about 12 of 1120), yet beat no penalty on both measures. It was less accurate than the best L2.
- Lesson: tune λ over a log grid; L2 for accuracy, L1 when sparsity itself is the goal.
Weight penalties in AI, robotics and aerospace
- Every modern training recipe uses weight decay; its value is reported alongside the learning rate.
- Sparse networks for embedded hardware: L1 and related penalties produce models that can be pruned for flight computers and microcontrollers.
- System identification: L1 picks out the few terms that matter in a learned dynamics model (SINDy uses a related sparse regression).
References
- A. Krogh, J. Hertz (1992). “A simple weight decay can improve generalization.” Advances in NeurIPS 4.
- R. Tibshirani (1996). “Regression shrinkage and selection via the lasso.” J. Royal Statistical Society B 58(1).
- T. van Laarhoven (2017). “L2 regularization versus batch and weight normalization.” arXiv:1706.05350.
- I. Loshchilov, F. Hutter (2019). “Decoupled weight decay regularization.” Proc. ICLR.
- I. Goodfellow, Y. Bengio, A. Courville (2016). Deep Learning, chapter 7. MIT Press.