Textbook › Part III Deep Learning: what assumption you add
CHAPTER 2

Fitting the Sample Too Well — Overfitting

FREE14 min

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 D\mathcal{D},

R(f)  =  E(x,y)∼D[ ℓ(f(x), y) ]R(f) \;=\; \mathbb{E}_{(x,y)\sim \mathcal{D}}\big[\,\ell(f(x),\, y)\,\big]
(1)

the generalisation error. Equation (1) is an expectation (Chapter 0-3, equation (3)), and since D\mathcal{D} is unknown you cannot evaluate it.

What you can evaluate is the average over your nn examples:

R^(f)  =  1n∑i=1nℓ(f(xi), yi)\hat{R}(f) \;=\; \frac{1}{n}\sum_{i=1}^{n} \ell(f(x_i),\, y_i)
(2)

For a fixed ff, equation (2) is an unbiased estimate of equation (1) (Chapter 0-3, equation (5)). The trouble is that training does not hold ff fixed. The moment you choose ff to make R^\hat{R} small, R^\hat{R} stops being a fair estimate of RR. 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 y=sin⁡(2πx)+εy = \sin(2\pi x) + \varepsilon with noise of standard deviation σ=0.25\sigma = 0.25, fit a degree-dd polynomial by least squares, and test on 4,000 fresh points.

degree dd 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 d=5d=5 and then takes off. At d=19d=19 the training error is exactly zero — 20 points threaded by 20 parameters — while the test error has six digits.

Test error cannot go below σ=0.25\sigma = 0.25. The yy values carry noise, so even naming the true sin⁡(2πx)\sin(2\pi x) exactly leaves 0.25 behind. The 0.2947 at d=5d=5 is sitting near that floor (the same role H(p)H(p) played in Chapter 0-4).

FIG 1Left, the fit; right, the learning curve for that degree (x = training size). Raise the degree and the left curve starts chasing individual points while the two curves on the right separate. Slide n to the right and they converge again at the same degree — overfitting is not a property of the model alone. The dashed σ line is the floor no model gets under

The figure's numbers will not match the tables. It draws its own sample, so the same dd and nn give different values — d=9, n=20d=9,\ n=20 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 d=9d = 9 fixed and change only how many training points there are.

size nn 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 n=10n = 10 and converges to within 0.005 at n=1000n = 1000, both curves near the 0.25 floor.

The n=10n = 10 row is extreme because there are 10 parameters for 10 points. Whenever n≤d+1n \le d+1 there exists a solution threading every point: zero error where the points are, and no constraint whatsoever between them, so it runs wild there.

"This model overfits easily" is only half true. Overfitting is a property of the model together with the sample size. The same model is overfitting at one size and underfitting at another. That is why "get more data" is always on the list of remedies.

4. Reading the symptoms

Put the two errors side by side and the next move is determined (measured at n=20n = 20).

train test symptom next move
d=1d=1 0.5443 0.5165 both high: underfitting raise capacity
d=5d=5 0.1540 0.2947 together near the floor: balanced stop here
d=15d=15 0.1042 67.45 wide gap: overfitting lower capacity / add data / regularise

In the d=1d=1 row the gap is −0.0278-0.0278: 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 +0.06+0.06. 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 σ\sigma. 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.

  1. Add data. As the table in section 3 shows, this is the surest one. It fails only against underfitting.
  2. Lower the capacity. Fewer degrees, layers, width. Effective — and overshooting lands you in underfitting.
  3. Regularise. Penalise the size of the weights and squeeze capacity continuously.

J(w)  =  R^(w)  +  λ∥w∥2J(w) \;=\; \hat{R}(w) \;+\; \lambda \lVert w \rVert^2
(3)

Keeping d=15d = 15 (the setting that broke in section 2) and moving only λ\lambda:

λ\lambda train RMSE test RMSE
0 0.1042 67.4518
10−610^{-6} 0.1264 3.3946
10−410^{-4} 0.1415 0.7237
10−210^{-2} 0.1574 0.3071
10−110^{-1} 0.2160 0.3914
1 0.3849 0.3999
10 0.6174 0.6140

67.45→0.307167.45 \to 0.3071 — back to roughly the d=5d=5 result (0.2947) without lowering the degree. In the rows where λ\lambda 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 w∼N(0,τ2I)w \sim \mathcal{N}(0, \tau^2 I) on the weights, and maximise the posterior (MAP):

−log⁡p(w∣data)  =  −log⁡p(data∣w)⏟likelihood  +  ∥w∥22τ2  +  const-\log p(w \mid \text{data}) \;=\; \underbrace{-\log p(\text{data} \mid w)}_{\text{likelihood}} \;+\; \frac{\lVert w \rVert^2}{2\tau^2} \;+\; \text{const}
(4)

L2 regularisation just is the prior "the weights should be near zero." The correspondence is λ=σ2/τ2\lambda = \sigma^2/\tau^2 with σ\sigma the noise level: the more strongly you believe it (smaller τ\tau), the larger λ\lambda.

  1. 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.
  2. 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 λ\lambda) 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 (d=5d=5, λ=10−2\lambda=10^{-2}), the mean gap is +0.0049+0.0049 — essentially zero, against +0.1002+0.1002 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.

Look at the test set and change course, and it has been demoted to a validation set. Do "the test score was poor so I added a layer" a few times and the same optimism from the table above moves onto your test numbers. How many times you have looked determines how much that number is worth.

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 (d+1d+1).

(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 d=19, n=20d=19,\ n=20 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 587889.3587889.3. Zero training error is not "learned well"; it is a signal that something went unmeasured.

Exercise 2. Two models measured on nn points. A: train 0.52, test 0.54. B: train 0.10, test 0.45. The noise is σ=0.25\sigma = 0.25. (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 +0.1002+0.1002.

(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 +0.3407+0.3407 to +0.0007+0.0007; going from 90 candidates to 6 took it from +0.1002+0.1002 to +0.0056+0.0056. 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 dd 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 kk 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 λ\lambda; 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 xx mapped to [−1,1][-1,1], 4,000 test points, one seed.

Going deeper

Comments

Sign in to comment