Simple Algorithm
Machine Learning · Descent · 24 min

Learning Theory: How Many Examples Are Enough?

PAC learning, VC dimension and uniform convergence.

Fit a model to a thousand examples and it can still fail on the next one. Learning theory turns that worry into arithmetic: it says how many examples guarantee that a good fit on the sample means a good fit on the world, and the answer depends on how many labelings a model class can produce, not on how many parameters it has.

What you’ll learn

  • Define PAC learnability in the realizable and agnostic settings, and say exactly what ε\varepsilon, δ\delta and "for every distribution" commit you to.
  • Derive the sample complexity of a finite class: (ln⁡∣H∣+ln⁡(1/δ))/ε(\ln\abs{\mathcal H}+\ln(1/\delta))/\varepsilon when a perfect hypothesis exists, and order ln⁡(∣H∣/δ)/ε2\ln(\abs{\mathcal H}/\delta)/\varepsilon^2 when none does.
  • Compute VC dimensions by exhibiting a shattered set and proving that no larger set is shattered.
  • State the fundamental theorem of statistical learning, use a VC bound to estimate sample sizes, and recognize when such bounds are vacuous.

From one hypothesis to many

You train a classifier on nn labeled examples drawn i.i.d. from an unknown distribution PP, with labels y∈{0,1}y\in\{0,1\}, and it makes few mistakes on them. What does that say about its risk R(h)=Pr⁡(x,y)∼P[h(x)≠y]R(h)=\Pr_{(\mathbf x,y)\sim P}[h(\mathbf x)\ne y], its error rate on new data? For a single hypothesis fixed before the data arrive, the empirical risk R^n(h)\hat R_n(h) is an average of nn independent coin flips with bias R(h)R(h), and Hoeffding's inequality pins it down: Pr⁡[∣R^n(h)−R(h)∣>ε]≤2e−2nε2\Pr\big[\abs{\hat R_n(h)-R(h)}>\varepsilon\big]\le2e^{-2n\varepsilon^2}.

Learning breaks that argument. Empirical risk minimization (ERM) searches a hypothesis class H\mathcal H and returns the hypothesis with the fewest training mistakes, so the winner is chosen because it did well on these particular examples. Part of its success is luck, and luck does not carry over to new data.

Predict before you look

A class contains 1,000 classifiers that each output a fair coin flip for every input, independently, and the 100 training labels are themselves coin flips. Every classifier has true error exactly 50%. What training error do you expect the best of the 1,000 to show?

Show the answer

0.34

Each training error is Binomial(100,12)/100\mathrm{Binomial}(100,\tfrac12)/100, with standard deviation 0.050.05. The minimum of 1,000 independent copies sits about three standard deviations below the mean: its expectation is 0.3390.339. ERM would report a classifier that looks 66% accurate and is exactly as good as guessing. With 1,000 training examples the best of 1,000 shows about 0.450.45: more data shrinks the luck.

To trust ERM you need uniform convergence: with high probability, ∣R^n(h)−R(h)∣≤ε\abs{\hat R_n(h)-R(h)}\le\varepsilon for every h∈Hh\in\mathcal H at once. Then whichever hypothesis ERM picks, its training error is an honest estimate of its risk. This lesson asks how large nn must be, as a function of H\mathcal H, for that to hold. (Learning-theory texts often write mm for the sample size; this course writes nn.)

Probably approximately correct

Read the quantifiers one at a time. Approximately correct is the ε\varepsilon: no finite sample can pin down a classifier exactly. Probably is the δ\delta: some training sets are unrepresentative (all examples from one corner of the input space, say), no learner can be blamed for those, so a small chance of failure is allowed. For every distribution makes the guarantee distribution-free: the same nn must work for the worst PP, which is why these bounds are often pessimistic for the data you actually have.

The realizable case assumes some h⋆∈Hh^\star\in\mathcal H is perfect, a strong assumption the agnostic case drops. The learner may return a hypothesis outside H\mathcal H (improper learning). Valiant also required running time polynomial in 1/ε1/\varepsilon, 1/δ1/\delta and the input size; the statistical question (how many examples) and the computational one (how much time) can come apart dramatically, as The Computational Hardness of Learning shows.

