Foundations to Frontier
Tests

Bayesian Neural Networks

A Bayesian neural network puts a prior distribution on the weights and predicts by averaging over the posterior, p(y | x, D) = ∫ p(y | x, θ) p(θ | D) dθ, instead of trusting one trained weight vector. The averaging is what produces epistemic uncertainty: networks that fit the data equally well disagree away from it. The posterior over thousands to billions of weights has no closed form, so every practical method in this subsection (variational inference, MC dropout, Laplace, ensembles, SWAG) is an approximation to it, and full-batch Hamiltonian Monte Carlo is the reference they are judged against. This page covers priors over functions and Neal's infinite-width limit, the posterior predictive and its aleatoric/epistemic split, why naive sampling fails, and HMC for networks. In the exercises you sample tanh-network priors: the output's excess kurtosis fell from 1.3 with one hidden unit to about 0 with 1024 while its variance stayed near 0.53. Importance sampling from the prior collapsed from an effective sample size of 1708 (of 20000 draws) after one data point to 1 after sixteen. Full-batch HMC on a 31-parameter network gave an epistemic std of 0.05 on the data, 0.24 in a gap between the two data clusters and 0.26 far away.

Level 0: Foundation

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

Many networks instead of one

Training a network normally gives one set of weights. But many different weight settings fit the same data almost equally well, and they agree where there is data and disagree where there is none. A Bayesian neural network keeps all of them, each weighted by how plausible it is, and predicts with their average. The spread of their predictions is the network saying “I don’t know”. The demo draws random networks from a prior: with more hidden units they turn into smooth random functions.

Bayesian deep learning lab
Six functions drawn from the prior of a tanh network with 4 hidden units. Output weights have variance 1/H, so the spread of f stays the same as H grows, but the draws become smoother sums of many small bumps: a Gaussian process in the limit.
Why engineers care

A landing-site classifier or a learned dynamics model must flag inputs unlike its training data; one trained network gives a confident answer everywhere, a posterior over networks gives an honest spread.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A Bayesian neural network has a distribution over…
  2. 2. Its predictions average over…
  3. 3. Networks that fit the data agree most…

Level 1: Beginner

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

By hand

  • Model: prior p(θ), likelihood p(D | θ) from the network f_θ (Gaussian for regression, categorical for classification), posterior p(θ | D) ∝ p(D | θ) p(θ).
  • Posterior predictive: p(y | x, D) = ∫ p(y | x, θ) p(θ | D) dθ ≈ (1/S) Σ_s p(y | x, θ_s) with θ_s from the posterior.
  • Two kinds of uncertainty: by the law of total variance, Var[y] = E_θ[noise variance] (aleatoric) + Var_θ[f_θ(x)] (epistemic). Only the epistemic part shrinks with more data.
  • Priors over functions: a weight prior induces a prior over functions. With output weights ~ N(0, σ²/H), a one-hidden-layer network becomes a Gaussian process as the width H → ∞ (Neal, 1996); deep versions give the NNGP kernels (Lee et al., 2018; Matthews et al., 2018).
  • Why it is hard: the posterior has millions of dimensions, many symmetric modes (permuting hidden units), and the likelihood needs the whole dataset.
  • Inference methods: HMC and NUTS (reference, full batch); stochastic-gradient MCMC (SGLD, Welling and Teh, 2011; SGHMC, Chen et al., 2014); variational inference; Laplace; MC dropout; deep ensembles; SWAG. The rest of this subsection covers them.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Epistemic variance is…
  2. 2. Neal (1996) showed wide one-layer BNNs become…
  3. 3. Only this part of the uncertainty shrinks with more data:

Level 2: Intermediate

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

Sampling a BNN posterior

Exercise 1.

  • The network’s output for a parameter vector.
  • The negative log posterior U(θ) (Gaussian likelihood and prior).
  • Leapfrog steps for Hamiltonian Monte Carlo.

Practice.

  • For small networks, run NUTS in NumPyro or BlackJAX on the full batch; check R-hat and effective sample size across several chains.
  • For large networks, full-batch HMC is a research tool (Izmailov et al., 2021 used hundreds of TPUs); use the approximations on the following pages and compare them to HMC on a small problem.
  • Choose priors on the function scale: scale output weights by 1/√width and check prior predictive draws before fitting.
  • Report the epistemic part separately: average the noise variance, take the variance of the means.
  • Libraries in 2026: NumPyro, BlackJAX, Pyro, TensorFlow Probability, the posteriors library for PyTorch, laplace-torch.
import jax.numpy as jnp, jax.random as jr, numpyro, numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS

def model(x, y=None, H=10):
    w = numpyro.sample("w", dist.Normal(0, 2).expand([H]))
    b = numpyro.sample("b", dist.Normal(0, 1).expand([H]))
    v = numpyro.sample("v", dist.Normal(0, 1 / jnp.sqrt(H)).expand([H]))
    c = numpyro.sample("c", dist.Normal(0, 1))
    f = jnp.tanh(x[:, None] * w + b) @ v + c
    numpyro.sample("y", dist.Normal(f, 0.1), obs=y)

mcmc = MCMC(NUTS(model), num_warmup=500, num_samples=1000, num_chains=4)
mcmc.run(jr.PRNGKey(0), x_train, y_train); mcmc.print_summary()
🐍 Exercise 1: network, posterior energy and leapfrog
Compute a tanh network's output, its negative log posterior, and leapfrog steps for HMC.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A standard convergence check for MCMC chains:
  2. 2. To keep the prior output scale fixed as width grows, scale output weights by…
  3. 3. Full-batch HMC for ResNets is…

