Capacity, Overfitting and Double Descent
A learning algorithm is judged not on the data it trained on but on new data from the same distribution. The gap between training and test error depends on the model's capacity: how rich a family of functions it can fit. Classical theory predicts a U-shaped test error as capacity grows; deep networks, with far more parameters than data points, add a twist called double descent. The basics are in Machine Learning and Model Selection.
Generalization error
With training data drawn from a distribution ,
Underfitting: the training error is high (too little capacity). Overfitting: the training error is low but the test error much higher (capacity used to memorize noise). The bias-variance decomposition of the squared error makes this precise: expected test error bias variance irreducible noise.
import numpy as np
rng = np.random.default_rng(0)
f = lambda x: np.sin(2 * np.pi * x)
x_tr = rng.uniform(0, 1, 15); y_tr = f(x_tr) + rng.normal(0, 0.2, 15)
x_te = rng.uniform(0, 1, 500); y_te = f(x_te) + rng.normal(0, 0.2, 500)
for deg in (1, 3, 9, 14):
c = np.polynomial.polynomial.polyfit(x_tr, y_tr, deg)
mse = lambda x, y: np.mean((np.polynomial.polynomial.polyval(x, c) - y) ** 2)
print(f"degree {deg:2d}: train MSE {mse(x_tr, y_tr):.4f} test MSE {mse(x_te, y_te):.4f}")degree 1: train MSE 0.1472 test MSE 0.3629
degree 3: train MSE 0.0171 test MSE 0.0432
degree 9: train MSE 0.0102 test MSE 0.8954
degree 14: train MSE 0.0000 test MSE 191406446.6219
Double descent
Classical intuition says to stop adding parameters once the test error rises. But when the number of parameters passes the number of training points , the model can fit the data exactly in infinitely many ways, and the training algorithm picks a particular one: gradient descent from zero converges to the minimum-norm interpolating solution. Its test error peaks at the interpolation threshold and then falls again as grows.
import numpy as np
rng = np.random.default_rng(1)
n, d = 40, 5 # 40 training points in 5-D
W = rng.normal(size=(d, 1000)) / np.sqrt(d) # random ReLU features
beta = rng.normal(size=d) / np.sqrt(d)
target = lambda X: 2 * np.tanh(X @ beta)
feats = lambda X, p: np.maximum(0, X @ W[:, :p])
X_te = rng.normal(size=(1000, d))
ps = (5, 20, 30, 36, 40, 44, 50, 80, 200, 1000)
err = {p: [] for p in ps}
for trial in range(20): # average over 20 training sets
X_tr = rng.normal(size=(n, d)); y_tr = target(X_tr) + rng.normal(0, 0.2, n)
for p in ps:
w = np.linalg.pinv(feats(X_tr, p)) @ y_tr # minimum-norm least squares
err[p].append(np.mean((feats(X_te, p) @ w - target(X_te)) ** 2))
for p in ps:
print(f"p = {p:4d} features: mean test MSE {np.mean(err[p]):8.3f}")p = 5 features: mean test MSE 0.275
p = 20 features: mean test MSE 0.131
p = 30 features: mean test MSE 0.300
p = 36 features: mean test MSE 1.004
p = 40 features: mean test MSE 29.479
p = 44 features: mean test MSE 1.489
p = 50 features: mean test MSE 0.530
p = 80 features: mean test MSE 0.313
p = 200 features: mean test MSE 0.229
p = 1000 features: mean test MSE 0.194
The test error falls, rises sharply as approaches (where the fit must pass through all 40 noisy points with a huge-norm solution), and falls again far beyond it.
Why deep networks generalize
Networks large enough to memorize random labels still generalize on real data. Part of the explanation is implicit regularization: SGD's noise and the minimum-norm bias of gradient methods select simple solutions among the many that fit. Explicit regularization (weight decay, dropout, data augmentation, early stopping) adds to it. Capacity, in the end, is not just a parameter count.
In software
- Always hold out a validation set; tune hyperparameters on it and report the test set once.
- Learning curves (loss versus epochs for training and validation) show overfitting as a widening gap; early stopping keeps the checkpoint with the best validation loss.
- LLM pre-training rarely overfits because each token is seen about once; scaling laws describe its test loss instead.
Exercises
Exercise 1: Diagnose
Training accuracy 99%, validation accuracy 70%. Under- or overfitting, and two remedies?
Answer
Overfitting; for example more data or augmentation, and stronger regularization or early stopping.
Solution
A large train-validation gap means variance dominates. Any measure that limits effective capacity or adds data narrows it.
Exercise 2: Interpolation threshold
A linear model with features is fitted to points by least squares. For which can it fit any labels exactly (generic data)?
Answer
.
Solution
Exact fit needs solvable for every , which requires rank , so at least columns.
Exercise 3: Minimum norm (code)
For an underdetermined system with 2 equations and 4 unknowns, check that pinv gives the solution with the smallest norm among exact solutions.
Answer
Adding any null-space vector keeps the fit but increases the norm.
Solution
import numpy as np
rng = np.random.default_rng(2)
A = rng.normal(size=(2, 4)); b = rng.normal(size=2)
w = np.linalg.pinv(A) @ b
null = np.linalg.svd(A)[2][2:] # basis of the null space
w2 = w + null.T @ np.array([0.5, -0.3])
print(np.allclose(A @ w, b), np.allclose(A @ w2, b), np.linalg.norm(w) < np.linalg.norm(w2))True True True
Test yourself
Low training error and much higher test error indicate…
- underfitting
- overfitting
- a bug in the loss
- perfect generalization
In double descent, the test error peaks when…
- the model has 1 parameter
- the number of parameters is about the number of training points
- the learning rate is zero
- the data is noise-free
Gradient descent from zero on an underdetermined least-squares problem converges to…
- a random solution
- the minimum-norm solution
- no solution
- the largest-norm solution