A model is only useful if it works on data it has not seen. Generalization is the gap between performance on the training set and performance on fresh samples from the same distribution, and nearly everything in practical machine learning (splits, validation, penalties, dropout, augmentation) is about measuring that gap honestly or shrinking it. I come back to this page whenever a training curve looks too good.

Overfitting is memorizing the homework instead of learning the subject. Image: すじにくシチュー, CC0, via Wikimedia Commons.

Mental Model

  • Training error always flatters the model. Only data the fitting process never touched gives an honest estimate.
  • Error splits into bias (the model family cannot represent the truth), variance (the fit changes a lot between training samples) and irreducible noise.
  • Regularization trades a little extra bias for a large cut in variance. L2 shrinks weights, L1 zeroes some of them, dropout and early stopping act on the training process.
  • Leakage, not overfitting, is the most common reason a result is wrong. Anything that lets test information into training inflates the score.
  • Classical theory says a U-shaped test curve. Modern overparameterized models can break that picture in specific, well-studied ways.

1. Bias and Variance

For squared error at a fixed input , with , , and a predictor trained on a random training set:

The expectation is over training sets, which is why you cannot measure the pieces from a single fit. A low-degree polynomial fitted to a sine wave is stiff: every training set gives nearly the same wrong curve (high bias, low variance). A degree-12 polynomial through 15 noisy points follows each noise bump, so a new sample produces a very different curve (low bias, high variance). The noise term is a floor no model can beat.

The decomposition is exact for squared error. For other losses, such as cross-entropy, there are analogues but no equally clean split, so treat “bias versus variance” there as a guide to intuition.

2. Train, Validation and Test

Hold out data in three roles. The training set fits the parameters. The validation set chooses hyperparameters, architecture and when to stop. The test set is touched once at the end to report performance. Every decision made by looking at a score turns that data into training data for the decision, so reusing the test set to pick between models quietly overfits to it.

Leakage is any path by which held-out information reaches training. Typical ones:

  • Fitting a scaler, imputer, PCA or feature selector on the full dataset before splitting. Fit transforms on the training fold only, then apply them to the rest.
  • Duplicates or near-duplicates across splits, for example several crops of one image, or multiple rows for one patient or one user. Split by group, not by row.
  • Random splits of time series. Predicting the past from the future looks great and fails in deployment, so split by time.
  • Features computed from the label or from the future, such as a “days until churn” column.

Common mistake

Standardizing the whole dataset, then cross-validating. The mean and variance of the validation folds leaked into every training fold. The effect is usually small for scaling and can be huge for supervised feature selection.

3. Cross-Validation

With little data, a single validation split is noisy. In -fold cross-validation you split into parts, train on , validate on the remaining one, rotate, and average the scores. Common values are or . The mean estimates expected performance and the spread across folds tells you how much to trust it. Use stratified folds for imbalanced classes, grouped folds when rows share an entity, and forward-chaining folds for time. When you also tune hyperparameters, use nested CV (an inner loop to tune, an outer loop to evaluate), otherwise the reported score is optimistic. For large neural nets a single validation split is the norm, since trainings are too expensive.

4. L2 and L1 Regularization

Add a penalty on the weights to the loss. For linear regression:

Ridge has the closed form . The term makes the matrix invertible and damps directions of small singular value, which is exactly where the variance lives. Probabilistically, ridge is the MAP estimate with a Gaussian prior on the weights and lasso is the MAP estimate with a Laplace prior.

The geometry explains the difference. Minimizing the loss subject to means finding where an elliptical loss contour first touches the constraint region. For the region is a disk, with no preferred direction, so the touching point has generically nonzero coordinates. For it is a diamond with corners on the axes. Elliptical contours tend to touch a corner, where some coordinates are exactly zero. So lasso gives sparse solutions and acts as feature selection, while ridge shrinks everything smoothly. With strongly correlated features lasso picks one more or less arbitrarily, and ridge spreads weight across them.

In a neural network, L2 on the weights is called weight decay (with the Adam caveat in Gradient Descent and Optimizers).

5. Dropout, Early Stopping, Augmentation

Dropout zeroes each hidden activation with probability during training. With “inverted dropout” the surviving units are scaled by so that nothing needs to change at test time. It prevents units from co-adapting, and it can be viewed as training an ensemble of thinned networks that share weights. It is switched off at inference.

Early stopping watches validation loss during training and keeps the weights from its minimum, usually with a patience window of a few epochs. For gradient descent from a small initialization, the weights grow gradually, so stopping early limits the effective model size. For linear models this acts much like an L2 penalty.

Data augmentation adds label-preserving transformations to the training set: flips, crops and color jitter for images, noise or time-shifts for signals, back-translation for text. It injects prior knowledge about which changes should not alter the answer. It only works when the transform truly preserves the label (flipping a “6” into a “9” does not).

6. Double Descent

The classical story has test error forming a U as capacity grows. Belkin and colleagues (2019) described a different shape for some model families: as capacity rises, test error peaks near the interpolation threshold, where the model has just enough parameters to fit the training set exactly, then falls again as the model grows further. It has been observed in random-feature models and minimum-norm linear regression, and in some neural networks. It is not a claim that bigger is always better. The peak depends on label noise and is damped or removed by regularization, and it also depends on how you count capacity (parameters, training epochs or data size). Among interpolating solutions, the ones chosen by gradient descent tend to have small norm, which gives a reason the second descent can be benign. Treat it as a caution against reading a single U-shaped curve as law.

7. Polynomial-Fit Demo

import numpy as np
 
rng = np.random.default_rng(1)
f = lambda x: np.sin(2 * np.pi * x)
xtr = rng.uniform(0, 1, 15);  ytr = f(xtr) + rng.normal(0, 0.2, 15)
xte = rng.uniform(0, 1, 500); yte = f(xte) + rng.normal(0, 0.2, 500)
 
def fit(deg, lam=0.0):
    V = np.vander(xtr, deg + 1, increasing=True)
    # ridge as an augmented least-squares problem (stable even for lam=0)
    A = np.vstack([V, np.sqrt(lam) * np.eye(deg + 1)])
    return np.linalg.lstsq(A, np.r_[ytr, np.zeros(deg + 1)], rcond=None)[0]
 
mse = lambda w, x, y: np.mean((np.vander(x, len(w), increasing=True) @ w - y) ** 2)
 
for deg in [1, 3, 5, 9, 12]:
    w = fit(deg)
    print(f"degree {deg:2d}  train {mse(w, xtr, ytr):.3f}  test {mse(w, xte, yte):.3f}")
for lam in [1e-6, 1e-3, 1e-1]:
    w = fit(12, lam)
    print(f"degree 12, lambda={lam:g}  train {mse(w, xtr, ytr):.3f}  test {mse(w, xte, yte):.3f}")

Running it (noise variance is , so test MSE cannot go much below that):

ModelTrain MSETest MSE
degree 10.2980.259
degree 30.0160.069
degree 50.0160.068
degree 90.0072.808
degree 120.0035679.7
degree 12, 0.0120.116
degree 12, 0.0230.071
degree 12, 0.1490.149

Train error falls monotonically with degree while test error is U-shaped with a violent right arm: the degree-12 curve is wild between and beyond the 15 training points. The same degree-12 model with a tiny ridge penalty lands at 0.071, close to the degree-3 fit, and an over-large penalty () underfits again. The exact test numbers for the high degrees depend strongly on the random draw of training points, so read them as a pattern, not as constants.