Check your understanding

A learner comes with a PAC guarantee: with n=2,000n=2{,}000 examples it achieves ε=0.05\varepsilon=0.05, δ=0.01\delta=0.01 for its class (realizable case). Which statement follows?

  1. On 99% of test points, the returned classifier is wrong at most 5% of the time
  2. If you redrew the 2,000 training examples and retrained many times, at least 99% of the returned classifiers would have error at most 5%, whatever the input distribution
  3. The returned classifier has error exactly 5% with probability 99%
  4. The returned classifier makes at most 100 mistakes on its own training set
Show the answer

B. If you redrew the 2,000 training examples and retrained many times, at least 99% of the returned classifiers would have error at most 5%, whatever the input distribution

δ\delta is a probability over training sets: a small fraction of samples may mislead the learner. ε\varepsilon bounds the error of the returned classifier on the distribution. The guarantee must hold for every distribution of inputs (with labels from some h⋆h^\star in the class).

Finite classes: refuting bad hypotheses

DerivationProof
  1. Pr⁡[R^n(h)=0]=(1−R(h))n<(1−ε)n≤e−εn(1−x≤e−x)\Pr\big[\hat R_n(h)=0\big]=(1-R(h))^n<(1-\varepsilon)^n\le e^{-\varepsilon n}\qquad(1-x\le e^{-x})

    Call hh bad if R(h)>εR(h)>\varepsilon. Each training example independently exposes a bad hh with probability R(h)>εR(h)>\varepsilon, so it survives all nn examples with probability

  2. Pr⁡[∃ h bad: R^n(h)=0]≤∣H∣ e−εn\Pr\big[\exists\,h\text{ bad}:\ \hat R_n(h)=0\big]\le\abs{\mathcal H}\,e^{-\varepsilon n}

    ERM returns some hypothesis with zero training error (h⋆h^\star has none, so one exists). It can fail only if a bad hypothesis survives. A union bound over at most ∣H∣\abs{\mathcal H} bad hypotheses gives

  3. ∣H∣e−εn≤δ  ⟺  n≥ln⁡∣H∣+ln⁡(1/δ)ε\abs{\mathcal H}e^{-\varepsilon n}\le\delta\iff n\ge\frac{\ln\abs{\mathcal H}+\ln(1/\delta)}{\varepsilon}

    Make the right side at most δ\delta and solve for nn. The class size enters only through its logarithm.

Predict before you look

A spam filter flags an email when it contains all three words of a chosen triple, picked from a 10,000-word vocabulary, so ∣H∣=(100003)≈1.67×1011\abs{\mathcal H}=\binom{10000}{3}\approx1.67\times10^{11}. Suppose some triple labels every email correctly. How many labeled emails guarantee error at most 1% with probability at least 99%?

Show the answer

3045 emails

ln⁡∣H∣≈25.84\ln\abs{\mathcal H}\approx25.84 and ln⁡(1/δ)=ln⁡100≈4.61\ln(1/\delta)=\ln100\approx4.61, so n≥30.44/0.01≈3,044.4n\ge30.44/0.01\approx3{,}044.4: 3,045 emails. A hundred and sixty-seven billion hypotheses cost only 26 nats.

Without a perfect hypothesis, one mistake no longer refutes anything: every classifier errs sometimes, so error rates must be estimated, to precision ε\varepsilon. Hoeffding's inequality for each hh, combined with a union bound over the class, gives uniform convergence:

Pr⁡[∃ h∈H: ∣R^n(h)−R(h)∣>ε]≤2∣H∣ e−2nε2\Pr\Big[\exists\,h\in\mathcal H:\ \abs{\hat R_n(h)-R(h)}>\varepsilon\Big]\le2\abs{\mathcal H}\,e^{-2n\varepsilon^2}
So n≥ln⁡(2∣H∣/δ)/(2ε2)n\ge\ln(2\abs{\mathcal H}/\delta)/(2\varepsilon^2) examples put every training error within ε\varepsilon of its risk. If all are within ε/2\varepsilon/2, ERM's output h^\hat h is within ε\varepsilon of the best h∘h^\circ in the class: R(h^)≤R^n(h^)+ε2≤R^n(h∘)+ε2≤R(h∘)+εR(\hat h)\le\hat R_n(\hat h)+\tfrac\varepsilon2\le\hat R_n(h^\circ)+\tfrac\varepsilon2\le R(h^\circ)+\varepsilon. Hence ERM is an agnostic PAC learner with n≥2ln⁡(2∣H∣/δ)/ε2n\ge2\ln(2\abs{\mathcal H}/\delta)/\varepsilon^2 (Shalev-Shwartz & Ben-David 2014, Corollary 4.6).

