3.A — L2 weight decay as a Gaussian prior (MAP) [appendix]
Goal: Derive that the L2-penalised objective is exactly the MAP estimate under a Gaussian prior on weights, and connect the penalty strength to the prior variance.
Optional appendix. This session gives the Bayesian reading of weight decay (already used pragmatically in 3.6 AdamW). It does not block Chapters 4–12 — skip if you only want the operational picture.
1. Why this session exists
Weight decay is by far the most common regulariser in deep learning. We've already seen the mechanics (3.6: a shrinkage each step). What we haven't done is justify why shrinking weights toward zero should help.
The standard justification is Bayesian: weight decay is the MAP estimate of under a Gaussian prior on the weights. The penalty strength is one-over-prior-variance. Once you see the derivation, the choice of L2 (rather than L1 or L4 or anything else) stops being arbitrary — it corresponds to the Gaussian assumption, which is the "most ignorant" symmetric prior in the maximum-entropy sense.
2. Recap: MLE and the loss
From 0.7: the maximum-likelihood estimator (MLE) of given data is
For Gaussian regression , the right-hand side is (up to a constant) the squared-error loss. For categorical classification with softmax outputs, it is the cross-entropy. The training loss is — by design — proportional to the negative log-likelihood.
3. Adding a prior: MAP
Now treat as a random variable with a prior . By Bayes' rule (0.5),
The maximum-a-posteriori (MAP) estimate is
This is the MLE objective with an extra term — the negative log-prior, acting as a regulariser. The shape of this term encodes our beliefs about before seeing the data.[1]
4. The Gaussian prior
Pick the most basic symmetric prior: each parameter is independent and Gaussian with mean and variance . Then
so
Substitute into the MAP objective:
Comparing to the L2-penalised loss , we read off
The penalty strength is the reciprocal of the prior variance. Large ↔ tight prior (we believe weights should be small). Small ↔ broad prior (we are agnostic). ↔ infinite prior variance ↔ pure MLE.
This is the precise statement of "L2 regularisation = Gaussian prior on weights".
5. Why Gaussian, why on weights?
Three reasons to like this choice.
(a) Maximum entropy. Among all distributions on with a given variance, the Gaussian is the one with maximum entropy (0.6). It is the least-committal symmetric prior compatible with "weights should be around zero, with some characteristic scale".
(b) Smoothness of . For a linear model , the gradient w.r.t. input is , so . Small ↔ small Lipschitz constant ↔ output changes little under small input perturbations. The L2 penalty encourages a smoother predictor, which generalises better when the true target function is smooth. The same intuition extends to deep nets, where each layer's weight matrix contributes to the overall Lipschitz constant.
(c) Numerical stability. Big weights amplify activations, which amplify gradients, which can explode. Keeping bounded keeps the forward/backward passes in a sane numeric range — relevant for fp16 training of LLMs.
What about biases? Under the same Gaussian prior, biases would also be shrunk. In practice we do not shrink biases. The justification: biases set the operating point of each neuron's activation (e.g., they determine where a ReLU starts firing); there is no statistical reason to believe operating points should cluster around zero. The bias on the output layer in particular calibrates the mean of the prediction and is left free. This matches the AdamW convention from 3.6 of excluding biases from weight decay.
6. What about L1?
The same derivation with a Laplace prior yields
So L1 regularisation = Laplace prior on weights. The minimiser of tends to set many exactly to zero (sparsity), because the Laplace prior is sharply peaked at zero. This is the basis of LASSO regression.
In deep learning L1 is rare for hidden-layer weights — sparsity in weights doesn't translate to sparsity in activations, and dense weights are typically more accurate. L1 reappears in specific contexts: sparse coding, structured pruning, mixture-of-experts gating losses.
7. The Hessian view (deep nets, briefly)
The MAP objective is
Its Hessian is
The L2 term shifts every eigenvalue of the Hessian up by . This regularises the conditioning: directions in parameter space that were nearly flat under alone (eigenvalues near zero — many in deep nets, the "degenerate" directions) become -curved under . The optimiser sees a strictly convex bowl in those directions and converges instead of wandering. This is the same trick as Levenberg–Marquardt or damped Newton; weight decay is essentially a free Tikhonov regulariser on the parameter geometry.
8. Takeaways
- The L2-penalised loss is the MAP estimate under an independent Gaussian prior on each parameter, with mean zero and variance .
- The penalty coefficient is . Large ↔ tight prior.
- L1 corresponds to a Laplace prior (sparsity-inducing). Used in sparse models, rare for hidden-layer weights of deep nets.
- Biases are typically excluded — no reason to believe operating points cluster at zero.
- Hessian view: L2 shifts every eigenvalue of up by , regularising flat directions of the loss landscape.
(a) Derive the L2 = Gaussian-prior MAP claim from scratch: write with , take , and identify the regulariser.
(b) A practitioner uses with their AdamW. What prior variance does this correspond to? What does it imply about the typical magnitude they expect for a single parameter?
(c) Derive that L1 = Laplace prior MAP. Why does the Laplace prior produce sparse solutions while Gaussian does not? (Hint: look at the shape of near for each prior.)
(d) Show that for a linear regression model with Gaussian likelihood and Gaussian prior , the MAP estimator is exactly ridge regression: .
References
- [1]
Bishop, C. M. (2006). Pattern Recognition and Machine Learning, §3.4 and §5.5. — L2 as Gaussian prior / MAP (Appendix 3.A).
↩