Simple Algorithm
Machine Learning · Convergence · 30 min

Stability, Compression & PAC-Bayes

Other roads to generalization guarantees.

Uniform convergence demands that every hypothesis in a class behave, including the many your algorithm would never output. Three other routes ask less: that the algorithm not lean too hard on any single example, that its output be rebuilt from a handful of examples, or that it stay close to a distribution chosen before seeing the data. The last of these produced the first nonvacuous generalization bounds for deep networks.

What you’ll learn

  • Define uniform stability, prove that it controls the expected generalization gap, and state the high-probability bounds of Bousquet and Elisseeff and their sharper successors.
  • Apply stability to regularized ERM and to SGD, and explain what "train faster, generalize better" does and does not mean.
  • Derive the sample-compression bound, and state what is known about compressing VC classes.
  • Prove a PAC-Bayes bound by the change-of-measure inequality, state its kl form, and explain how optimizing it gave nonvacuous bounds for deep networks.

Stability: no single example matters much

Uniform convergence controls every hypothesis in the class. Algorithmic stability looks only at the algorithm: if removing or replacing one training example barely changes what it learns, it cannot have memorized that example, so its training error cannot be much more optimistic than its test error. Throughout, losses take values in [0,M][0,M] and ASA_S denotes the hypothesis the algorithm AA returns on the sample S=(z1,…,zn)S=(z_1,\dots,z_n).