For the keyword class at ε=0.05\varepsilon=0.05, δ=0.01\delta=0.01, the realizable bound asks for 609 emails and the agnostic one for 24,910: noise costs a factor of order 1/ε1/\varepsilon. The logarithm also has an Occam's razor reading. If every hypothesis can be written down in bb bits, then ∣H∣≤2b\abs{\mathcal H}\le2^b and ln⁡∣H∣≤bln⁡2\ln\abs{\mathcal H}\le b\ln2: the examples you need grow linearly with the description length of the rule you are looking for.

Infinite classes: shattering

Thresholds on the line, ha(x)=1{x≥a}h_a(x)=\mathbf 1\{x\ge a\} for a∈Ra\in\R, form an infinite class, yet they are easy to learn. Take the realizable case with target threshold a⋆a^\star, and let the learner put its threshold a^\hat a at the smallest positive training example. It errs only on [a⋆,a^)[a^\star,\hat a), and that interval has probability above ε\varepsilon only if no training point fell into the interval of mass ε\varepsilon just to the right of a⋆a^\star, an event of probability (1−ε)n≤e−εn(1-\varepsilon)^n\le e^{-\varepsilon n}. So n≥ln⁡(1/δ)/εn\ge\ln(1/\delta)/\varepsilon suffices, with no ∣H∣\abs{\mathcal H} anywhere. What matters is not how many hypotheses a class contains but how many different labelings of a sample it can produce.

ClassVC dimensionShattered set, and why nothing larger is
Thresholds 1{x≥a}\mathbf 1\{x\ge a\} on R\R1Any single point. For x1<x2x_1<x_2 the labeling (1,0)(1,0) is impossible.
Intervals 1{a≤x≤b}\mathbf 1\{a\le x\le b\}2Any two points. For x1<x2<x3x_1<x_2<x_3 the labeling (1,0,1)(1,0,1) is impossible.
Axis-aligned rectangles in R2\R^24A diamond of four points works. Given any five points, the bounding box of the four extreme ones (left, right, top, bottom) also covers the fifth, so "fifth point 0, rest 1" is impossible.
Half-spaces 1{w⊤x+b≥0}\mathbf 1\{\mathbf w^\top\mathbf x+b\ge0\} in Rd\R^dd+1d+1Any d+1d+1 affinely independent points work. Radon's theorem partitions every set of d+2d+2 points into two parts with overlapping convex hulls, and labeling the parts differently defeats every half-space.
Any finite classat most log⁡2∣H∣\log_2\abs{\mathcal H}Shattering kk points needs 2k2^k distinct hypotheses.
1{sin⁡(θx)>0}\mathbf 1\{\sin(\theta x)>0\}, θ∈R\theta\in\R∞\inftyOne parameter, yet every finite set xi=10−ix_i=10^{-i} is shattered (reveal below).
VC dimension often equals the number of parameters (half-spaces have d+1d+1), but that is a coincidence of linear classes, not a law.
Think first How can a class with a single real parameter θ\theta shatter kk points for every kk? Try to construct θ\theta for the points xi=10−ix_i=10^{-i}, i=1,…,ki=1,\dots,k, and any labels yi∈{0,1}y_i\in\{0,1\}.

