S14 · Lesson 1 · Learning from data
Fitting a model to examples, the sum-of-squares error and its root-mean-square form, least squares for a line, training and test error, overfitting and the gap between them, and how more data or regularization closes it
An engineer fitting a calibration curve to measurements, a modem adapting its equalizer, and a speech recognizer trained on hours of audio are all doing the same thing: choosing a model's parameters so it fits examples, in the hope it will also fit cases it hasn't seen. The hope is the hard part. A flexible enough model can fit any training set perfectly and still predict badly, and the whole of machine learning is about managing that gap. This course builds the tools, from least squares through Bayesian inference to neural networks, with every number checked.
- For a linear model with p parameters fitted by least squares to N noisy examples (noise variance σ²), the expected training and test errors when the model's form is correct: and .
- Training error always falls as parameters are added; test error rises. The gap, , is the cost of fitting the noise.
- More data (larger N) shrinks the gap for the same model.
Predict
A model with 5 parameters is fitted to 20 examples whose noise variance is 0.04. If the model's form is right, what mean squared error should you expect on new data?
Least squares fits part of the noise along with the signal, lowering the training error to = 0.03. On new data that fitted noise hurts instead of helping: = 0.05. The gap of 0.02 is , and only more data or fewer effective parameters shrinks it.
Name it
The setup
Given N examples , choose parameters w of a model to minimize the sum-of-squares error
and report the root-mean-square error , in the units of t.
Least squares for a line
For , setting the derivatives to zero gives
Generalization
The training error measures the fit to the examples; the test error, on fresh data, measures what matters. For a correct linear model with p parameters, N examples and noise variance σ², their expectations are . A model with too many parameters for its data overfits: it matches the noise.
Remedies
More data; fewer parameters; or regularization, adding a penalty to E that shrinks weights toward zero. For a single weight through the origin, regularized least squares gives .
Worked example
Fit a line to (0, 1), (1, 3), (2, 4) and find its RMS error.
Show the solution
- Sums. For the three points:
- Slope.
- Intercept.
- Residuals. They are −0.167, 0.333 and −0.167, so E = 0.0833 and
Answer the line is , with an RMS error of 0.236.
Worked example
A single weight through the origin has Σx² = 10 and Σxt = 8. How does regularization change it?
Show the solution
- Without regularization.
- With λ = 1.
- With λ = 10.
- What it trades. Larger λ trades a worse fit to the training data for a smaller, steadier weight, which lesson 9 shows can lower the test error.
Answer regularization shrinks the weight from 0.8 to 0.727 at λ = 1 and to 0.4 at λ = 10.
Worked example
A thermistor's calibration fits 8 points whose noise has σ = 0.02 (in the log of resistance). A correct model has 3 parameters. What training and test RMS errors should you expect with it, and with a 7-parameter polynomial that also contains the right model?
Show the solution
- Expected errors. For a correct linear model the expected training and test errors are
- Three parameters.
- Seven parameters. The training error is less than half the smaller model's, but the test error is larger:
- Compare. The bigger model looks better on the data it was fitted to and is worse on new data: training error alone can't choose a model.
Answer with 3 parameters, expect a training RMS error of 0.0158 and a test RMS error of 0.0235; with 7, a training error of 0.0071 and a test error of 0.0274.
Check
Fit a least-squares line to the points (0, 2), (1, 2), (3, 10). What's its slope?
Check
A fit has sum-of-squares error E = 2 over 10 examples. What's its RMS error?
Check
A correct linear model with 3 parameters is fitted to 100 examples with noise variance 0.04. What test mean squared error should you expect?
Check
For the same fit (3 parameters, 100 examples, noise variance 0.04), what's the expected gap between test and training error?
Check
A single weight through the origin has Σx² = 10 and Σxt = 3. What weight does regularization with λ = 20 give?
Check
A correct model with 5 parameters on data with noise variance 0.02: how many examples keep the expected test–training gap down to 0.01?
Check
A least-squares line is fitted to 10 points with Σx = 20, Σx² = 80, Σt = 80 and Σxt = 140.
(a)
What is the slope ?
(b)
What is the intercept ?
Check
A correct linear model with p parameters is fitted by least squares to N examples whose noise has variance s.
Step 1
What mean squared training error do you expect?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What mean squared test error do you expect?
Step 3
How large is the gap between them?
Explain why
Why does a model's training error understate how well it will do on new data?
Least squares picks the parameters that make the model fit the training examples as closely as possible, and the examples contain noise as well as the underlying relationship. With p adjustable parameters, the fit can bend to follow part of that particular noise, roughly a fraction p/N of it, so the training residuals come out smaller than the true noise. New examples carry fresh, independent noise that the fitted bends don't match, and the parameters' own estimation error now adds to the noise. So test error sits above the noise level by about as much as training error sits below it, and the more parameters per example, the more misleading the training error becomes.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.