Unit 6, lesson 42

Generalization, Model Capacity, and Learning Guarantees

How Hoeffding's inequality and the union bound give a generalization bound for a finite hypothesis class with bounded losses, what capacity measures in it, and why inductive bias and the no free lunch theorem matter.

  • Video 42
  • Uncertainty and Learning Guarantees

A classifier makes 120 mistakes on 1,000 labeled examples, so its empirical error rate is 0.12. How large could its error rate be on new cases from the same population? The observed score is one number computed from one sample, while a guarantee about risk is a theorem that holds only under stated assumptions. This lesson derives one such theorem for a finite set of candidate predictors, computes what it promises, and shows which conditions a project must meet before the promise applies.

Key terms

Finite hypothesis class

Definition A hypothesis class H\mathcal{H} is the set of predictors hh that a learning procedure is allowed to choose from. It is finite when it has finitely many members, and ∣H∣\lvert \mathcal{H} \rvert denotes their number. The guarantees on this page assume the following setting. The examples (X1,Y1),…,(Xn,Yn)(\mathbf{X}_1, Y_1), \dots, (\mathbf{X}_n, Y_n) are independent draws from one distribution PP, the population whose risk we want to describe. The loss satisfies 0≤L(y,y^)≤10 \le L(y, \hat{y}) \le 1 for every possible target and prediction. The class is fixed before the sample is drawn, so it does not depend on these examples. For each hh in H\mathcal{H} the population risk and the empirical risk are

R(h)=E[L(Y,h(X))],R^n(h)=1n∑i=1nL(Yi,h(Xi)),\begin{aligned} R(h) &= E\bigl[L\bigl(Y, h(\mathbf{X})\bigr)\bigr], \\[0.4em] \hat{R}_n(h) &= \frac{1}{n}\sum_{i=1}^{n} L\bigl(Y_i, h(\mathbf{X}_i)\bigr), \end{aligned}

where (X,Y)(\mathbf{X}, Y) is a new draw from PP. The gap of hh is Δn(h)=∣R^n(h)−R(h)∣\Delta_n(h) = \lvert \hat{R}_n(h) - R(h) \rvert. Its value depends on the random sample.

In plain words The class is the menu of predictors. The theorems below say nothing until three things hold, a menu written down before the data arrive, losses that can never exceed one, and examples drawn independently from the population we care about. Zero-one loss, which charges 1 for a wrong label and 0 for a right one, meets the boundedness condition. Squared error on an unbounded target does not, even when every observed error happens to be small.

Example For a constructed task of flagging late deliveries, take the twenty rules “flag the delivery when its distance exceeds tt km” for t=1,2,…,20t = 1, 2, \dots, 20, chosen before any data are seen. They form a finite class with ∣H∣=20\lvert \mathcal{H} \rvert = 20. Under zero-one loss, a rule that makes 120 mistakes on 1,000 examples has empirical risk 0.12. Its population risk is the probability that it errs on a new delivery drawn from PP, a fixed number that the sample does not reveal. The rules with every real threshold tt form an infinite class, although each rule is described by a single number.

Hoeffding’s inequality

Definition Let Z1,…,ZnZ_1, \dots, Z_n be independent random variables with a≤Zi≤ba \le Z_i \le b for every ii, where a<ba < b are constants, and let Zˉ\bar{Z} be their average. Hoeffding’s inequality states that for every ε>0\varepsilon > 0

P(∣Zˉ−E[Zˉ]∣≥ε)≤2exp⁡(−2nε2(b−a)2).\begin{aligned} &P\bigl(\lvert \bar{Z} - E[\bar{Z}] \rvert \ge \varepsilon\bigr) \\[0.4em] &\quad \le 2\exp\Bigl(-\frac{2n\varepsilon^2}{(b - a)^2}\Bigr) . \end{aligned}

For one predictor hh fixed before the sample is drawn, the losses Zi=L(Yi,h(Xi))Z_i = L(Y_i, h(\mathbf{X}_i)) are independent, lie in [0,1][0, 1] and have mean R(h)R(h), so

P(Δn(h)≥ε)≤2e−2nε2.P\bigl(\Delta_n(h) \ge \varepsilon\bigr) \le 2e^{-2n\varepsilon^2} .

The probability is over the random sample, and the factor 2 covers deviations in both directions. The inequality uses only independence and the range of the losses, so it holds for every distribution PP. The same generality makes it loose for many particular distributions. It applies to a predictor fixed in advance. A predictor chosen because its empirical risk is small depends on the sample, and the inequality does not apply to it directly.

In plain words An average of many independent bounded quantities rarely strays far from its mean, and the chance of a large deviation shrinks exponentially as nε2n\varepsilon^2 grows. Bounded losses keep any one example from moving the average by more than 1/n1/n.

