Generalization in Classification

Dive into Deep Learning · §3.5

Generalization in Classification: Error Bars, Test Reuse & VC Theory
empirical vs. population error · CLT & Hoeffding intervals · union bound · adaptive overfitting · shattering & VC dimension

Two Obstacles Between Measured and True Accuracy

Why Generalization Needs Quantitative Theory

Our Fashion-MNIST classifier scored \sim 83\% on 10{,}000 validation images and \sim 84\% on 60{,}000 training images. How far can we trust either number on future garments?

  • Finite-sample uncertainty & test reuse: Even an untouched test set of n = 10{,}000 images has statistical error bars—and querying that split repeatedly to select models erodes those guarantees.
  • Memorization vs. learning: A lookup-table memorizer achieves 100\% training accuracy (0\% error) while guessing randomly on new images. Why does low training error certify a linear model when a memorizer’s zero error certifies nothing?

Empirical vs. Population Error of a Fixed Classifier

Test Error as a Binomial Average

Fix a single classifier f before drawing an i.i.d. test set \mathcal{D} = \{(\mathbf{x}^{(i)}, y^{(i)})\}_{i=1}^n \sim P(X, Y):

\epsilon_{\mathcal{D}}(f) = \frac{1}{n}\sum_{i=1}^n \mathbf{1}\!\left(f(\mathbf{x}^{(i)}) \neq y^{(i)}\right), \qquad \epsilon(f) = P\!\left(f(X) \neq Y\right)

  • Unbiased Bernoulli average: Each indicator is an independent \mathrm{Bernoulli}(\epsilon(f)) trial with \mathbb{E}[\epsilon_{\mathcal{D}}(f)] = \epsilon(f) and standard error: \mathrm{SE}\big(\epsilon_{\mathcal{D}}(f)\big) = \sqrt{\frac{\epsilon(f)(1 - \epsilon(f))}{n}} \le \frac{0.5}{\sqrt{n}}
  • Benchmark (\epsilon \approx 0.17, n = 10\text{k}): \mathrm{SE} \approx 0.38\%; halving it requires 4\times more test data.

Asymptotic 95% Intervals via the Central Limit Theorem

What n = 10,000 Test Examples Measure

By the Central Limit Theorem, \epsilon_{\mathcal{D}}(f) lies within 1.96 standard errors of \epsilon(f) with probability \approx 0.95:

P\!\left(\big|\epsilon_{\mathcal{D}}(f) - \epsilon(f)\big| \le 1.96\sqrt{\frac{\epsilon(f)(1 - \epsilon(f))}{n}}\right) \approx 0.95

  • Fashion-MNIST radius (\epsilon = 0.17, n = 10\text{k}): r_{\text{normal}} = 1.96 \times 0.0038 \approx \pm 0.7\%.
  • Reading n = 10{,}000: A worst-case (\epsilon = 1/2) radius \le \pm 1\% requires n \ge (1.96 \times 0.5 / 0.01)^2 = 9{,}604 test images, so the 10{,}000-image test sets of MNIST, Fashion-MNIST, and CIFAR-10 pin accuracy to about one point.

Finite-Sample Guarantees via Hoeffding’s Inequality

Distribution-Free Concentration

When \epsilon_{\mathcal{D}}(f) = 0 on a small test set, the plug-in normal radius collapses to 0. Hoeffding’s inequality gives an exact finite-sample bound for every n and \epsilon(f) \in [0, 1]:

P\!\left(\big|\epsilon_{\mathcal{D}}(f) - \epsilon(f)\big| \ge t\right) \le 2\exp\!\left(-2nt^2\right)

  • Exact (1 - \delta) confidence radius: r_{\text{Hoeffding}} = \sqrt{\frac{\log(2/\delta)}{2n}}
  • Cost of a finite-sample guarantee: At 95\% confidence (\delta = 0.05) and r \le 0.01, Hoeffding requires n \ge 18{,}445 (\approx 2\times the normal sample size 9{,}604; both assume variance 1/4, but the exponential tail bound is looser than the normal quantile).

