Foundations to Frontier
Tests
B. Statistical Learning Theory › B2 Generalization theory

PAC-Bayes Bounds

PAC-Bayes bounds control the risk of a randomized classifier, one drawn from a learned posterior distribution Q over hypotheses, through its training error plus KL(Q‖P) against a prior P chosen before seeing the data. They work for continuous, overparameterized models where VC bounds are vacuous, they reward flat or noise-robust solutions, and with a data-dependent prior they give some of the tightest generalization certificates known (including non-vacuous bounds for neural networks). This topic covers the McAllester and Seeger (kl) forms, the change-of-measure proof, Gaussian posteriors for linear classifiers, and in the research exercise shows how learning the prior on half the data cuts the bound in half.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Bet on a crowd, not on one classifier

Instead of picking one classifier, imagine a whole cloud of slightly different ones and, at prediction time, asking a random member of the cloud. Before seeing any data, you write down a prior guess of where good classifiers live. After training, the cloud (the posterior) moves to where the data says. PAC-Bayes says the cloud’s true error is at most its training error plus a penalty for how far it moved from your prior. A wide cloud that didn’t have to move far, meaning a solution robust to perturbation, generalizes well.

PAC-Bayes: a cloud of classifiers, priced by its distance from the prior
weights: dashed prior, solid posterior + samples0.10.20.30.40.5top: bound · lower: train Gibbs · dashed: testr →
train Gibbs error 0.145 · KL 8.00 · PAC-Bayes-kl bound 0.271 (square-root form 0.296) · test Gibbs error 0.145 · best r ≈ 3.6 gives 0.271
Instead of one classifier, PAC-Bayes bounds the average error of a random classifier drawn from a posterior Q. Moving the cloud further out (larger r) makes it more confident and lowers its training error, but costs KL(Q‖P) = r²/2 against the prior at the origin, and the bound balances the two. A prior learned on half the data starts near where the posterior ends, so the KL is tiny and the bound (evaluated on the other half) becomes much tighter. The bound uses only 316 points yet with 95% probability it stays above the true (dashed test) curve.
Why engineers care

PAC-Bayes gives some of the few non-vacuous generalization certificates for neural networks, and it formalizes the intuition that “flat minima generalize”. It also underlies the analysis of Bayesian and ensemble methods, which are used for uncertainty-aware perception and safe learning-based control.

📝 Quick check (Level 0)
0/3 answered
  1. 1. PAC-Bayes bounds the error of…
  2. 2. The prior must be chosen…
  3. 3. The penalty measures…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Gibbs risk

For a distribution Q over hypotheses, the Gibbs risk is R(Q)=Eh∼QR(h)R(Q) = \mathbb E_{h\sim Q}R(h) and the empirical version is R^(Q)=Eh∼QR^(h)\hat R(Q) = \mathbb E_{h\sim Q}\hat R(h).

The McAllester bound

For any prior P fixed before the data, with probability at least 1 − δ, simultaneously for all posteriors Q:

R(Q)≤R^(Q)+KL(Q∥P)+ln⁡(2m/δ)2m.R(Q)\le\hat R(Q) + \sqrt{\frac{\mathrm{KL}(Q\|P) + \ln(2\sqrt m/\delta)}{2m}}.

Compare with the finite-class bound (ln⁡∣H∣+ln⁡(1/δ))/2m\sqrt{(\ln|\mathcal H| + \ln(1/\delta))/2m}. A point mass on one of |H| hypotheses under a uniform prior has KL=ln⁡∣H∣\mathrm{KL} = \ln|\mathcal H|, so PAC-Bayes generalizes Occam’s razor to continuous classes.

Gaussians

For Q=N(μ,σ2I)Q = \mathcal N(\mu, \sigma^2I) and P=N(μ0,σ2I)P = \mathcal N(\mu_0, \sigma^2I), the KL is ∥μ−μ0∥2/(2σ2)\|\mu - \mu_0\|^2/(2\sigma^2). Small moves from the prior and large posterior variance (flatness) are cheap.

📝 Quick check (Level 1)
0/3 answered
  1. 1. KL(N(μ, I) ‖ N(0, I)) for μ = (3, 4):
  2. 2. The bound holds…
  3. 3. With a point-mass posterior and a uniform prior on a finite class, KL equals…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

The tighter kl form (Seeger, Maurer)

kl(R^(Q) ∥ R(Q))≤KL(Q∥P)+ln⁡(2m/δ)m,\mathrm{kl}\big(\hat R(Q)\,\big\|\,R(Q)\big)\le\frac{\mathrm{KL}(Q\|P) + \ln(2\sqrt m/\delta)}m,

where kl is the binary KL divergence. Inverting it numerically gives a bound that is much tighter when the training error is small (it behaves like 1/m instead of 1/√m). Pinsker’s inequality kl(q∥p)≥2(q−p)2\mathrm{kl}(q\|p)\ge2(q - p)^2 recovers McAllester.

Linear classifiers with Gaussian posteriors

For w′∼N(w,σ2I)w'\sim\mathcal N(w, \sigma^2I), the stochastic classifier sign(w′·x) errs on (x, y) with probability exactly

Φ ⁣(−y w⋅xσ∥x∥),\Phi\!\left(-\frac{y\,w\cdot x}{\sigma\|x\|}\right),