Example For n=1,000n = 1{,}000 and ε=0.05\varepsilon = 0.05 the bound is 2e−5≈0.01352e^{-5} \approx 0.0135. So if one classifier was fixed before the 1,000 examples were drawn, the chance that its empirical error differs from its population error by 0.05 or more is at most about 0.0135. With only n=100n = 100 examples the same ε\varepsilon gives 2e−0.5≈1.212e^{-0.5} \approx 1.21, a bound above 1 that says nothing.

Union bound and uniform convergence

Definition The union bound states that for any events A1,…,AkA_1, \dots, A_k

P(A1∪⋯∪Ak)≤∑j=1kP(Aj),\begin{aligned} &P(A_1 \cup \dots \cup A_k) \\[0.4em] &\quad \le \sum_{j=1}^{k} P(A_j) , \end{aligned}

with no assumption of independence. A class H\mathcal{H} has the uniform convergence property for a loss when, for every ε>0\varepsilon > 0 and every δ\delta in (0,1)(0, 1), there is a sample size n0n_0 such that for every n≥n0n \ge n_0 and every distribution PP, a sample of nn independent draws from PP gives sup⁡h∈HΔn(h)<ε\sup_{h \in \mathcal{H}} \Delta_n(h) < \varepsilon with probability at least 1−δ1 - \delta. In the setting above, apply the union bound to the events Δn(h)≥ε\Delta_n(h) \ge \varepsilon, one for each member, and bound each by Hoeffding’s inequality.

P(max⁡h∈HΔn(h)≥ε)≤2∣H∣e−2nε2.\begin{aligned} &P\Bigl(\max_{h \in \mathcal{H}} \Delta_n(h) \ge \varepsilon\Bigr) \\[0.4em] &\quad \le 2\lvert \mathcal{H} \rvert e^{-2n\varepsilon^2} . \end{aligned}

So every finite class with losses in [0,1][0, 1] has the property, and any n0≥ln⁡(2∣H∣/δ)/(2ε2)n_0 \ge \ln(2\lvert \mathcal{H} \rvert/\delta)/(2\varepsilon^2) works.

In plain words Each member of the class has a small chance of a misleading score. The union bound adds those chances, so the chance that at least one member is misleading is at most the total. Adding is valid however the events overlap, and it is wasteful when they overlap a lot, as they do for similar predictors. The payoff is a statement about every member at once, so it still holds for whichever member the data end up choosing.

Example With n=1,000n = 1{,}000 and ε=0.05\varepsilon = 0.05, each fixed classifier has failure probability at most 2e−5≈0.01352e^{-5} \approx 0.0135. For 20 classifiers the union bound gives at most 40e−5≈0.2740e^{-5} \approx 0.27. For 2,000 classifiers it gives 4000e−5≈26.954000e^{-5} \approx 26.95, which is above 1 and says nothing at this ε\varepsilon. If two classifiers in the class give identical predictions, their failure events are the same event, yet the union bound counts its probability twice.

Generalization bound

Definition A generalization bound limits the population risk of a learned predictor by quantities computed from the training sample, with a stated probability over the draw of that sample. For a finite class the derivation above gives the following theorem. Suppose the class is fixed before the sample is drawn, the nn examples are independent draws from PP, the loss takes values in [0,1][0, 1] and δ\delta is in (0,1)(0, 1). Setting 2∣H∣e−2nε2=δ2\lvert \mathcal{H} \rvert e^{-2n\varepsilon^2} = \delta and solving gives

ε=ln⁡(2∣H∣/δ)2n,\varepsilon = \sqrt{\frac{\ln(2\lvert \mathcal{H} \rvert/\delta)}{2n}} ,

with the natural logarithm. Then, with probability at least 1−δ1 - \delta, every hh in H\mathcal{H} satisfies Δn(h)≤ε\Delta_n(h) \le \varepsilon. Because this event covers every member, it covers a member h^\hat{h} chosen with the same sample. If h^\hat{h} minimizes the empirical risk over H\mathcal{H} and h∗h^* minimizes the population risk over H\mathcal{H}, both of which exist because the class is finite, then on the same event

R(h^)≤R^n(h^)+ε≤R^n(h∗)+ε≤R(h∗)+2ε.\begin{aligned} R(\hat{h}) &\le \hat{R}_n(\hat{h}) + \varepsilon \\[0.4em] &\le \hat{R}_n(h^*) + \varepsilon \\[0.4em] &\le R(h^*) + 2\varepsilon . \end{aligned}

The first and last steps use the uniform event, and the middle step holds because h^\hat{h} has the smallest empirical risk. The theorem says nothing about how good h∗h^* is, and it can be loose, since Hoeffding’s inequality ignores the actual spread of the losses and the union bound ignores overlap between members.