Empirical Coverage: Normal vs. Hoeffding Intervals

Monte Carlo Simulation Across Test-Set Sizes

Simulating 10{,}000 independent test sets at each size n \in [100, 10{,}000] for \epsilon = 0.17 and \epsilon = 0.01 compares actual 95\% interval coverage:

  • Moderate error (\epsilon = 0.17, blue): The plug-in normal interval (solid) stays close to 95\% coverage at every n, while Hoeffding (dashed) is conservative (coverage above 99.9\%).
  • Near-zero error (\epsilon = 0.01, orange): For n \le 200, \sim 13\%\text{--}37\% of test sets observe zero mistakes (\epsilon_{\mathcal{D}} = 0), causing the plug-in normal interval to undercover down to \sim 63\%.
  • Hoeffding holds everywhere: Always stays above the 0.95 dotted line.

Comparing k Fixed Classifiers: The Union Bound

Selection Bias on a Shared Test Set

Evaluating k fixed models f_1, \ldots, f_k on the same test set \mathcal{D} multiplies the failure probability by at most k (Boole’s union bound):

P\!\left(\max_{1 \le j \le k} \big|\epsilon_{\mathcal{D}}(f_j) - \epsilon(f_j)\big| \ge t\right) \le 2k\exp\!\left(-2nt^2\right)

Setting the right-hand side to \delta yields the simultaneous confidence radius:

r(k, n, \delta) = \sqrt{\frac{\log(2k/\delta)}{2n}}

  • Benign \sqrt{\log k} growth: At n = 10{,}000, \delta = 0.05, the radius grows gently from 1.36\% (k = 1) to 2.30\% (k = 10^3) and 2.96\% (k = 10^6).

How Selection Bias Inflates the Best of k Coin Flippers

Cherry-Picking the Luckiest Model

Even when all k models are fixed before inspecting \mathcal{D}, reporting \max_{1 \le j \le k} \text{acc}_{\mathcal{D}}(f_j) cherry-picks the luckiest draw:

  • Experiment (n = 1{,}000 labels, k = 10{,}000 random guessers): Every single classifier flips an independent fair coin (true population accuracy = 0.50).
  • Logarithmic inflation: The best test score among the first k random guessers climbs steadily along 0.5 + \sqrt{\frac{\log k}{2n}}, reaching \approx 0.56 at k = 10{,}000 with zero actual signal learned.

Adaptive Overfitting Breaks the Union Bound

When Past Test Scores Guide New Models

When engineers use test-set feedback to design the next model (adaptive reuse), overfitting breaks the \sqrt{\log k} bound:

kept = guesses[:1000][accuracy[:1000] > 0.5]  # guessers that scored above 0.5
vote = kept.mean(axis=0) > 0.5                # their majority vote
print(f'majority vote: {(vote == labels).mean():.3f}, '
      f'best single guesser: {accuracy[:1000].max():.3f}')
majority vote: 0.709, best single guesser: 0.564
  • From 50\% to \sim 70\% in 1{,}000 queries: Majority-voting the coin-flippers that scored > 0.50 on the test set reaches \sim 70\% test accuracy while remaining at 50\% on fresh data.
  • Three-way split discipline: Train on train, select on val, and touch test once.

Uniform Convergence Over a Hypothesis Class

Statistical Learning Theory

Because the trained model f_{\mathcal{S}} \in \mathcal{F} depends on the training set \mathcal{S}, certifying \epsilon(f_{\mathcal{S}}) requires bounding the worst-case gap across all f \in \mathcal{F} simultaneously (uniform convergence):

\sup_{f \in \mathcal{F}} \big|\epsilon_{\mathcal{S}}(f) - \epsilon(f)\big|

  • Finite hypothesis classes (|\mathcal{F}| < \infty): By the union bound over k = |\mathcal{F}|, with probability \ge 1 - \delta for all f \in \mathcal{F}: \epsilon(f) \le \epsilon_{\mathcal{S}}(f) + \sqrt{\frac{\log(2|\mathcal{F}|/\delta)}{2n}}