Take θ=π(1+∑j=1k(1−yj)10j)\theta=\pi\big(1+\sum_{j=1}^k(1-y_j)10^j\big). Then θxi\theta x_i equals π(1−yi)\pi(1-y_i), plus π\pi times an even integer (the terms with j>ij>i), plus πci\pi c_i with ci=10−i+∑j<i(1−yj)10j−i∈(0,0.22)c_i=10^{-i}+\sum_{j<i}(1-y_j)10^{j-i}\in(0,0.22). So θxi\theta x_i lies in (0,0.22π)(0,0.22\pi) modulo 2π2\pi when yi=1y_i=1, where the sine is positive, and in (π,1.22π)(\pi,1.22\pi) when yi=0y_i=0, where it is negative. The parameter stores the labels as decimal digits: one real number can encode arbitrarily many bits.

The fundamental theorem

The growth function ΠH(n)=max⁡∣C∣=n∣HC∣\Pi_{\mathcal H}(n)=\max_{\abs C=n}\abs{\mathcal H_C} counts the most labelings H\mathcal H can produce on nn points. It equals 2n2^n up to n=dn=d, and beyond that the Sauer–Shelah lemma (Vapnik & Chervonenkis, 1971; Sauer, 1972; Shelah, 1972) caps it: ΠH(n)≤∑i=0d(ni)≤(en/d)d\Pi_{\mathcal H}(n)\le\sum_{i=0}^d\binom ni\le(en/d)^d for n≥d≥1n\ge d\ge1. Exponential growth collapses to polynomial growth. The proof of uniform convergence then replaces the union bound over ∣H∣\abs{\mathcal H} hypotheses by a union bound over the at most ΠH(2n)\Pi_{\mathcal H}(2n) labelings of a doubled sample, so ln⁡∣H∣\ln\abs{\mathcal H} turns into roughly dln⁡nd\ln n.

R(h)≤R^n(h)+2dln⁡(en/d)n+ln⁡(1/δ)2nfor all h∈H, with probability at least 1−δR(h)\le\hat R_n(h)+\sqrt{\frac{2d\ln(en/d)}{n}}+\sqrt{\frac{\ln(1/\delta)}{2n}}\qquad\text{for all }h\in\mathcal H,\ \text{with probability at least }1-\delta
A concrete uniform-convergence bound for classifiers with values in {−1,+1}\{-1,+1\} and VC dimension dd, valid for n≥dn\ge d (Mohri, Rostamizadeh & Talwalkar 2018, Corollary 3.19). What matters is the rate dln⁡(n/d)/n\sqrt{d\ln(n/d)/n}: the ratio of examples to VC dimension controls the gap.
Predict before you look

Half-spaces in R10\R^{10} have VC dimension d=11d=11. With n=50,000n=50{,}000 examples and δ=0.01\delta=0.01, how large is the gap term 2dln⁡(en/d)/n+ln⁡(1/δ)/(2n)\sqrt{2d\ln(en/d)/n}+\sqrt{\ln(1/\delta)/(2n)}?

Show the answer

0.071

Here en/d≈12,356en/d\approx12{,}356 with logarithm 9.429.42, so the first term is 22×9.42/50,000≈0.064\sqrt{22\times9.42/50{,}000}\approx0.064, and the confidence term is 4.61/100,000≈0.007\sqrt{4.61/100{,}000}\approx0.007: about 0.0710.071 in total. It is guaranteed for every distribution, and it shrinks slowly: quadrupling the data to 200,000200{,}000 examples only brings it to about 0.0380.038.

Check your understanding

Suppose VCdim⁡(H)=∞\operatorname{VCdim}(\mathcal H)=\infty. Which statement is true?

  1. ERM fails, but a cleverer algorithm can still PAC learn H\mathcal H
  2. H\mathcal H is agnostic PAC learnable but not PAC learnable
  3. No algorithm PAC learns H\mathcal H: for every sample size, some distribution with a perfect hypothesis in H\mathcal H defeats it
  4. H\mathcal H cannot fit most training sets
Show the answer

C. No algorithm PAC learns H\mathcal H: for every sample size, some distribution with a perfect hypothesis in H\mathcal H defeats it

The theorem makes learnability by any algorithm equivalent to finite VC dimension. The obstruction is the no-free-lunch construction (No Free Lunch & Inductive Bias): whenever H\mathcal H shatters 2n2n points, every learner given nn examples meets some distribution on those points, realizable by H\mathcal H, under which its error reaches 1/81/8 with probability at least 1/71/7 (Shalev-Shwartz & Ben-David 2014, Corollary 6.4). Infinite VC dimension supplies such a set for every nn.