In plain words Before the sample is drawn, there is a probability of at least 1−δ1 - \delta that every member’s score will land within ε\varepsilon of its risk. After one sample the event has either happened or not, and we cannot tell which. The radius depends on the class size only through a logarithm under the square root, shrinks in proportion to 1/n1/\sqrt{n}, and grows when δ\delta is made smaller. Those are properties of this formula, not predictions of how a particular model’s error will behave.

Example Take n=1,000n = 1{,}000 and δ=0.05\delta = 0.05. For ∣H∣=20\lvert \mathcal{H} \rvert = 20, the quantity 2∣H∣/δ2\lvert \mathcal{H} \rvert/\delta is 800, its logarithm is about 6.6846, and dividing by 2,000 and taking the square root gives ε≈0.0578\varepsilon \approx 0.0578.

Class size ∣H∣\lvert \mathcal{H} \rvertln⁡(2∣H∣/δ)\ln(2\lvert \mathcal{H} \rvert/\delta)ε\varepsilon
11≈3.689\approx 3.689≈0.0429\approx 0.0429
2020≈6.685\approx 6.685≈0.0578\approx 0.0578
2,0002{,}000≈11.290\approx 11.290≈0.0751\approx 0.0751

A member of the 20-rule class selected with empirical risk 0.12 therefore has population risk between about 0.0622 and 0.1778 on the uniform event, and the empirical risk minimizer is within 2ε≈0.1162\varepsilon \approx 0.116 of the best rule in the class. A class 100 times larger adds only ln⁡100≈4.61\ln 100 \approx 4.61 to the logarithm. Quadrupling the sample to 4,000 halves the radius to about 0.0289. The bound is also loose. For a single fixed classifier with true error rate 0.12 and n=1,000n = 1{,}000, the exact binomial probability of a gap of 0.0429 or more is about 0.00004, far below the 0.05 that the bound allows.

Capacity

Definition Capacity describes how rich a hypothesis class is, how many different patterns of predictions its members can produce, and therefore how easily some member can match a sample by chance. No single measure applies to every class. In the finite-class bound the measure is ln⁡∣H∣\ln \lvert \mathcal{H} \rvert, the logarithm of the number of distinct predictors. The number of parameters is a different quantity. A class indexed by even one real-valued parameter usually contains infinitely many predictors, so the finite-class bound gives no conclusion about it, and infinite classes need other capacity measures, such as the VC dimension, each with its own theorem and assumptions. Two rules follow from the derivation. Replacing ∣H∣\lvert \mathcal{H} \rvert by any number M≥∣H∣M \ge \lvert \mathcal{H} \rvert keeps the bound valid, because ε\varepsilon increases with the class size. And the class must contain every predictor the whole procedure could have returned, across every candidate model, hyperparameter setting and feature subset the data might have led to, all fixed before the sample is drawn. Counting only the candidates a search happened to try is not enough, and an adaptive search over networks with real-valued weights can reach infinitely many predictors.

In plain words A richer class has more ways to match the training sample by luck, so its training scores need a wider allowance. The allowance depends on what the class can express, not on how many numbers describe one member. Capacity is neither good nor bad by itself. A class with too little of it may not contain any good predictor.

Example A linear classifier with ten real-valued coefficients on real-valued features defines infinitely many classifiers, not ten. Suppose instead that each coefficient is restricted, before any data are seen, to a grid of 1,000 values. The class then has at most 100010=10301000^{10} = 10^{30} members, and with n=1,000n = 1{,}000 and δ=0.05\delta = 0.05

ε≤ln⁡(4×1031)2000≈0.191.\varepsilon \le \sqrt{\frac{\ln(4 \times 10^{31})}{2000}} \approx 0.191 .

Substituting ∣H∣=10\lvert \mathcal{H} \rvert = 10, the number of parameters, would give about 0.0547, a radius that the theorem does not support.

Inductive bias and the no free lunch theorem

Definition The inductive bias of a learning procedure is the set of assumptions or preferences, beyond the training data, that leads it to favor some predictors over others that fit the same data equally well. It enters through the class, such as linear functions, through penalties, such as the ridge preference for small weights, and through built-in structure, such as smoothness or the sharing of weights across positions in an image model. The no free lunch theorem shows that some bias is unavoidable. One version concerns a finite input set X\mathcal{X}, labels in {0,1}\{0, 1\} and zero-one loss. If the target function ff is drawn uniformly from all 2∣X∣2^{\lvert \mathcal{X} \rvert} functions from X\mathcal{X} to {0,1}\{0, 1\}, then for every learning algorithm whose internal randomness is independent of ff, and any training inputs chosen independently of ff and labeled by ff, the predicted label at any input outside the training inputs is correct with probability exactly 1/21/2. Averaged over all targets, every algorithm therefore has the same error on unseen inputs, 1/21/2, so an advantage on some targets is exactly offset on others. Another version, in the same binary setting, shows that for every algorithm and every training size below half the number of possible inputs, some distribution has a predictor with zero risk while the algorithm’s risk is at least 1/81/8 with probability at least 1/71/7 over the sample.

