Skip to content

2.1 The learning setup: data, model, loss

ready≈ 8.5 hoursNeeds: 1.4, 1.6Unlocks: 2.2, 2.6
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Polynomial overfit lab)
  4. 4 Do
  5. 5 Check

Every model in this course follows one recipe. You have a dataset {(xi,yi)}i=1n\{(\mathbf{x}_i, y_i)\}_{i=1}^{n}datasetn training pairs of an input x_i and its target y_i. Rows of X are the inputs xᵢᵀ; y stacks the targets.Introduced in 2.1 The learning setup: data, model, loss. UDL writes I pairs {xᵢ, yᵢ}; CS229 writes m examples (x⁽ⁱ⁾, y⁽ⁱ⁾)., a model that maps an input to a prediction y^=f(x;θ)\hat{y} = f(\mathbf{x}; \boldsymbol{\theta})predictionThe model's output for input x with parameters θ. The hat marks an estimate.Introduced in 2.1 The learning setup: data, model, loss. UDL writes f[x, φ] with parameters φ; CS229 writes h_θ(x)., and a loss L(θ)L(\boldsymbol{\theta})lossThe scalar we minimise; lower is better. Often an average over training examples.Introduced in 1.6 Optimization. that says how wrong the predictions are. Training means choosing the parameters θ\boldsymbol{\theta}parametersAll learnable parameters of a model, collected into one vector.Introduced in 1.6 Optimization. UDL uses φ (phi) for model parameters; CS229 and most papers use θ. that make the loss small, which is the optimization problem from module 1.6 (UDL ch. 2; CS229 notes, Part I). In this module the model is a polynomial, y^=∑kwkxk\hat y = \sum_k w_k x^k, and the loss is the mean squared error.

The goal is not a small loss on the training data. The goal is good predictions on new inputs from the same source. A flexible enough model can drive training error to zero by memorising the noise: a degree-nn polynomial passes exactly through n+1n+1 points. That is overfitting: training error keeps falling while error on unseen data turns up (UDL §8.3). The polynomial overfit lab below shows both curves at once.

So you hold some data back. The test set is set aside before you start and used once, at the end, to estimate performance on new data. Every look you take at it while choosing a model leaks a little information into your choices, and the estimate becomes optimistic. Choices such as the degree are made on a separate validation set, or by cross-validation in module 2.5 (UDL §8.5; scikit-learn, Cross-validation).

All of this rests on the i.i.d. assumption: training and test examples are independent draws from the same distribution. It breaks in two common ways. Leakage means information about the test set reaches training: duplicated rows, features computed from the label, or preprocessing statistics computed on all the data before splitting (scikit-learn, Common pitfalls: data leakage). Distribution shift means the world the model is used in differs from the one it was trained on (UDL ch. 8 Notes, “Real-world performance”). No split can detect shift. You can only design your test set to look like the deployment you care about.

Polynomial overfit lab

Overfitting, regularization, train vs test error

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

● train ○ test - - truth — fit 1e-31e-21e-11e01e1051015MSE (log scale) vs degree
train MSE 0.0249 test MSE 0.0378 noise floor σ² 0.0400 best test degree at this λ: 5

Raise the degree with λ = 0: training error only falls, while test error (orange) turns up once the curve starts chasing noise. Then add λ, or more training points, and watch the high-degree fit calm down. Test error can't beat the noise floor σ² on average. Training error can, but only by fitting the noise.

Left: training points (filled), test points (hollow), the true function (dashed) and a fitted polynomial. Right: training and test mean squared error against polynomial degree on a log scale. Sliders set the degree, the ridge penalty λ (introduced properly in 2.5), the noise level and the number of training points. Keyboard: Tab through the controls; arrow keys adjust sliders.

Split data with a seeded permutation, build polynomial features, fit them by least squares (the closed form comes in 2.2), and sweep the degree to produce the train/test curves from the widget yourself.

Train/test split experiments stubs/m2_1_setup.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m2_1_setup.py and run mlc check 2.1. The tests are identical; import the results on the progress page.

Hints · train_test_split 0/3 revealed
Hints · polyfeatures 0/3 revealed
Hints · sweep_degree 0/3 revealed

Locally: mlc check 2.1.

1. What is the test set for?
2. As the degree grows, training error falls steadily but test error falls and then rises. Past the turning point the model is…
3. You standardise features using the mean and standard deviation of the whole dataset, then split. What went wrong?
4. Can a degree-9 polynomial always reach zero training error on 10 points with distinct x values?
5. A spam filter trained on 2019 emails gets worse every year. That is…
  • 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.