What the guarantees do and do not say

Two caveats keep these theorems honest. First, they are worst case over distributions: the constants and the dln⁡n/n\sqrt{d\ln n/n} rate must cover adversarial PP, so for benign data the numbers are pessimistic. Second, dd can be enormous. For ReLU networks with WW weights in LL layers the VC dimension is O(WLlog⁡W)\order{WL\log W}, and some such networks reach Ω(WLlog⁡(W/L))\Omega(WL\log(W/L)) (Bartlett, Harvey, Liaw & Mehrabian, 2019). A network with a million weights trained on fifty thousand examples sits far inside d≫nd\gg n, where every VC bound is vacuous, and yet such networks often generalize well. Explaining that needs complexity measures that depend on the data and on the training algorithm (Generalization in Modern ML).

Complexity measureDepends onTypical gap termDeveloped in
ln⁡∣H∣\ln\abs{\mathcal H}the size of the classln⁡(∣H∣/δ)/n\sqrt{\ln(\abs{\mathcal H}/\delta)/n}this lesson
VC dimension ddthe class, combinatoriallydln⁡(n/d)/n\sqrt{d\ln(n/d)/n}VC Dimension & the Fundamental Theorem
Rademacher complexity, margins, normsthe class and the data2Λr/(γn)2\Lambda r/(\gamma\sqrt n) for linear predictors with ∥w∥≤Λ\norm{\mathbf w}\le\Lambda, ∥x∥≤r\norm{\mathbf x}\le r, margin γ\gammaRademacher Complexity
Stability, compression, PAC-Bayesthe algorithm (and a prior)(DKL(Q ∥ π)+ln⁡(2n/δ))/(2n)\sqrt{(\KL(Q\,\|\,\pi)+\ln(2\sqrt n/\delta))/(2n)} for a randomized predictor QQ and a prior π\pi fixed before seeing the dataStability, Compression & PAC-Bayes
From worst-case counting toward measures that can see the data and the algorithm. Each row is a different route to the same kind of statement: risk is at most training error plus a gap term.

Key takeaways

  • A PAC guarantee promises, for every data distribution at once, that a random training sample will fail to deliver a classifier within ε\varepsilon of perfect (realizable case) or of the best in the class (agnostic case) with probability at most δ\delta.
  • Finite classes need (ln⁡∣H∣+ln⁡(1/δ))/ε(\ln\abs{\mathcal H}+\ln(1/\delta))/\varepsilon examples when a perfect hypothesis exists and 2ln⁡(2∣H∣/δ)/ε22\ln(2\abs{\mathcal H}/\delta)/\varepsilon^2 for agnostic ERM: noise costs a factor 1/ε1/\varepsilon, and class size enters only through its logarithm.
  • Shattering measures what a class can do on finite samples: the VC dimension is the biggest sample it can label in every possible way, which half-spaces in Rd\R^d manage for d+1d+1 points and a one-parameter sine family for any number.
  • For binary classification, learnability, uniform convergence, ERM working and finite VC dimension dd coincide; with noise, Θ((d+ln⁡(1/δ))/ε2)\Theta((d+\ln(1/\delta))/\varepsilon^2) examples are necessary and sufficient, and without noise Θ((d+ln⁡(1/δ))/ε)\Theta((d+\ln(1/\delta))/\varepsilon).
  • Worst-case bounds become vacuous when dd is comparable to nn, as for large neural networks; that is what motivates data- and algorithm-dependent theories.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In the realizable finite-class bound n≥(ln⁡∣H∣+ln⁡(1/δ))/εn\ge(\ln\abs{\mathcal H}+\ln(1/\delta))/\varepsilon with ε=0.01\varepsilon=0.01, you double the number of hypotheses. How many more examples does the bound ask for?

  1. Twice as many
  2. About 69 more
  3. About 41% more
  4. None: only the VC dimension matters
Show the answer

B. About 69 more

