S141 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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: σ2(1−p/N) and σ2(1+p/N).
  • Training error always falls as parameters are added; test error rises. The gap, 2σ2p/N, 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?

Name it

The setup

Given N examples (xn,tn), choose parameters w of a model y(x,𝐰) to minimize the sum-of-squares error

E(𝐰)=12∑n=1N[y(xn,𝐰)−tn]2

and report the root-mean-square error ERMS=2E/N, in the units of t.

Least squares for a line

For y=w0+w1x, setting the derivatives to zero gives

w1=N∑xt−∑x∑tN∑x2−(∑x)2,w0=t¯−w1x¯

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 σ2(1∓p/N). A model with too many parameters for its data overfits: it matches the noise.

Remedies

More data; fewer parameters; or regularization, adding a penalty λ2‖𝐰‖2 to E that shrinks weights toward zero. For a single weight through the origin, regularized least squares gives w=∑xt/(∑x2+λ).

Worked example

Fit a line to (0, 1), (1, 3), (2, 4) and find its RMS error.

Show the solution
  1. Sums. For the three points: N=3,∑x=3,∑t=8 ∑x2=5,∑xt=11
  2. Slope. w1=3×11−3×83×5−9=1.5
  3. Intercept. w0=8/3−1.5×1=1.167
  4. Residuals. They are −0.167, 0.333 and −0.167, so E = 0.0833 and ERMS=2×0.0833/3=0.236

Answer the line is y=1.167+1.5x, 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
  1. Without regularization. w=8/10=0.8
  2. With λ = 1. w=8/11=0.727
  3. With λ = 10. w=8/20=0.4
  4. 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
  1. Expected errors. For a correct linear model the expected training and test errors are σ2(1−p/N)andσ2(1+p/N)
  2. Three parameters. train RMS=0.025/8=0.0158test RMS=0.0211/8=0.0235
  3. Seven parameters. The training error is less than half the smaller model's, but the test error is larger: train RMS=0.021/8=0.0071test RMS=0.0215/8=0.0274
  4. 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 y=w0+w1x is fitted to 10 points with Σx = 20, Σx² = 80, Σt = 80 and Σxt = 140.

(a)

What is the slope w1?

(b)

What is the intercept w0?

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

Explain why

Why does a model's training error understate how well it will do on new data?

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.