From Infinite Function Classes to Finite Labelings

Why Memorizers Fail and Linear Models Generalize

What if |\mathcal{F}| = \infty, such as continuous linear halfspaces f(\mathbf{x}) = \operatorname{sign}(\mathbf{w}^\top\mathbf{x} + b) on \mathbb{R}^d?

  • Finite dichotomies on n points: On any n points \mathbf{x}^{(1)}, \ldots, \mathbf{x}^{(n)}, there are at most 2^n binary \pm 1 labelings (Vapnik & Chervonenkis, 1971).
  • Shattering & VC dimension: \mathcal{F} shatters m points if it realizes all 2^m binary labelings on them; the VC dimension v = \mathrm{VC}(\mathcal{F}) is the largest m that \mathcal{F} can shatter (v = \infty for unrestricted memorizers).

Shattering in the Plane: Lines Have VC Dimension 3

Geometric Intuition for VC Dimension

Consider oriented linear halfplanes f(\mathbf{x}) = \operatorname{sign}(w_1 x_1 + w_2 x_2 + b) in \mathbb{R}^2:

  • \mathrm{VC} \ge 3 (left): Any 3 non-collinear points in \mathbb{R}^2 can be partitioned in all 2^3 = 8 ways by a straight line.
  • \mathrm{VC} < 4 (middle & right): No set of 4 points in \mathbb{R}^2 can be shattered. If the 4 points form a convex quadrilateral (middle), a straight line cannot separate opposite diagonal corners (+ vs. -); if one point lies inside the triangle of the other three (right), any halfplane containing the three vertices contains the interior point. Thus \mathrm{VC} = 3.

VC Dimension of Linear Classifiers in d Dimensions

Capacity Without Counting Parameters Blindly

In \mathbb{R}^d, linear halfspaces f(\mathbf{x}) = \operatorname{sign}(\mathbf{w}^\top\mathbf{x} + b) have exact VC dimension:

\mathrm{VC}\big(\text{linear halfspaces in }\mathbb{R}^d\big) = d + 1

  • Lower bound (v \ge d + 1): The origin \mathbf{0} and the d standard basis vectors \mathbf{e}_1, \ldots, \mathbf{e}_d are shattered independently.
  • Upper bound (v \le d + 1, Radon’s theorem): Any d + 2 lifted points [\mathbf{x}_i, 1] \in \mathbb{R}^{d+1} are linearly dependent, splitting into two subsets whose intersecting convex hulls cannot be separated by any hyperplane.
  • VC \neq parameter count: f_\omega(x) = \operatorname{sign}(\sin(\omega x)) has 1 parameter but v = \infty.

Sauer’s Lemma and the VC Generalization Bound

Polynomial Growth of Dichotomies

Why does finite VC dimension v < \infty guarantee uniform convergence?

  • Sauer–Shelah lemma: For n \ge v, the number of distinct binary labelings \mathcal{F} can induce on any n points is bounded by \sum_{i=0}^v \binom{n}{i} \le (en/v)^v = O(n^v) instead of 2^n.
  • VC generalization bound: With probability \ge 1 - \delta, simultaneously for every f \in \mathcal{F}: \epsilon(f) \;\le\; \epsilon_{\mathcal{S}}(f) + c\sqrt{\frac{v + \log(1/\delta)}{n}}

Strengths and Limits of VC Theory in Deep Learning

Distribution-Free Loose Bounds vs. Held-Out Evaluation

What does the VC bound tell us about Fashion-MNIST—and about deep networks?

  • Why linear models cannot memorize (n \gg d): For one halfspace with d = 784, v = 785, n = 60{,}000, \sqrt{v/n} \approx 0.114 bounds the gap under any distribution, up to the constant c (observed gap is even tighter, \sim 0.01).
  • Vacuousness for deep nets: Overparameterized nets can fit random labels on n points (v \ge n, \sqrt{v/n} \ge 1), so sharp certificates in deep learning come post-training from an untouched held-out test set (\pm 0.7\% at n = 10{,}000).