In plain words Data say nothing by themselves about inputs that were never observed, so some assumption has to connect seen and unseen cases. The theorem averages over every conceivable labeling, including labelings with no pattern at all, and real problems are not like that. It does not say that learning is futile. It says that a procedure works because its assumptions suit the problem. The generalization bound controls how far the chosen member falls behind the best member of the class, and only the fit between the bias and the problem decides whether that best member is any good.

Example Suppose there are two inputs and the training set shows label 1 at the first. A world with label 1 at the second input and a world with label 0 there agree with everything observed. A learner that assumes nearby inputs share labels predicts 1, and it is right in the first world and wrong in the second. Too strong a bias fails in a different way. The class of the two constant classifiers has ∣H∣=2\lvert \mathcal{H} \rvert = 2, and with n=1,000n = 1{,}000 and δ=0.05\delta = 0.05 its radius is about 0.0468. If the feature is uniform on [0,1][0, 1] and the label is 1 exactly when the feature is at least 0.5, both constants have risk 0.5, while the threshold rule at 0.5 has risk 0. The bound is narrow around a poor answer.

Check yourself

  1. Five prespecified classifiers are scored on 400 independent examples with zero-one loss, and δ=0.1\delta = 0.1. The classifier with the smallest empirical error, 0.08, is selected. Compute ε\varepsilon and the upper bound on the selected classifier’s population error rate. Does the bound apply although the classifier was chosen with the same data?
Show the answer

The quantity 2∣H∣/δ2\lvert \mathcal{H} \rvert/\delta is 2⋅5/0.1=1002 \cdot 5/0.1 = 100, and ln⁡100≈4.6052\ln 100 \approx 4.6052. Dividing by 2n=8002n = 800 gives about 0.005756, and the square root is ε≈0.0759\varepsilon \approx 0.0759. With probability at least 0.9 over the sample, all five classifiers have gaps of at most ε\varepsilon simultaneously, so the selected one has population error rate at most

0.08+0.0759≈0.156.0.08 + 0.0759 \approx 0.156 .

The bound does apply. The uniform event covers all five members, so it does not matter which one the data picked. It would not apply if the five candidates had been chosen after looking at these 400 examples, or if more than five candidates had been tried and only five counted.

  1. A team has 1,000 prespecified rules and wants every gap to be at most 0.05 with δ=0.01\delta = 0.01. How many independent examples does the finite-class theorem require? What does it then guarantee about the rule with the smallest empirical risk, compared with the best rule in the class? How many examples would a radius of 0.025 require?
Show the answer

The condition ε≤0.05\varepsilon \le 0.05 is equivalent to

n≥ln⁡(200,000)2⋅0.052≈2441.2,n \ge \frac{\ln(200{,}000)}{2 \cdot 0.05^2} \approx 2441.2 ,

since 2⋅1000/0.01=200,0002 \cdot 1000/0.01 = 200{,}000 and its logarithm is about 12.206. So the theorem needs at least 2,442 examples. With probability at least 0.99, every rule is then within 0.05 of its risk, and the empirical risk minimizer has population risk at most 2ε=0.12\varepsilon = 0.1 above that of the best rule in the class. Halving the radius multiplies the requirement by four, which gives n≥9764.9n \ge 9764.9, so at least 9,765 examples.

  1. A colleague fits a linear demand forecaster with ten real-valued coefficients to 400 consecutive days of sales. To quote a guarantee, they divide every squared error by the largest squared error in the sample so that the losses lie in [0,1][0, 1], set ∣H∣=10\lvert \mathcal{H} \rvert = 10 and δ=0.05\delta = 0.05, and call the result a 95 percent guarantee for next month’s squared error. Which conditions of the finite-class theorem fail?
Show the answer

Several conditions fail, and any one of them is enough to void the claim.

The class is not finite with ten members. Ten real-valued coefficients define infinitely many forecasters, and the fitted coefficients were chosen from all of them, so the number of parameters cannot stand in for ∣H∣\lvert \mathcal{H} \rvert and the finite-class bound gives no conclusion.

The loss is not bounded by a constant fixed in advance. Raw squared errors have no upper limit, and a scale taken from the sample after seeing it does not make the loss bounded on new days, since a future error can exceed the largest one observed. The rescaled number is also not the squared error the colleague wants to describe.

The days are not independent draws from one distribution. Consecutive days are correlated, demand drifts, and next month is a different population from the 400 days used.

An honest report would instead evaluate the forecaster on later days that played no part in fitting or choosing it, with a time-ordered split, and state that the result describes those days. A guarantee would need a theorem built for dependent data, a loss bounded by a constant chosen before the data were seen and a class whose capacity is measured correctly.

How this lesson connects