Key ideas
- Training error is computed on the data the model was fitted to, so it is optimistically biased. Generalization error is the expected error on new data from the same distribution and can only be estimated, on data the model never saw1. All guarantees assume IID data1. Dependent examples (several scans per patient, consecutive time steps) must therefore be split by group or by time, or the test error is misleading.
- Underfitting: training and validation error both high, with a small gap (model too simple). Overfitting: training error far below validation error1. With a fixed model, less data makes overfitting more likely; more data almost never hurts1. To check whether more data would help, retrain on growing subsets and see if validation error is still falling (a learning curve).
- Zero training error is cheap: a polynomial of high enough degree fits any training set1 (degree already passes through points with distinct , since the Vandermonde system is invertible), and 1-NN memorises any labelling3. Zero test error is possible only if the labels are a noise-free function of the input and the model has learned it.
- Bias–variance: expected squared error = squared bias + variance + irreducible noise5. Simple models have high bias; flexible ones have high variance. Regularisation and larger in -NN trade variance for bias; bagging cuts variance, boosting cuts bias5.
- Honest evaluation. Choose models on a validation set or by -fold cross-validation, and touch the test set as rarely as possible1. Designing a new model after looking at test results leaks information (adaptive overfitting). Scoring many models on one test set makes a lucky winner likely (false discovery)2.
- -fold CV trains fresh models, each on folds, and averages the validation scores1. Each model sees only examples, so the estimate is slightly pessimistic for the final model trained on all . Leave-one-out () is nearly unbiased but costly. A grid of settings costs fits before the final refit.
- VC dimension: the largest such that some set of points can be shattered, i.e. labelled in all ways by functions of the class2,4. One shattered set of points proves VC , but showing that VC is exactly also needs an argument that no set of points can be shattered.
- VC bounds give test error training error , useful only when VC 2,4. Structural risk minimisation picks the model with the smallest such bound. VC dimension only looks at the sign of , not the size of weights or the margin.
- Deep networks can fit random labels, so VC-type bounds are vacuous for them, yet they generalize well3. Test error versus model size can show double descent: down, up near the point where the model just fits the training data, then down again3.
- Early stopping limits the number of epochs instead of the weights: stop when validation error has not improved by more than a small for a set number of epochs ("patience"). It matters with label noise; on clean, realizable data it helps little3.
Formulas
- Bias–variance at input : 5.
- Test-set error on fresh examples, true error : standard deviation 2.
- Hoeffding: , so examples give tolerance with confidence 2. That bounds one direction; a guarantee for needs .
- independent evaluations, each misleading with probability : at least one is misleading with probability .
- VC dimensions: threshold on : 1; interval: 2; linear classifier with offset in : (a line in the plane: 3)2,4; with on : (linear on ); axis-aligned rectangles: 4 (a diamond of 4 points is shattered; of any 5 points, at most 4, one extreme per side, fix the bounding box, and a remaining point inside it cannot be excluded)6; 1-NN (it memorises any labelling3) and 4: infinite; a finite class: at most 4.
- Perceptron on data separable through the origin with margin , all : at most updates, whichever misclassified point is used at each step6.
Worked example
How big must a test set be to know a classifier's error within with 95% confidence?
- Hoeffding, with d2l's recipe (): . Covering both directions, instead of , gives about 4612.
- CLT, worst case , two standard deviations: gives . The finite-sample Hoeffding bound is the more conservative of the two.
- If you then compare 10 models on that test set, the chance that at least one score is off by more than is up to (treating the evaluations as independent).
In code
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
rng = np.random.default_rng(0)
x = rng.uniform(-3, 3, size=(30, 1))
y = np.sin(x[:, 0]) + rng.normal(0, 0.3, size=30)
for d in [1, 3, 12]:
model = make_pipeline(PolynomialFeatures(d), LinearRegression())
train = model.fit(x, y).score(x, y) # R^2 on the training data
cv = cross_val_score(model, x, y, cv=5).mean() # 5 fresh fits, mean R^2
print(d, round(train, 2), round(cv, 2)) # 1 0.54 -0.29 / 3 0.87 0.56 / 12 0.91 -33.57
Degree 1 underfits, degree 3 fits, degree 12 overfits: its training score is the best and its cross-validated score the worst.
Common pitfalls
- An infinite VC dimension (RBF SVM, 1-NN) does not make a model worse in practice; the bound is only an upper bound.
- Zero training error does not imply zero test error, and a high VC dimension does not imply poor generalization.
- Cross-validation must refit a fresh model in every fold; continuing to train one model across folds leaks the later validation folds into training.
- Shuffling dependent data (time series, repeated measurements of one subject) before splitting inflates the score.
- Early stopping is not a cure-all: on noise-free, separable data it brings little3.
- Reusing the test set for model choice turns it into a validation set; its score is then optimistic.
References
- Dive into Deep Learning — Sec. 3.6 Generalization (3.6.1 Training Error and Generalization Error; 3.6.2 Underfitting or Overfitting?, incl. 3.6.2.1 Polynomial Curve Fitting and 3.6.2.2 Dataset Size; 3.6.3 Model Selection; 3.6.3.1 Cross-Validation; 3.6.4 Summary)
- Dive into Deep Learning — Sec. 4.6 Generalization in Classification (4.6.1 The Test Set; 4.6.2 Test Set Reuse; 4.6.3 Statistical Learning Theory)
- Dive into Deep Learning — Sec. 5.5 Generalization in Deep Learning (5.5.1 Revisiting Overfitting and Regularization; 5.5.2 Inspiration from Nonparametrics; 5.5.3 Early Stopping)
- Wikipedia: Vapnik–Chervonenkis dimension — Definitions; Examples; Uses — In statistical learning theory; Bounds
- Wikipedia: Bias–variance tradeoff — Bias–variance decomposition of mean squared error; Approaches
- Blum, Hopcroft & Kannan, Foundations of Data Science (draft of 4 Jan 2018) — Sec. 5.2 The Perceptron algorithm, Theorem 5.1, pp. 130–131; Sec. 5.11.1 Definitions and Key Theorems (axis-parallel rectangles), p. 149; Sec. 5.11.2 Examples, p. 151