Skip to content

2.5 Generalization and regularization

ready≈ 8.5 hoursNeeds: 2.2Unlocks: 3.5
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Bias–variance sampler)
  4. 4 Do
  5. 5 Check

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 σ2\sigma^2 is the randomness in yy 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 λ∥w∥2\lambda\lVert\mathbf{w}\rVert^2 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.

Bias–variance decomposition of expected squared errorstep 1 of 5
  1. y=f(x)+ϵ,E[ϵ]=0,  Var[ϵ]=σ2,fˉ(x)=ED[f^(x)]y = f(x) + \epsilon, \quad \mathbb{E}[\epsilon] = 0, \; \mathrm{Var}[\epsilon] = \sigma^2, \quad \bar f(x) = \mathbb{E}_{\mathcal{D}}[\hat f(x)]

Bias–variance sampler

Variance across fits, bias of a model class

Static view on small screens. Open this page on a laptop to interact.

thin: one fit per dataset · thick: average fit · dashed: truth bias²0.003variance0.028noise σ²0.090expected0.122expected test MSE = bias² + variance + σ²

Each thin curve was fitted to a different random training set from the same source. Their spread is the variance; how far their average (thick) sits from the truth is the bias. Degree 0–1 on a sine: rigid, consistent, wrong. Degree 12: right on average, but every fit is different. λ pulls the high-degree fits together. More points per dataset shrink the variance without adding bias. Scores use |x| ≤ 0.9, away from the wild edges.

Left: many polynomial fits, each to its own random training set, with their average and the true function. Right: bars for bias squared, variance and noise, and their sum, the expected test error. Sliders set the degree, the ridge penalty λ, the number of datasets, the points per dataset and the noise level. Keyboard: Tab through the controls; arrow keys adjust sliders.

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.

Cross-validation and an L2 sweep stubs/m2_5_generalization.py

Loading 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.

Hints · kfold_indices 0/3 revealed
Hints · bias_variance 0/3 revealed

Locally: mlc check 2.5.

1. Training error is high, and test error is about the same. The likely problem is…
2. Training error is near zero but test error is large. What helps most directly?
3. Increasing λ in ridge regression typically…
4. Why not choose λ by its error on the test set?
5. Can any model get expected squared error below σ², the noise variance?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
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.