DerivationStability controls the expected gap
  1. ES[R(AS)]=ES,z′[ℓ(AS,z′)]=ES,z′[ℓ(AS(i),zi)]\E_S\big[R(A_S)\big]=\E_{S,z'}\big[\ell(A_S,z')\big]=\E_{S,z'}\big[\ell(A_{S^{(i)}},z_i)\big]

    Let z′z' be a fresh example and S(i)S^{(i)} the sample with ziz_i replaced by z′z'. The pairs (S,z′)(S,z') and (S(i),zi)(S^{(i)},z_i) have the same distribution, so for every ii

  2. E[R(AS)−R^n(AS)]=1n∑i=1nE[ℓ(AS(i),zi)−ℓ(AS,zi)]\E\big[R(A_S)-\hat R_n(A_S)\big]=\frac1n\sum_{i=1}^n\E\big[\ell(A_{S^{(i)}},z_i)-\ell(A_S,z_i)\big]

    Average over ii and subtract the expected training error (Shalev-Shwartz & Ben-David 2014, Theorem 13.2):

  3. ∣E[R(AS)−R^n(AS)]∣≤εstab\Big|\E\big[R(A_S)-\hat R_n(A_S)\big]\Big|\le\varepsilon_{\mathrm{stab}}

    Each term compares the loss on ziz_i of a model that never saw ziz_i with one trained on it: the gap is an average sensitivity to individual examples. If replacing one example never moves any loss by more than εstab\varepsilon_{\mathrm{stab}} (Hardt, Recht & Singer 2016, Theorem 2.2):

Predict before you look

An algorithm has uniform stability β=1/n\beta=1/n with n=10,000n=10{,}000, loss in [0,1][0,1] (M=1M=1) and δ=0.05\delta=0.05. How large is the Bousquet–Elisseeff gap term 2β+(4nβ+M)ln⁡(1/δ)/(2n)2\beta+(4n\beta+M)\sqrt{\ln(1/\delta)/(2n)}?

Show the answer

0.061

ln⁡20/20,000≈0.0122\sqrt{\ln20/20{,}000}\approx0.0122 and 4nβ+M=54n\beta+M=5, so the gap is 0.0002+5×0.0122≈0.0610.0002+5\times0.0122\approx0.061: five times the Hoeffding term of a single fixed hypothesis. With β=1/n=0.01\beta=1/\sqrt n=0.01 the same formula gives 0.02+401×0.0122≈4.90.02+401\times0.0122\approx4.9, vacuous. That n\sqrt n overhead is what the newer bounds remove.

Which algorithms are stable?

The mechanism is strong convexity: the regularized objective is 2λ2\lambda-strongly convex, so changing one of its nn terms can move the minimizer only by O(1/(λn))\order{1/(\lambda n)}. The soft-margin SVM (hinge loss, σ=1\sigma=1) and kernel ridge regression with bounded responses are covered. The same argument gives E[R(AS)−R^n(AS)]≤2ρ2/(λn)\E[R(A_S)-\hat R_n(A_S)]\le2\rho^2/(\lambda n) for ρ\rho-Lipschitz convex losses with penalty λ∥w∥2\lambda\norm{\mathbf w}^2 (Shalev-Shwartz & Ben-David 2014, Corollary 13.6). Stronger regularization buys stability with bias, the bias–variance trade-off in another guise.

Hence the slogan "train faster, generalize better": in the convex case the gap is bounded by the sum of the step sizes, ∑tηt\sum_t\eta_t, so fewer and smaller steps generalize better, and combining this with the optimization error gives early stopping a theory. The limits are as instructive. The nonconvex bound grows almost linearly in TT, and every uniform stability bound is blind to the labels, as Zhang, Bengio, Hardt, Recht & Vinyals (2017) pointed out: the same algorithm run on random labels has the same stability guarantee, while its true gap there is huge (about 90% for ten balanced classes). In the general learning setting, by contrast, stability is exactly the right notion: Shalev-Shwartz, Shamir, Srebro & Sridharan (2010) exhibited problems that are learnable although uniform convergence fails and ERM does not work, and identified stability as the condition necessary and sufficient for learnability there.

Check your understanding

Which statement about the convex SGD bound εstab≤(2G2/n)∑tηt\varepsilon_{\mathrm{stab}}\le(2G^2/n)\sum_t\eta_t is correct?

  1. It guarantees that SGD reaches low test error
  2. It improves the longer SGD runs
  3. It bounds the expected generalization gap, grows with the total step size, and does not depend on the labels
  4. It needs the loss to be strongly convex
Show the answer

C. It bounds the expected generalization gap, grows with the total step size, and does not depend on the labels

Stability bounds the difference between test and training error, not the test error itself, which also needs small training error. The bound grows with ∑tηt\sum_t\eta_t, so it worsens with more or larger steps, and nothing in it refers to the labels. Strong convexity gives the TT-independent version; the general convex bound does not need it.

Compression: keep a few examples, rebuild the rest

Class or algorithmSize kkWhat is kept
Thresholds 1{x≥a}\mathbf 1\{x\ge a\}1the smallest positive example
Axis-aligned boxes in Rd\R^d2d2dfor each coordinate, the two extreme positive examples; the smallest box around them is consistent
Hard-margin SVMnumber of support vectorsthe support vectors: retraining on them alone returns the same separator, by the KKT conditions
Perceptron, margin γ\gamma, radius rrat most (r/γ)2(r/\gamma)^2the examples on which it erred, in order; replaying them rebuilds the same weight vector (Novikoff's mistake bound)
DerivationThe compression bound
  1. (1−ε)n−k≤e−ε(n−k)(1-\varepsilon)^{n-k}\le e^{-\varepsilon(n-k)}

    Fix a length kk and an index sequence I∈{1,…,n}kI\in\{1,\dots,n\}^k. The hypothesis hIh_I rebuilt from those examples is independent of the at least n−kn-k others. If R(hI)>εR(h_I)>\varepsilon, it labels all of them correctly with probability at most

  2. Pr⁡[∃I∈[n]k: hI consistent outside I and R(hI)>εk]≤nke−εk(n−k)\Pr\big[\exists I\in[n]^k:\ h_I\text{ consistent outside }I\text{ and }R(h_I)>\varepsilon_k\big]\le n^ke^{-\varepsilon_k(n-k)}

    The returned hypothesis is one of the hIh_I, and it is correct on every example outside II. A union bound over the at most nkn^k sequences of length kk:

  3. R(h)≤kln⁡n+ln⁡((n+1)/δ)n−kR(h)\le\frac{k\ln n+\ln\big((n+1)/\delta\big)}{n-k}

    Choose εk\varepsilon_k so that each length contributes at most δ/(n+1)\delta/(n+1), and take a union over k=0,…,nk=0,\dots,n so that the size may depend on the data. With probability at least 1−δ1-\delta, a consistent reconstruction of size k<nk<n satisfies

  4. This is Littlestone and Warmuth's union-bound argument (as sketched by Moran & Yehudayoff, 2016). Shalev-Shwartz & Ben-David (2014, Corollary 30.3) state the cruder 8kln⁡(n/δ)/n8k\ln(n/\delta)/n for a fixed k≤n/2k\le n/2. No VC dimension, norm or parameter count appears: only how few examples suffice to pin the output down.

Predict before you look

A hard-margin SVM separates n=1,000n=1{,}000 training points using k=20k=20 support vectors. With δ=0.05\delta=0.05, evaluate the compression bound (kln⁡n+ln⁡((n+1)/δ))/(n−k)\big(k\ln n+\ln((n+1)/\delta)\big)/(n-k).

Show the answer

0.151

20ln⁡1000≈138.220\ln1000\approx138.2 and ln⁡(1001/0.05)≈9.9\ln(1001/0.05)\approx9.9, so the bound is 148.1/980≈0.151148.1/980\approx0.151, whatever the dimension of the feature space, even for an RBF kernel. With 10 support vectors it would be about 0.0800.080: fewer support vectors, better guarantee.

Compression implies PAC learnability, hence finite VC dimension, and Littlestone and Warmuth asked about the converse. Floyd & Warmuth (1995) asked whether every class of VC dimension dd has a scheme of size O(d)\order d. Moran & Yehudayoff (2016) proved that every such class has one of size 2O(d)2^{\order d} (counting a little side information), so for binary classification compressibility and learnability are equivalent; the O(d)\order d question, they note, remains open.

PAC-Bayes: distance from a prior

Fix a prior PP over H\mathcal H before seeing the data. After seeing it, choose any posterior QQ, which may depend on the sample in any way, and predict with the Gibbs classifier: draw h∼Qh\sim Q for each prediction. Its risk and training error are R(Q)=Eh∼QR(h)R(Q)=\E_{h\sim Q}R(h) and R^n(Q)=Eh∼QR^n(h)\hat R_n(Q)=\E_{h\sim Q}\hat R_n(h), for a loss in [0,1][0,1]. PAC-Bayes bounds charge for the KL divergence DKL(Q ∥ P)\KL(Q\,\|\,P) instead of for the size of H\mathcal H.

DerivationProof
  1. ES eλ(R(h)−R^n(h))≤eλ2/(8n)\E_S\,e^{\lambda(R(h)-\hat R_n(h))}\le e^{\lambda^2/(8n)}

    For a fixed hh, R(h)−R^n(h)R(h)-\hat R_n(h) is an average of nn independent mean-zero terms, each with range of length 1/n1/n. Hoeffding's lemma gives

  2. Eh∼P eλ(R(h)−R^n(h))≤eλ2/(8n)δ\E_{h\sim P}\,e^{\lambda(R(h)-\hat R_n(h))}\le\frac{e^{\lambda^2/(8n)}}\delta

    The prior does not depend on SS, so average over h∼Ph\sim P and swap the expectations. Markov's inequality then holds for the random variable Eh∼Peλ(R−R^n)\E_{h\sim P}e^{\lambda(R-\hat R_n)}: with probability at least 1−δ1-\delta,

  3. λ(R(Q)−R^n(Q))≤DKL(Q ∥ P)+λ28n+ln⁡1δ\lambda\big(R(Q)-\hat R_n(Q)\big)\le\KL(Q\,\|\,P)+\frac{\lambda^2}{8n}+\ln\frac1\delta

    On that event, change measure with f=λ(R−R^n)f=\lambda(R-\hat R_n). Because the event does not involve QQ, the inequality holds for every QQ at once, including ones chosen after seeing the data. Divide by λ\lambda:

Minimizing the right side over QQ is exactly the equality case of the change of measure: the Gibbs posterior Q⋆(h)∝P(h) e−λR^n(h)Q^\star(h)\propto P(h)\,e^{-\lambda\hat R_n(h)}, a Bayesian posterior with the likelihood replaced by an exponentiated training loss (for the log loss with λ=n\lambda=n it is formally Bayes' posterior). The best λ\lambda, about 8n(DKL+ln⁡(1/δ))\sqrt{8n(\KL+\ln(1/\delta))}, depends on QQ, and fixing it in advance is avoided with a union bound over a grid of λ\lambda values, at the cost of a logarithmic term (Alquier 2024, Theorem 2.4).

Finite classes are a special case. A point mass on hh has DKL(δh ∥ P)=ln⁡(1/P(h))\KL(\delta_h\,\|\,P)=\ln(1/P(h)), so each hypothesis pays for its own prior weight, a weighted union bound; a uniform prior gives back ln⁡∣H∣\ln\abs{\mathcal H}. Putting prior mass on hypotheses you consider simple is Occam's razor with a price list. For Gaussians, DKL(N(w,s2I) ∥ N(w0,σ2I))=12[p s2/σ2+∥w−w0∥2/σ2−p+pln⁡(σ2/s2)]\KL\big(\mathcal N(\mathbf w,s^2I)\,\|\,\mathcal N(\mathbf w_0,\sigma^2I)\big)=\frac12\big[p\,s^2/\sigma^2+\norm{\mathbf w-\mathbf w_0}^2/\sigma^2-p+p\ln(\sigma^2/s^2)\big] in Rp\R^p: the bound rewards solutions near the prior's center that tolerate a lot of noise, that is, flat ones.

Check your understanding

For a fixed λ>0\lambda>0, which posterior minimizes R^n(Q)+DKL(Q ∥ P)/λ\hat R_n(Q)+\KL(Q\,\|\,P)/\lambda?

  1. The point mass on the empirical risk minimizer
  2. The prior PP itself
  3. The uniform distribution over H\mathcal H
  4. The Gibbs posterior Q⋆(h)∝P(h) e−λR^n(h)Q^\star(h)\propto P(h)\,e^{-\lambda\hat R_n(h)}
Show the answer

D. The Gibbs posterior Q⋆(h)∝P(h) e−λR^n(h)Q^\star(h)\propto P(h)\,e^{-\lambda\hat R_n(h)}

Multiply by λ\lambda: minimizing EQ[λR^n]+DKL(Q ∥ P)\E_Q[\lambda\hat R_n]+\KL(Q\,\|\,P) is the change of measure with f=−λR^nf=-\lambda\hat R_n, whose equality case is dQ/dP∝e−λR^ndQ/dP\propto e^{-\lambda\hat R_n}. It interpolates between the prior (λ→0\lambda\to0) and the ERM point mass (λ→∞\lambda\to\infty).

Nonvacuous bounds for deep networks

Dziugaite & Roy (2017) took the kl bound literally. They trained fully connected networks with 0.3 to 2.4 million parameters by SGD on 55,000 MNIST images relabeled into two classes (digits 0–4 against 5–9). They then fit a Gaussian posterior with a learned diagonal covariance around the SGD solution, and a prior centered at the random initialization w0\mathbf w_0 (not at zero), with its variance chosen from a grid under a union bound. Finally they minimized the bound directly by SGD, with a differentiable surrogate for the 0–1 loss. Because the posterior can be wide only where the training loss stays low, the bound rewards flat regions around the solution.

Predict before you look

One of their networks had DKL(Q ∥ P)≈5,144\KL(Q\,\|\,P)\approx5{,}144 nats and stochastic training error R^n(Q)≈0.028\hat R_n(Q)\approx0.028, with n=55,000n=55{,}000 and δ=0.035\delta=0.035. Evaluate the Pinsker form R^n(Q)+(DKL+ln⁡(2n/δ))/(2n)\hat R_n(Q)+\sqrt{(\KL+\ln(2\sqrt n/\delta))/(2n)}.

Show the answer

0.244

(DKL+ln⁡(2n/δ))/n=(5144+9.5)/55,000≈0.0937(\KL+\ln(2\sqrt n/\delta))/n=(5144+9.5)/55{,}000\approx0.0937, so the bound is 0.028+0.0469≈0.2440.028+\sqrt{0.0469}\approx0.244. Inverting the kl form instead, solving kl(0.028 ∥ q)=0.0937\mathrm{kl}(0.028\,\|\,q)=0.0937, gives q≈0.161q\approx0.161, the value they reported for this network: the kl form exploits the small training error and Pinsker throws that away.

SettingResult
Dziugaite & Roy (2017), true labelsbounds of 0.16 to 0.22 on the stochastic network's error, whose measured test error was about 0.03, where VC bounds for the same architectures are vacuous
Dziugaite & Roy (2017), random labelsvacuous, as it must be when the labels carry no signal: the bound's square-root complexity term alone came to 1.35
Pérez-Ortiz, Rivasplata, Shawe-Taylor & Szepesvári (2021)networks trained by minimizing PAC-Bayes bounds with data-dependent priors, with risk certificates near 0.03 on MNIST (as summarized by Alquier, 2024)
A bound for the randomized (Gibbs) network, not the deterministic one SGD returned. With data-dependent priors, learned on one part of the data and used for the bound on the other, a recurring criticism is that the prior becomes so good that the PAC-Bayes step adds little beyond a held-out bound (Alquier, 2024).
RouteWhat it measuresStrengthWeakness
Uniform convergence (VC, Rademacher)the class, possibly on the samplealgorithm-free; tight for small classesvacuous when the class can fit noise
Stabilitysensitivity of the algorithm to one examplecovers regularized ERM and SGD; learnability in general settingslabel-independent; nonconvex bounds grow with training time
Compressionhow few examples determine the outputdimension-free (support vectors)needs an explicit reconstruction
PAC-BayesKL distance from a data-independent priornonvacuous numbers for some deep networksbounds a randomized predictor; depends on the prior
Information-theoretic bounds connect the routes: if ℓ(w,Z)\ell(\mathbf w,Z) is σ\sigma-sub-Gaussian for every w\mathbf w (a loss in [a,b][a,b] has σ=(b−a)/2\sigma=(b-a)/2), the expected gap is at most 2σ2I(S;W)/n\sqrt{2\sigma^2I(S;W)/n} (Xu & Raginsky, 2017), and conditional mutual information can be bounded through VC dimension, compression schemes and differential privacy alike (Steinke & Zakynthinou, 2020).

Key takeaways

  • Uniform stability controls the expected gap exactly, through the replace-one identity; with high probability the gap is at most 2β+(4nβ+M)ln⁡(1/δ)/(2n)2\beta+(4n\beta+M)\sqrt{\ln(1/\delta)/(2n)}, sharpened to about βlog⁡nlog⁡(1/δ)+log⁡(1/δ)/n\beta\log n\log(1/\delta)+\sqrt{\log(1/\delta)/n}.
  • Regularized ERM is stable with β≤σ2κ2/(2λn)\beta\le\sigma^2\kappa^2/(2\lambda n), and SGD on convex losses with εstab≤(2G2/n)∑tηt\varepsilon_{\mathrm{stab}}\le(2G^2/n)\sum_t\eta_t; such bounds are label-independent, so they cannot separate true from random labels.
  • A compression scheme of size kk gives R(h)≤(kln⁡n+ln⁡((n+1)/δ))/(n−k)R(h)\le(k\ln n+\ln((n+1)/\delta))/(n-k) with no reference to dimension; every VC class compresses to size 2O(d)2^{\order d}, and whether O(d)\order d suffices is open.
  • PAC-Bayes: R(Q)≤R^n(Q)+λ/(8n)+(DKL(Q ∥ P)+ln⁡(1/δ))/λR(Q)\le\hat R_n(Q)+\lambda/(8n)+(\KL(Q\,\|\,P)+\ln(1/\delta))/\lambda by change of measure, minimized by the Gibbs posterior; the kl form gives 1/n1/n rates at zero training error.
  • Optimizing a Gaussian posterior around SGD's solution gave the first nonvacuous bounds for deep networks (0.16 to 0.22 against a test error of about 0.03), and vacuous ones on random labels, as it should.

Check yourself

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

Question 1

An algorithm is ε\varepsilon-uniformly stable in the replace-one sense. Which conclusion is guaranteed?

  1. Its test error is at most ε\varepsilon
  2. With probability at least 1−δ1-\delta, its generalization gap is at most ε\varepsilon
  3. Its expected generalization gap, over random training sets, is at most ε\varepsilon in absolute value
  4. Its training error is at most ε\varepsilon
Show the answer

C. Its expected generalization gap, over random training sets, is at most ε\varepsilon in absolute value

The replace-one identity writes the expected gap as an average change in loss when one example is swapped, which uniform stability bounds by ε\varepsilon. High-probability statements need more (Bousquet–Elisseeff and its successors), and nothing bounds the training or test error themselves.

Question 2

Hardt, Recht and Singer's bounds depend on the number of steps, the step sizes, the Lipschitz and smoothness constants and nn. Why can they not explain why a network that can fit random labels still generalizes on true labels?

  1. Because SGD is never uniformly stable
  2. Because nothing in the bound depends on the labels: with the same training run on random labels the same bound would hold, yet the true gap there is huge (about 90% with ten balanced classes)
  3. Because the bounds apply only to convex losses
  4. Because uniform stability implies uniform convergence, which fails for such networks
Show the answer

B. Because nothing in the bound depends on the labels: with the same training run on random labels the same bound would hold, yet the true gap there is huge (about 90% with ten balanced classes)

A label-independent bound must be at least as large as the gap in the worst labeling, including random ones, so it cannot certify the small gap on real labels (Zhang et al., 2017). Explanations must use properties of the data, such as how far the weights move or how flat the solution is.

Question 3

An RBF-kernel SVM separates n=2,000n=2{,}000 training points with 15 support vectors. With δ=0.05\delta=0.05, what does the compression bound (kln⁡n+ln⁡((n+1)/δ))/(n−k)\big(k\ln n+\ln((n+1)/\delta)\big)/(n-k) certify?

  1. Error at most about 0.0630.063, although the RBF feature space is infinite-dimensional
  2. Nothing, because the feature space is infinite-dimensional
  3. Error exactly 15/2000=0.007515/2000=0.0075
  4. Error at most 15/200015/2000 with probability at least 0.950.95
Show the answer

A. Error at most about 0.0630.063, although the RBF feature space is infinite-dimensional

(15ln⁡2000+ln⁡(2001/0.05))/1985=(114.0+10.6)/1985≈0.063(15\ln2000+\ln(2001/0.05))/1985=(114.0+10.6)/1985\approx0.063. The bound counts examples needed to rebuild the classifier, not dimensions. The ratio NSV/(n+1)N_{\mathrm{SV}}/(n+1) appears in Vapnik's leave-one-out bound, but only as a bound on the expected error.

Question 4

In the PAC-Bayes bound, what happens if the learner simply reports the posterior Q=PQ=P, the prior?

  1. The bound becomes vacuous
  2. DKL(Q ∥ P)\KL(Q\,\|\,P) is infinite
  3. The bound does not apply, because QQ must depend on the data
  4. DKL(Q ∥ P)=0\KL(Q\,\|\,P)=0, and the bound reduces to a Hoeffding-type bound for a randomized predictor fixed before seeing the data
Show the answer

D. DKL(Q ∥ P)=0\KL(Q\,\|\,P)=0, and the bound reduces to a Hoeffding-type bound for a randomized predictor fixed before seeing the data

With Q=PQ=P the complexity term vanishes and only ln⁡(1/δ)\ln(1/\delta) or ln⁡(2n/δ)\ln(2\sqrt n/\delta) remains, exactly what a hypothesis chosen in advance would pay. Every nat of KL is the price of using the data to move away from the prior.

Further reading

  • O. Bousquet & A. Elisseeff, "Stability and generalization," Journal of Machine Learning Research 2, 499–526 (2002).
  • M. Hardt, B. Recht & Y. Singer, "Train faster, generalize better: stability of stochastic gradient descent," Proceedings of the 33rd International Conference on Machine Learning, PMLR 48, 1225–1234 (2016).
  • S. Moran & A. Yehudayoff, "Sample compression schemes for VC classes," Journal of the ACM 63(3) (2016).
  • G. K. Dziugaite & D. M. Roy, "Computing nonvacuous generalization bounds for deep (stochastic) neural networks with many more parameters than training data," Proceedings of the 33rd Conference on Uncertainty in Artificial Intelligence (2017).
  • P. Alquier, "User-friendly introduction to PAC-Bayes bounds," Foundations and Trends in Machine Learning 17(2), 174–303 (2024).