Lesson 2.4 · 7 min read · about 50 min to master at a very slow pace

Generalisation and the Generalisation Gap

Generalisation is performing well on new data. The generalisation gap is how much worse a model does there than on its training data. What makes the gap grow or shrink, and what to do when test scores disappoint.

Not startedJump to quiz

Definition

Generalisation is the ability of a fitted model to perform well on new observations from the same population. The generalisation gap measures how far training performance overstates real performance:

The true gap can't be computed, because needs the unknown population. We estimate it by comparing training error with error on held-out data.

What affects the gap

FactorEffect on the gap
Model complexity / number of parametersMore flexible → bigger gap
Training-set size More representative data → smaller gap (it tends to 0 as )
Regularisation / algorithmic stabilityConstraining the model → smaller gap
Noise in the dataNoisier → easier to "fit the noise" → bigger gap
00.10.20.30.4100300500700900training-set size nerrorlarge gapsmall gap
Training errorValidation error
A learning curve: with more data, training error rises slightly (harder to memorise) and validation error falls. The gap closes.

When the test score disappoints

Scenario

Validation accuracy is 96%, but the final test accuracy is only 79%. What now?

Possible causes: overfitting to the validation set through many rounds of selection; distribution shift (the test data differ from training and validation, e.g. collected later or elsewhere); preprocessing applied inconsistently to the test set; or an evaluation bug.

Actions: investigate the cause; improve the model using only training and validation data; then get a fresh, independent test set for a final honest estimate. Don't keep tuning against the same test set: that would leak it (lesson 2.8).

Big idea

A small gap is good news only if both errors are low. A model can have a tiny gap and still be useless (underfitting: both errors high).

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

Going deeperValidation accuracy is 96% but test accuracy is 79%. What should I do?

First, investigate rather than tweak. Likely causes are overfitting (often to the validation set itself, after trying many models), distribution shift between the validation and test data, preprocessing inconsistencies (e.g. the test set was scaled differently), or an evaluation problem.

Then improve the model using only the training and validation data, and evaluate the final version on a fresh independent test set. If you keep adjusting until the old test score goes up, that test set has become a validation set and its score is no longer an honest estimate.

Cheatsheet

Generalisation gap drivers

↑ complexity → ↑ gap. ↑ data → ↓ gap. ↑ regularisation → ↓ gap. ↑ noise → ↑ gap.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptWhich change tends to shrink the generalisation gap?
Q2diagnoseValidation accuracy is 96%, but the final test accuracy is 79%. What should the team do?
Q3interpretTraining error is 30% and validation error is 31%. The gap is tiny. Is this a good model?
Q4interpretOn a learning curve, as the training-set size grows, what typically happens?
Q5diagnoseA model validated well at launch, but its accuracy drops steadily over the following months. What is the most likely cause?