Foundations to Frontier
Tests

Laplace Approximation for Neural Networks

The Laplace approximation turns a trained network into a Bayesian one after the fact. It fits a Gaussian centred at the MAP weights, with precision equal to the curvature (Hessian) of the negative log posterior there. With the generalised Gauss–Newton curvature and a linearised predictive, it gives calibrated error bars at the cost of one extra pass over the data. It also estimates the marginal likelihood, which can tune the prior precision without a validation set. MacKay proposed it in 1992; the laplace-torch library (Daxberger et al., 2021) made it practical with diagonal, Kronecker-factored (KFAC) and last-layer curvature. This page covers the Gaussian at the mode, GGN vs Hessian, linearised vs sampled predictives, curvature structures, and evidence-based tuning. In the exercises you build GGN precisions, linearised variances and Laplace evidences. You then apply Laplace to a converged 31-parameter network. The evidence picked the prior precision with the best test NLL (λ = 0.1). The full-GGN linearised predictive had std 0.04 on the data, 0.14 in the gap and 1.67 far away. A diagonal approximation was too wide on the data (0.08) and far too narrow away from it (0.11). Sampling the nonlinear network from the same Gaussian wrecked the predictions: test NLL 3.06 against −0.73 for the MAP alone.

Level 0: Foundation

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

A Gaussian at the bottom of the valley

Train a network as usual. At the weights you end up with, the loss sits at the bottom of a valley. If the valley is steep in some direction, the data pin that weight down; if it is flat, many values are about as good. The Laplace approximation turns this into a bell curve over weights: narrow where the valley is steep, wide where it is flat. Uncertainty comes for free after training. The demo compares an exact posterior with its Laplace bell curve; with little data they differ.

Bayesian deep learning lab
Solid: the exact posterior of a success probability after 3 of 10 successes (flat prior). Dashed: the Laplace approximation, N(mode 0.30, std 0.145). With few trials or k near 0 or n the posterior is skewed and the Gaussian misses it; with many trials they agree.
Why engineers care

Laplace adds uncertainty to an already trained and certified network without retraining it, which matters when the network is fixed, for example in a flight-qualified perception stack.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The Laplace approximation is centred at…
  2. 2. Its width comes from…
  3. 3. It requires retraining?

Level 1: Beginner

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

By hand

  • Approximation: p(θ | D) ≈ N(θ*, Λ⁻¹) with θ* the MAP and Λ = −∇² log p(θ | D) at θ* (second-order Taylor expansion of the log posterior).
  • GGN: for Gaussian likelihood, Λ ≈ JᵀJ/σ² + λI with J the Jacobian of the outputs; always positive semi-definite, unlike the Hessian of a nonconvex loss. For classification replace 1/σ² by the softmax Hessian.
  • Predictive: the linearised (GLM) predictive uses f(x; θ) ≈ f(x; θ*) + J_x (θ − θ*), so f(x) ~ N(f(x; θ*), J_x Λ⁻¹ J_xᵀ). For classification add the probit approximation sigmoid(μ/√(1 + πv/8)).
  • Curvature structures: full (P² memory, small nets), diagonal, KFAC per layer (Ritter, Botev and Barber, 2018), low rank, or last layer only (Kristiadi, Hein and Hennig, 2020).
  • Evidence: log p(D) ≈ log p(D | θ*) + log p(θ*) + (P/2) log 2π − ½ log det Λ; maximise it over the prior precision λ (MacKay, 1992; Immer et al., 2021).
  • Tooling: laplace-torch (Daxberger et al., 2021, “Laplace Redux”); post-hoc, after any training.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The GGN is preferred to the Hessian because it is…
  2. 2. The linearised predictive variance is…
  3. 3. KFAC approximates each layer's curvature as…

Level 2: Intermediate

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

Laplace in practice

Exercise 1.

  • The GGN precision for regression.
  • The linearised predictive variance.
  • The Laplace evidence.

Practice.

  • Train to convergence first: Laplace is a local approximation at a mode.
  • Start with last-layer KFAC Laplace and the GLM predictive; it is cheap and usually most of the gain.
  • Tune the prior precision by marginal likelihood (or validation NLL); an untuned λ is the most common failure.
  • Use the linearised (GLM) predictive or probit, not samples pushed through the full network.
  • Check calibration and out-of-distribution behaviour, and compare with a small ensemble.
from laplace import Laplace
la = Laplace(model, "regression", subset_of_weights="last_layer", hessian_structure="kron")
la.fit(train_loader)                                  # one pass to accumulate the curvature
la.optimize_prior_precision(method="marglik")         # tune λ by the Laplace evidence
f_mu, f_var = la(x_test)                              # linearised (GLM) predictive
pred_std = (f_var.squeeze() + la.sigma_noise ** 2).sqrt()
🐍 Exercise 1: GGN, linearised variance and evidence
Build the GGN precision, compute linearised predictive variances, and estimate the Laplace log evidence.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Before fitting a Laplace approximation the network should be…
  2. 2. A cheap default structure:
  3. 3. The prior precision is tuned by…

Level 3: Advanced

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

Theory