Level 3: Advanced

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

Theory

Infinite width (Neal, 1996). f(x) = Σ_j v_j φ_j(x) with independent v_j ~ N(0, σ_v²/H) and i.i.d. hidden units. Each term has variance σ_v² E[φ²]/H, so Var f(x) = σ_v² E[φ(x)²] for every H. The terms are i.i.d., so by the central limit theorem any finite set of outputs becomes jointly Gaussian as H → ∞: a Gaussian process with kernel k(x, x’) = σ_v² E[φ(x) φ(x’)]. The excess kurtosis of a sum of H i.i.d. terms is that of one term divided by H.

Weight-space vs function-space. Many weight vectors give the same function (permuting hidden units, flipping signs of tanh units), so the posterior has at least H! 2^H equivalent modes. What matters for prediction is the distribution of functions, which is why methods that sample several modes (ensembles) can predict well even with poor weight-space coverage.

Why naive Monte Carlo fails. Self-normalised importance sampling with the prior as proposal has weights ∝ p(D | θ). Each data point multiplies the likelihood, so the weights’ spread grows exponentially with n and the effective sample size (Σw)²/Σw² collapses to 1. HMC avoids this by following the gradient of log p(θ | D): leapfrog integration nearly conserves the Hamiltonian H = U + ½‖p‖², it is reversible and volume preserving, so a Metropolis test on the energy error makes the chain exact.

Cold posteriors. Wenzel et al. (2020) found that tempering the posterior (p(θ | D)^(1/T), T below 1) often predicts better than the Bayes posterior for large image models; later work traced much of this to data augmentation and misspecified priors and likelihoods (Izmailov et al., 2021; Fortuin et al., 2022).

📝 Quick check (Level 3)
0/3 answered
  1. 1. With v ~ N(0, σ²/H), Var f(x) as H grows…
  2. 2. Importance sampling from the prior fails because…
  3. 3. Leapfrog is used in HMC because it is…

Level 4: Expert

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

What does a neural-network prior look like, and what does the exact posterior’s uncertainty look like?

History.

  • D. MacKay, “A practical Bayesian framework for backpropagation networks” (1992); R. Neal, Bayesian Learning for Neural Networks (1996), with HMC and the infinite-width limit.
  • M. Welling and Y. W. Teh, “Bayesian learning via stochastic gradient Langevin dynamics” (2011).
  • P. Izmailov et al., “What are Bayesian neural network posteriors really like?” (2021): full-batch HMC on CIFAR-10 ResNets as a reference.
  • F. Wenzel et al., “How good is the Bayes posterior in deep neural networks really?” (2020): the cold-posterior effect.

Exercise 2. One-hidden-layer tanh networks, w ~ N(0, 4), b ~ N(0, 1), v ~ N(0, 1/H).

  • Prior: 2000 draws of f(0.5) for widths 1, 4, 64, 1024.
  • Importance sampling: 20000 prior draws (H = 10) weighted by the likelihood of 1, 4 or 16 points.
  • HMC: 24 points of sin(3x) + N(0, 0.1²) with x in two clusters, [−1, −0.35] and [0.35, 1]; 31 parameters; 900 iterations of 12 leapfrog steps (step 0.008), 300 burn-in, every 5th kept. Epistemic std of f averaged over x in [0.4, 0.95] (data), [−0.15, 0.15] (gap) and [1.6, 2] (far).
width H Var f(0.5) excess kurtosis
1 0.49 1.3
4 0.53 0.4
64 0.53 −0.0
1024 0.53 −0.1
data points ESS of 20000 prior draws
1 1708
4 57
16 1
HMC posterior data gap far
epistemic std of f 0.05 0.24 0.26
  • Neal’s limit: the output variance stayed near 0.5 while the excess kurtosis went to 0 (Gaussian).
  • Prior sampling is hopeless: the effective sample size fell exponentially with data.
  • HMC (acceptance 0.93) fitted the data (RMSE of the mean against sin(3x): 0.07) and its uncertainty was five times larger in the gap and far away than on the data. This is the behaviour the approximations on the next pages try to reproduce.
  • Seeds 1 and 2: gap std 0.31 and 0.21, far 0.26 and 0.29, data 0.06 and 0.05; ESS with 16 points 1 and 1; kurtosis at H = 1: 1.2 and 1.3.
  • Caveat: a single chain explores one region of a multimodal posterior; four chains and R-hat would be the honest check.
🐍 Exercise 2 (research): what does a neural-network prior look like, and what does the exact posterior's uncertainty look like?
Sample network priors at several widths, try importance sampling from the prior, and run full-batch HMC on a small network to get the reference posterior uncertainty.

Bayesian neural networks in AI, robotics and aerospace

  • Safe exploration: model-based reinforcement learning (PILCO’s successors, PETS) uses uncertainty over learned dynamics to avoid unvisited states.
  • Scientific ML: Bayesian PINNs and surrogate models report credible intervals for inferred physical parameters.
  • Perception: uncertainty on depth, segmentation and detection flags inputs (fog, glare, new terrain) the model has not seen.

References

  1. R. M. Neal (1996). Bayesian Learning for Neural Networks. Springer.
  2. D. J. C. MacKay (1992). “A practical Bayesian framework for backpropagation networks.” Neural Computation.
  3. P. Izmailov, S. Vikram, M. D. Hoffman, A. G. Wilson (2021). “What are Bayesian neural network posteriors really like?” ICML.
  4. F. Wenzel et al. (2020). “How good is the Bayes posterior in deep neural networks really?” ICML.
  5. V. Fortuin et al. (2022). “Bayesian neural network priors revisited.” ICLR.