Skip to content
ml-recall

Topic notes

Learning Theory

Why and when models generalize - training vs. test error, bias-variance, honest evaluation (test sets, cross-validation), VC dimension and its limits for deep networks.

4-min read AI-checked

Practise 42 questions Take a test

Key ideas

Formulas

Worked example

How big must a test set be to know a classifier's error within ±0.02\pm 0.02 with 95% confidence?

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

References

  1. 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)
  2. 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)
  3. 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)
  4. Wikipedia: Vapnik–Chervonenkis dimension — Definitions; Examples; Uses — In statistical learning theory; Bounds
  5. Wikipedia: Bias–variance tradeoff — Bias–variance decomposition of mean squared error; Approaches
  6. 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