Sub topic 1
What it means for a model to generalise
Prerequisites
This chapter assumes you know basic Python and NumPy. It also assumes you know what features, labels, and a loss function are in supervised learning. You should understand linear regression as fitting parameters by minimising a loss, and you should have used scikit-learn's fit and predict.
Generalisation in one sentence
A model generalises when it predicts well on data it has never seen. Training only shows the model examples it can learn from. What you care about is new data from the same source, such as tomorrow's customers or next month's images.
Training error and generalisation error
The training error is the loss measured on the data the model was fitted to. The generalisation error is the loss you would expect on fresh data drawn from the same process. You can never compute it exactly. You estimate it by holding some data back and measuring the loss there.
The two numbers can disagree. Training error is measured on examples the model has already adjusted itself to, so it is usually optimistic. The gap between them is called the generalisation gap.
Why the gap appears
Real data contains signal and noise. The signal is the pattern that holds for new data as well. The noise is random variation that is specific to your sample. A flexible model can lower its training error by fitting the noise as well as the signal. That helps on the training set and does nothing for new data, because the new data has different noise.
Measuring it
The code below fits a model on 70% of the data and reports the error on both parts.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, size=(40, 1))
y = np.sin(X).ravel() + rng.normal(0, 0.2, size=40)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=0)
model = LinearRegression().fit(X_train, y_train)
train_mse = mean_squared_error(y_train, model.predict(X_train))
val_mse = mean_squared_error(y_val, model.predict(X_val))
print(round(train_mse, 3), round(val_mse, 3))Here a straight line is fitted to a sine curve. Both errors will be fairly high, which tells you something different from a large gap. Later chapters show how to read each pattern.
Reading the two numbers
| Training error | Validation error | Usual meaning |
|---|---|---|
| High | High | The model is too simple to capture the pattern. |
| Low | Much higher | The model has fitted noise that does not carry over. |
| Low | Low, close to training | The model generalises well. |
Common mistakes
- Reporting training error as if it were performance.
- Reusing the same held-back data again and again to make choices, which slowly turns it into training data.
- Splitting data in a way that does not match use. If the model will predict future events, validate on later data, not on a random shuffle.
Exercise
A model has a training mean squared error of 0.02 and a validation mean squared error of 0.45. Is the problem a high generalisation gap, or is the model too simple? Explain your reasoning.
Show answer
The problem is a large generalisation gap. Training error is low, so the model is flexible enough to fit the training data. Validation error is much higher, so what it learned does not carry over to new data. A model that was too simple would show high error on both sets.