a smoothed, margin-based loss. Scaling the mean up (larger r = ‖w‖/σ) lowers this loss but raises KL = r²/2. The bound picks the balance, and this is the origin of PAC-Bayes margin bounds.

Choosing the prior

The prior can be anything that doesn’t look at the evaluation sample. Use a grid of priors with a union bound, or learn the prior on a separate part of the data (a data-dependent prior). The second is the trick behind non-vacuous bounds for deep networks (Dziugaite and Roy, 2017).

🐍 Exercise 1: a PAC-Bayes bound for logistic regression
Train logistic regression on noisy linear data, put a Gaussian posterior N(r·û, I) around the learned direction, compute its exact Gibbs error and KL to the prior N(0, I), and evaluate both the McAllester and the kl-inverse bounds, optimizing r over a grid with a union bound.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The kl form is tighter mainly when…
  2. 2. Gibbs error of a Gaussian linear classifier is a…
  3. 3. A data-dependent prior is valid if…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

The change of measure (Donsker–Varadhan)

For any measurable f and distributions Q ≪ P:

EQ[f]≤KL(Q∥P)+ln⁡EP[ef].\mathbb E_Q[f]\le\mathrm{KL}(Q\|P) + \ln\mathbb E_P[e^{f}].

Proof of the kl bound

Take f(h)=m kl(R^(h)∥R(h))f(h) = m\,\mathrm{kl}(\hat R(h)\|R(h)). Since P doesn’t depend on S, Maurer’s lemma gives ESEP[ef]≤2m\mathbb E_S\mathbb E_P[e^{f}]\le2\sqrt m, and Markov’s inequality makes EP[ef]≤2m/δ\mathbb E_P[e^f]\le2\sqrt m/\delta with probability at least 1 − δ. On that event, for every Q, change of measure gives m EQkl(R^(h)∥R(h))≤KL(Q∥P)+ln⁡(2m/δ)m\,\mathbb E_Q\mathrm{kl}(\hat R(h)\|R(h))\le\mathrm{KL}(Q\|P) + \ln(2\sqrt m/\delta). Joint convexity of kl moves the expectation inside: kl(R^(Q)∥R(Q))≤EQkl(⋅)\mathrm{kl}(\hat R(Q)\|R(Q))\le\mathbb E_Q\mathrm{kl}(\cdot). ∎

Because the high-probability event doesn’t involve Q, the bound holds for all posteriors at once, including the one chosen after looking at the data.

Connections

  • Bayes: minimizing R^(Q)+KL(Q∥P)/λ\hat R(Q) + \mathrm{KL}(Q\|P)/\lambda over Q gives the Gibbs posterior Q∝P e−λR^Q\propto P\,e^{-\lambda\hat R}. With log-loss this is Bayesian inference, and the objective is the (negative) ELBO.
  • Flat minima: a large σ with low Gibbs error means that loss stays low under weight noise.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Donsker–Varadhan bounds E_Q[f] by…
  2. 2. The bound holds for all Q because…
  3. 3. The optimal posterior for R̂ + KL/λ is…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

PAC-Bayes, measured

Exercise 1 (logistic regression, d = 20, 5% label noise). With prior N(0, I), the best kl-form bound at m = 2000 is 0.278 (best r = 12, KL = 72), while the posterior’s true Gibbs error is 0.149. The bound is non-vacuous but about twice the truth. At m = 500 it is 0.367 against 0.200. The square-root (McAllester) form is always looser.

Exercise 2: data-dependent priors. Learn the prior’s direction on the first half of the data and evaluate the bound on the second half. The posterior barely moves from such a prior: KL falls from 32 to 4.4 at m = 500, and from 72 to 1.5 at m = 2000. The bound drops from 0.382 to 0.242 at m = 500, and from 0.253 to 0.121 at m = 2000, close to the true 0.098. Using half the data well is worth more than using all of it with an uninformed prior.

🐍 Exercise 2 (research): data-dependent priors
Compare the PAC-Bayes-kl bound with a fixed prior N(0, I) (evaluated on all m points) against a prior N(r·û₁, I) learned on the first half (evaluated on the second half), for several m. Check that both stay above the true Gibbs risk and measure how much the KL shrinks.

PAC-Bayes in AI, robotics and aerospace

  • Deep learning: non-vacuous bounds for MNIST-scale networks (Dziugaite and Roy), and bounds via compression.
  • Bayesian deep learning: variational inference as PAC-Bayes bound minimization; ensembles and dropout.
  • Robotics: PAC-Bayes control (Majumdar et al.) certifies the success rate of learned policies on novel environments, such as obstacle avoidance for drones.
  • Meta-learning: learning the prior across tasks (PAC-Bayes meta-learning).

References

  1. D. McAllester (1999). “PAC-Bayesian model averaging.” COLT.
  2. M. Seeger (2002). “PAC-Bayesian generalisation error bounds for Gaussian process classification.” JMLR, 3.
  3. A. Maurer (2004). “A note on the PAC Bayesian theorem.” arXiv:cs/0411099.
  4. G. K. Dziugaite, D. M. Roy (2017). “Computing nonvacuous generalization bounds for deep (stochastic) neural networks with many more parameters than training data.” UAI.
  5. A. Majumdar, A. Farid, A. Sonar (2021). “PAC-Bayes control: learning policies that provably generalize to novel environments.” IJRR, 40.
  6. P. Alquier (2024). “User-friendly introduction to PAC-Bayes bounds.” Foundations and Trends in Machine Learning, 17.