Sub topic 2
Model capacity: how flexible a model is
This chapter assumes you know basic Python and NumPy, what a loss function is, and how scikit-learn's fit and predict work. It also assumes you have seen linear regression.
What capacity means
Model capacity is the range of patterns a model can represent. A model with low capacity can only draw simple shapes, such as a straight line. A model with high capacity can bend to fit almost any shape, including random noise in the training data.
Capacity depends on the model family and its settings. Common ways to raise it:
- Add polynomial features or raise the polynomial degree.
- Add more features.
- Add layers or units to a neural network.
- Allow a decision tree to grow deeper.
- Reduce the number of neighbours in k-nearest neighbours.
Capacity is not the same as the number of parameters, though the two are related. Penalties and early stopping can shrink the effective capacity of a model without changing its parameter count. Later chapters cover those.
Seeing capacity in code
The script below fits polynomials of degree 1, 4, and 15 to 40 noisy points from a sine curve. It prints the training and validation mean squared error for each.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
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)
for degree in (1, 4, 15):
model = make_pipeline(PolynomialFeatures(degree), StandardScaler(), LinearRegression())
model.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(degree, round(train_mse, 3), round(val_mse, 3))Each printed line shows the degree, the training error, and the validation error. Here is what to expect:
- Degree 1: A line cannot follow a sine wave, so both errors are high.
- Degree 4: The curve can follow the wave, so both errors should be low and close together.
- Degree 15: The model has far more flexibility than 28 training points can pin down. Training error should be the lowest of the three, but validation error can rise sharply.
The exact numbers depend on the random seed and the split, so run the script and read your own output.
The pattern to remember
Training error almost always falls as capacity rises. Validation error usually falls first and then rises. The gap between the two errors tends to widen with capacity. That widening gap is the signature of a model that is starting to fit noise.
Common mistakes
- Choosing capacity by training error. Training error always favours the most flexible model. Compare models on validation data.
- Assuming more capacity is always better. It helps only until the extra flexibility starts fitting noise. How soon that happens depends on how much data you have.
- Ignoring data size. With 40 points, degree 15 is far too flexible. With 40,000 points, the same model may be fine.
Exercise
You raise the polynomial degree from 4 to 15. Training error drops, but validation error rises. What does this say about capacity, and which error should guide your choice?
Show answer
The model now has more capacity than the data supports, so it is fitting noise in the training set. Validation error should guide the choice, because it estimates performance on unseen data, while training error will keep falling as capacity grows.