2.5 Generalization and regularization
1. Learn
Section titled “1. Learn”- Understanding Deep Learning (Prince)chapter 8 Measuring performance (pp. 118–137) and chapter 9 Regularization, §9.1–9.2 (pp. 138–143)
2. Bridge
Section titled “2. Bridge”Module 2.1 showed test error rising as a model gets more flexible. This module explains why, and what to do about it.
Three sources of error. Imagine drawing many training sets from the same source and fitting a model to each. At a test point, the expected squared error splits exactly into three parts (UDL §8.2). Noise is the randomness in itself, and no model can remove it. Bias² is how far the average fit is from the truth, because the model class is too rigid. Variance is how much the fits differ from one training set to the next, because the model chases each set’s noise. A more flexible model lowers bias and raises variance. The sampler below draws those fits for you, and the derivation shows where the three terms come from.
Regularization restricts a flexible model instead of switching to a rigid one. L2 regularization (ridge) adds to the loss. It shrinks the weights, so fits vary less between datasets: variance drops and bias rises a little. Module 2.4 showed the same penalty is MAP estimation with a Gaussian prior (UDL §9.1). The bias weight is left unpenalised.
Choosing λ. You can’t use training error (it always prefers λ = 0) or the test set (module 2.1). k-fold cross-validation splits the training data into k folds. Each fold takes one turn as the validation set while you train on the others, and the k validation errors are averaged. Then you pick the λ with the lowest average and refit on all the training data (UDL ch. 8 Notes; scikit-learn, Cross-validation).
A modern caveat. The U-shaped test curve is not the whole story. Push capacity far past the point of fitting the training data exactly, and test error can fall again: double descent (UDL §8.4; Belkin et al. 2019). Large neural networks live in that regime and generalize well despite having more parameters than examples. Why they do is still an open question; UDL §8.4.1 lists the candidate explanations. Implicit regularization from the optimizer and explicit tricks such as dropout come back in Part 3.
3. Explore
Section titled “3. Explore”Build k-fold cross-validation and ridge regression, use them to pick λ for a degree-12 polynomial, and estimate bias² and variance by Monte Carlo. The tests check your decomposition against an independent simulation of the expected test error.
stubs/m2_5_generalization.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m2_5_generalization.py and run mlc check 2.5. The tests are identical; import the results on the progress page.
Locally: mlc check 2.5.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.