Concept Check: Sample Sizing, Zero Errors & VC Theory

Concept Check

Test your command of test-set statistics and VC theory on three problems:

  1. Quartering the confidence radius: How many test examples n are required to guarantee a 95\% confidence radius of at most \pm 0.5 percentage points (t = 0.005) in the worst case (\epsilon = 1/2) under the normal approximation versus Hoeffding’s inequality?
  2. The “Rule of Three” for zero test errors (Exercise 2): If a classifier makes 0 errors on a test set of n = 100 examples, why is the plug-in normal interval 0 \pm 0 misleading, and for what population error \epsilon does (1 - \epsilon)^n \le e^{-n\epsilon} \le 0.05 hold?
  3. Quadratic decision boundaries: What is the VC dimension of quadratic classifiers f(\mathbf{x}) = \operatorname{sign}(w_0 + w_1 x_1 + w_2 x_2 + w_3 x_1^2 + w_4 x_1 x_2 + w_5 x_2^2) in the plane \mathbb{R}^2?

Answers: (1) Halving the radius from 0.01 to 0.005 multiplies n by 4: normal needs (1.96 \times 0.5 / 0.005)^2 = 38{,}416 examples; Hoeffding needs \log(40)/(2 \times 0.005^2) \approx 73{,}778. (2) At 0 observed errors, the plug-in variance vanishes; solving e^{-n\epsilon} \le 0.05 \iff \epsilon \ge \frac{\log 20}{n} \approx \frac{3}{n} gives the [0,\; 3/n] rule of three ([0, 3\%] at n = 100, which is tighter than Hoeffding’s O(1/\sqrt{n}) bound \approx 13.6\%). (3) It is a linear halfspace in the 5-dimensional lifted feature map \phi(x_1, x_2) = (x_1, x_2, x_1^2, x_1 x_2, x_2^2) plus bias w_0, so \mathrm{VC} = 5 + 1 = 6.

Summary: Generalization Guarantees in Classification

Section Summary

Regime / Setting Error Bound / Radius (95\% Confidence, \delta = 0.05) Scaling Law & Practical Lesson
Single fixed model (Normal CLT) \big|\epsilon_{\mathcal{D}}(f) - \epsilon(f)\big| \le 1.96\sqrt{\epsilon(1-\epsilon)/n} \le 0.98/\sqrt{n} \pm 0.7\% on Fashion-MNIST (n = 10\text{k}); n \ge 9{,}604 for worst-case \pm 1\%
Single fixed model (Hoeffding) \big|\epsilon_{\mathcal{D}}(f) - \epsilon(f)\big| \le \sqrt{\log(2/\delta)/(2n)} Exact for all n, \epsilon \in [0, 1]; n \ge 18{,}445 for \pm 1\% (\approx 2\times normal n)
Best of k fixed models (Union) \max_{1\le j\le k}\big|\epsilon_{\mathcal{D}}(f_j) - \epsilon(f_j)\big| \le \sqrt{\log(2k/\delta)/(2n)} Grows slowly as \sqrt{\log k}; best of 10\text{k} fair-coin guessers reaches \approx 0.56
Adaptive test-set reuse Breaks union bound (can overfit in O(n) queries) Majority vote of 1\text{k} filtered guessers hits \sim 0.70; tune on validation split only
Finite hypothesis class \mathcal{F} \sup_{f\in\mathcal{F}}\big|\epsilon_{\mathcal{S}}(f) - \epsilon(f)\big| \le \sqrt{\log(2|\mathcal{F}|/\delta)/(2n)} Uniform convergence across all f \in \mathcal{F}, including trained f_{\mathcal{S}}
VC dimension v = \mathrm{VC}(\mathcal{F}) \epsilon(f) \le \epsilon_{\mathcal{S}}(f) + c\sqrt{(v + \log(1/\delta))/n} v = d + 1 for linear classifiers in \mathbb{R}^d; v = \infty for memorizers; vacuous for deep nets