Fitting the Sample Too Well — Overfitting
Chapter 1 chose to fit the rule rather than write it. More fitting power looks like a better deal, and up to a point it is: push it harder and performance on the data in your hands keeps improving. Past that point, it improves only on the data in your hands.
That gap is this chapter. The cause is not the model's quality. It is that the quantity you are lowering and the quantity you want lowered are different things.
(Chapter 1 promised "Chapter 2" would cover why that loss — why cross-entropy. That needed the language of probability, so it moved to Chapter 0-3. This chapter takes the other half: what happens when you fit harder.)
1. What you lower vs. what you want lowered
What you actually want low is the error on data you have not seen. With pairs drawn from a distribution ,
the generalisation error. Equation (1) is an expectation (Chapter 0-3, equation (3)), and since is unknown you cannot evaluate it.
What you can evaluate is the average over your examples:
For a fixed , equation (2) is an unbiased estimate of equation (1) (Chapter 0-3, equation (5)). The trouble is that training does not hold fixed. The moment you choose to make small, stops being a fair estimate of . You are measuring a choice with the same ruler you chose by — that is what overfitting is, and the same story runs all the way to this chapter's common misreading.
2. Measured — what raising the degree does
Draw 20 points from with noise of standard deviation , fit a degree- polynomial by least squares, and test on 4,000 fresh points.
| degree | parameters | train RMSE | test RMSE |
|---|---|---|---|
| 1 | 2 | 0.5443 | 0.5165 |
| 3 | 4 | 0.1621 | 0.3211 |
| 5 | 6 | 0.1540 | 0.2947 |
| 9 | 10 | 0.1438 | 0.4457 |
| 15 | 16 | 0.1042 | 67.4518 |
| 19 | 20 | 0.0000 | 587889.3 |
Training RMSE never once rises. More capacity can always fit what is in front of it better. Test error, meanwhile, bottoms out at and then takes off. At the training error is exactly zero — 20 points threaded by 20 parameters — while the test error has six digits.
Test error cannot go below . The values carry noise, so even naming the true exactly leaves 0.25 behind. The 0.2947 at is sitting near that floor (the same role played in Chapter 0-4).
The figure's numbers will not match the tables. It draws its own sample, so the same and give different values — reads 0.4457 in the table above and 47.8 in the figure. What matches is the shape: training falls forever, test is U-shaped, more data closes the gap. The mismatch is itself a result. Where the parameter count approaches the sample size, changing the sample changes the order of magnitude of the test error. It is a signal not to compare unstable things on a single measurement.
3. The size side — there is no such thing as "a model that overfits"
Hold fixed and change only how many training points there are.
| size | train RMSE | test RMSE | gap |
|---|---|---|---|
| 10 | 0.0000 | 19246.5 | off the scale |
| 20 | 0.1438 | 0.4457 | 0.3019 |
| 50 | 0.2402 | 0.3462 | 0.1060 |
| 200 | 0.1939 | 0.2558 | 0.0619 |
| 1000 | 0.2464 | 0.2515 | 0.0052 |
The model did not change at all. It still breaks at and converges to within 0.005 at , both curves near the 0.25 floor.
The row is extreme because there are 10 parameters for 10 points. Whenever there exists a solution threading every point: zero error where the points are, and no constraint whatsoever between them, so it runs wild there.
4. Reading the symptoms
Put the two errors side by side and the next move is determined (measured at ).
| train | test | symptom | next move | |
|---|---|---|---|---|
| 0.5443 | 0.5165 | both high: underfitting | raise capacity | |
| 0.1540 | 0.2947 | together near the floor: balanced | stop here | |
| 0.1042 | 67.45 | wide gap: overfitting | lower capacity / add data / regularise |
In the row the gap is : the test error is lower than the training error (0.5165 against 0.5443). The sign flipping is sample noise — another draw puts it near . The point is not the sign but that underfitting leaves the gap small while both numbers stay high. What to read is not the gap but how high both are, compared against the floor . Watch only the gap and you will call an underfitting model good "because it isn't overfitting."
5. Remedies
Roughly in decreasing order of reliability.
- Add data. As the table in section 3 shows, this is the surest one. It fails only against underfitting.
- Lower the capacity. Fewer degrees, layers, width. Effective — and overshooting lands you in underfitting.
- Regularise. Penalise the size of the weights and squeeze capacity continuously.
Keeping (the setting that broke in section 2) and moving only :
| train RMSE | test RMSE | |
|---|---|---|
| 0 | 0.1042 | 67.4518 |
| 0.1264 | 3.3946 | |
| 0.1415 | 0.7237 | |
| 0.1574 | 0.3071 | |
| 0.2160 | 0.3914 | |
| 1 | 0.3849 | 0.3999 |
| 10 | 0.6174 | 0.6140 |
— back to roughly the result (0.2947) without lowering the degree. In the rows where is too large (1, 10) even the training error rises: that is the picture of having gone too far into underfitting.
Equation (3) also reads in the language of probability. Take the maximum likelihood of Chapter 0-3, add a prior on the weights, and maximise the posterior (MAP):
L2 regularisation just is the prior "the weights should be near zero." The correspondence is with the noise level: the more strongly you believe it (smaller ), the larger .
- Early stopping. Stop part-way through training, because the number of steps is a form of capacity too. Where to stop is decided on validation data — and the hazard in that is the next section.
- Data augmentation, dropout and the rest come in Chapter 4 onward.
A common misreading: "I picked it on validation data, so that number is the real performance"
Splitting data into train, validation and test is well-known practice. But why is two not enough? What was said under equation (2) applies unchanged: the ruler you chose with is not a fair ruler for the thing you chose.
Measured. Fit 90 candidates (15 degrees × 6 values of ) on 30 training points, pick the best on validation, then measure that model on 4,000 test points. Averaged over 300 repetitions:
| validation size | winner's validation RMSE | same model's test RMSE | optimism | share where optimistic |
|---|---|---|---|---|
| 10 | 0.2380 | 0.5787 | +0.3407 | 83.3% |
| 20 | 0.2541 | 0.3543 | +0.1002 | 72.3% |
| 100 | 0.2670 | 0.2725 | +0.0054 | 60.7% |
| 1000 | 0.2697 | 0.2704 | +0.0007 | 56.0% |
With 10 validation points, the winner's number looks 0.34 better than the truth. More validation data and the gap disappears.
That this is not coincidence is settled by removing the choosing. With the same 20 validation points but a single fixed candidate (, ), the mean gap is — essentially zero, against under the same conditions with selection. The bias comes from the act of choosing, not from the split.
It grows with the number of candidates (20 validation points):
| candidates | optimism |
|---|---|
| 6 | +0.0056 |
| 30 | +0.0178 |
| 90 | +0.1002 |
The more you try, the less the validation number means. The validation set has been spent on choosing, so reporting performance needs one more split that has never been used to choose anything. That is why there are three.
Exercises
The answers are folded away. Work them first.
Exercise 1. Twelve training points, fitting a degree-11 polynomial. (a) How many parameters? (b) What happens to the training error? (c) Is that good news?
Show the answer
(a) Twelve ().
(b) It goes to essentially zero. Twelve unknowns against twelve equations: for points in general position exactly one polynomial passes through all of them. The row in the text is the measurement — training RMSE 0.0000.
(c) No. Zero training error says nothing about the space between the points, which carries no constraint at all. The test RMSE in that same row is . Zero training error is not "learned well"; it is a signal that something went unmeasured.
Exercise 2. Two models measured on points. A: train 0.52, test 0.54. B: train 0.10, test 0.45. The noise is . (a) The symptom in each. (b) The remedy for each. (c) Is "the smaller the gap, the better the model" right?
Show the answer
(a) A is underfitting — the gap is a small 0.02, but both numbers are more than twice the floor of 0.25. B is overfitting (gap 0.35).
(b) A: raise capacity (degree, layers, width). More data will not lower it — the shortfall is expressive power, not sample size. B: add data, lower capacity, or regularise.
(c) No. A's gap of 0.02 beats B's 0.35, yet the test errors are 0.54 against 0.45: B is the better model. The gap is a symptom of overfitting, not a measure of quality. Read the test error itself, and its distance from the floor.
Exercise 3. You tried 90 candidates and chose on 20 validation points. The winner's validation RMSE is 0.25. (a) May you report 0.25 as its performance? (b) Roughly where is the real figure? (c) Which helps more: more validation data, or fewer candidates?
Show the answer
(a) No. That is the number you chose by. Under exactly these conditions (90 candidates, 20 validation points) the measurement in the text found a mean optimism of .
(b) Around 0.35, adding the 0.10 of optimism to 0.25. But that is an average over 300 repetitions, so it is an estimate applied to a single run, nothing firmer. To know, measure on test data that has never been used to choose.
(c) Both help, and in the measurements here validation size helped more. Going from 10 to 1000 validation points took the optimism from to ; going from 90 candidates to 6 took it from to . But cutting candidates means giving up search, so more validation data (or reusing it through cross-validation) usually comes first.
What this chapter does not say
- Double descent is not covered. The tables stop inside the range where raising keeps raising test error. The reported phenomenon of error falling again far past the point where parameters exceed examples cannot be shown in this setting (unregularised polynomials).
- Theoretical generalisation guarantees (VC dimension, PAC learning, uniform convergence) are untouched. This chapter is measurements only.
- Cross-validation — reusing data through splits — is not described. It is the standard remedy when validation data is scarce, and it acts on the top rows of the misreading table.
- The regularisation that actually does the work in deep learning (batch normalisation, augmentation, dropout, and the implicit bias of the optimiser itself) is Chapter 4 onward. The L2 here was chosen to show the mechanism in its smallest form.
- Every number was computed and checked (the three tables over degree, size and ; the symptom table; the selection optimism over 300 repetitions together with the no-selection control; and the candidate-count sweep). The estimator is the same throughout: least squares in a basis with mapped to , 4,000 test points, one seed.
Comments
Sign in to comment