Doubling ∣H∣\abs{\mathcal H} adds ln⁡2≈0.69\ln2\approx0.69 to the numerator, hence ln⁡2/ε≈69\ln2/\varepsilon\approx69 examples, whatever the starting size. Sample size grows with the logarithm of the class size, that is, with the number of bits needed to name a hypothesis.

Question 2

What is the VC dimension of unions of two intervals on the real line, 1{x∈[a1,b1]∪[a2,b2]}\mathbf 1\{x\in[a_1,b_1]\cup[a_2,b_2]\}?

  1. 2
  2. 3
  3. 4
  4. 5
Show the answer

C. 4

Any labeling of four points x1<⋯<x4x_1<\dots<x_4 has at most two runs of 1s (the worst case is 1,0,1,01,0,1,0 or 0,1,0,10,1,0,1), and each run is covered by one interval, so four points are shattered. Five points labeled 1,0,1,0,11,0,1,0,1 have three runs of 1s, which two intervals cannot cover.

Question 3

For the same finite class, why does the sample complexity grow like 1/ε21/\varepsilon^2 in the agnostic setting but only like 1/ε1/\varepsilon in the realizable one?

  1. A noisy problem has more hypotheses to search
  2. ERM is no longer computable when labels are noisy
  3. With noise every hypothesis makes mistakes, so error rates must be estimated to accuracy ε\varepsilon, and averages of nn coin flips fluctuate by about 1/n1/\sqrt n
  4. The union bound does not apply to noisy labels
Show the answer

C. With noise every hypothesis makes mistakes, so error rates must be estimated to accuracy ε\varepsilon, and averages of nn coin flips fluctuate by about 1/n1/\sqrt n

In the realizable case a single training mistake eliminates a hypothesis, and a bad hypothesis is caught at rate ε\varepsilon per example. With noise, telling an error rate of 0.300.30 from 0.30+ε0.30+\varepsilon requires estimation, and Hoeffding's 2e−2nε22e^{-2n\varepsilon^2} needs nn of order 1/ε21/\varepsilon^2.

Question 4

A class of binary classifiers is described by two real parameters. What can you conclude about its VC dimension?

  1. It is exactly 2
  2. It is at most 3
  3. It is at least 2
  4. Nothing in general: it can be anything from 0 to ∞\infty
Show the answer

D. Nothing in general: it can be anything from 0 to ∞\infty

VC dimension counts realizable labelings, not parameters. Half-planes through the origin in R2\R^2 have two parameters and VC dimension 2, but 1{sin⁡(θx)>0}\mathbf 1\{\sin(\theta x)>0\} has one parameter and infinite VC dimension, and a two-parameter class whose parameters do not change the predictions has VC dimension 0.

Question 5

A VC bound for your trained network says its test error is at most 3.7. What should you conclude?

  1. The network will perform badly on new data
  2. Nothing about this network: a bound above 1 holds for every classifier and carries no information
  3. The bound must contain an arithmetic mistake
  4. Collecting 3.7 times more data will make the network accurate
Show the answer

B. Nothing about this network: a bound above 1 holds for every classifier and carries no information

An error rate is at most 1, so a bound of 3.7 is vacuous: true but empty. Such bounds are worst-case over all distributions and all networks in the class; they cannot certify the network, and they do not say it will fail. Data- and algorithm-dependent bounds (the Convergence lessons) can do better.

Go deeper

Further reading

  • L. G. Valiant, "A theory of the learnable," Communications of the ACM 27(11), 1134–1142 (1984).
  • V. N. Vapnik & A. Ya. Chervonenkis, "On the uniform convergence of relative frequencies of events to their probabilities," Theory of Probability and Its Applications 16(2), 264–280 (1971).
  • A. Blumer, A. Ehrenfeucht, D. Haussler & M. K. Warmuth, "Learnability and the Vapnik–Chervonenkis dimension," Journal of the ACM 36(4), 929–965 (1989).
  • S. Shalev-Shwartz & S. Ben-David, Understanding Machine Learning: From Theory to Algorithms (Cambridge University Press, 2014), chapters 2–6.
  • M. Mohri, A. Rostamizadeh & A. Talwalkar, Foundations of Machine Learning, 2nd ed. (MIT Press, 2018), chapters 2–3.