Derivation. Expand Ψ(θ) = log p(θ | D) around the MAP: Ψ(θ) ≈ Ψ(θ*) − ½ (θ − θ*)ᵀ Λ (θ − θ*), since the gradient vanishes there. Exponentiating gives a Gaussian. Integrating the same expansion gives the evidence: p(D) = ∫ p(D, θ) dθ ≈ p(D, θ*) (2π)^(P/2) det(Λ)^(−1/2).

Why GGN with the linearised predictive. For squared error, ∇²L = JᵀJ/σ² + Σ_i (e_i/σ²) ∇²f_i. Dropping the second term gives the GGN, which is exactly the Hessian of the linearised model f(θ*) + J(θ − θ*). So the GGN Laplace is an exact Gaussian posterior for the linearised network, and predictions should use that linear model (Immer, Korzepa and Bauer, 2021). Pushing samples through the nonlinear network instead mixes two models: the Gaussian is wide in directions where the linearised model is flat, but the true network is not flat there, so samples produce wild functions and the predictive underfits.

Diagonal vs full. For a precision matrix Λ, the diagonal approximation uses 1/Λ_kk, which is smaller than the true marginal variance (Λ⁻¹)_kk: overconfident in poorly determined directions. Yet in function space it can be too wide on the data, because it ignores correlations that cancel there.

Laplace corrects ReLU overconfidence. Kristiadi et al. (2020) proved that even a last-layer Gaussian makes a ReLU classifier’s confidence tend to below 1 far from the data, while the MAP network becomes arbitrarily confident.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The GGN is the exact Hessian of…
  2. 2. Sampling the nonlinear network from a GGN Laplace tends to…
  3. 3. Diagonal variance 1/Λ_kk is, compared with (Λ⁻¹)_kk,…

Level 4: Expert

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

Which Laplace variants give good uncertainty, and can the evidence choose the prior?

History.

  • D. MacKay, “A practical Bayesian framework for backpropagation networks” (1992): Laplace and the evidence for networks.
  • H. Ritter, A. Botev, D. Barber, “A scalable Laplace approximation for neural networks” (KFAC Laplace, 2018).
  • A. Immer, M. Korzepa, M. Bauer, “Improving predictions of Bayesian neural nets via local linearization” (2021).
  • E. Daxberger et al., “Laplace Redux: effortless Bayesian deep learning” (2021).

Exercise 2. 1-10-1 tanh network (31 parameters), 40 points of sin(3x) + N(0, 0.1²) with a gap, prior N(0, λ⁻¹ I). The MAP is found by Levenberg–Marquardt to gradient below 10⁻⁵ for λ in 0.01, 0.1, 1, 10. Then GGN Laplace at the evidence-chosen λ: full, diagonal, last-layer (11 parameters), all with the linearised predictive, and full with samples through the network. Test NLL on 400 new points from the training distribution.

λ log evidence test NLL
0.01 −10.6 −0.64
0.1 14.0 −0.75
1 6.9 −0.70
10 −61.6 −0.24
at λ = 0.1 std data std gap std far test NLL
MAP only 0 0 0 −0.73
full GGN, linearised 0.04 0.14 1.67 −0.75
diagonal, linearised 0.08 0.06 0.11 −0.73
last layer, linearised 0.03 0.07 0.23 −0.75
full GGN, network samples 8.37 8.44 8.69 3.06
  • Evidence works here: it chose the λ with the best test NLL on all three seeds.
  • Full GGN + linearisation looks like HMC qualitatively: small on the data, larger in the gap, much larger far away (perhaps too large: tanh units with weak prior extrapolate steeply).
  • Diagonal is wrong in both directions; last layer is reasonable on the data but grows far less away from it.
  • Sampling the network is a disaster, as Immer et al. reported.
  • In-distribution NLL barely changes between variants: the differences are about behaviour away from the data.
  • Seeds 1 and 2: full GGN std 0.05/0.32/2.87 and 0.04/0.20/2.13; network sampling NLL 3.04 and 3.04.
🐍 Exercise 2 (research): which Laplace variants give good uncertainty, and can the evidence choose the prior?
Fit MAPs for several prior precisions, choose one by the Laplace evidence, and compare full, diagonal, last-layer and sampled Laplace predictives.

Laplace in AI, robotics and aerospace

  • Post-hoc uncertainty for fixed models: add last-layer Laplace to a certified perception network without touching its weights.
  • Continual and federated learning: the Laplace precision (as in elastic weight consolidation) measures which weights matter.
  • Bayesian optimisation and model selection: the Laplace evidence selects architectures and data augmentation (Immer et al., 2021-2022).

References

  1. D. J. C. MacKay (1992). “A practical Bayesian framework for backpropagation networks.” Neural Computation.
  2. H. Ritter, A. Botev, D. Barber (2018). “A scalable Laplace approximation for neural networks.” ICLR.
  3. A. Kristiadi, M. Hein, P. Hennig (2020). “Being Bayesian, even just a bit, fixes overconfidence in ReLU networks.” ICML.
  4. A. Immer, M. Korzepa, M. Bauer (2021). “Improving predictions of Bayesian neural nets via local linearization.” AISTATS.
  5. E. Daxberger et al. (2021). “Laplace Redux: effortless Bayesian deep learning.” NeurIPS.