Foundations to Frontier
Tests

Deep Ensembles

A deep ensemble trains the same network several times from different random initialisations and averages the predictions. The members' disagreement measures epistemic uncertainty. Lakshminarayanan, Pritzel and Blundell (2017) showed that this simple recipe, with members that predict a mean and a variance, matched or beat Bayesian approximations on calibration and out-of-distribution data. Large benchmarks (Ovadia et al., 2019) confirmed that ensembles degrade most gracefully under dataset shift. Their strength is that independent runs land in different modes of the loss landscape, which local approximations around one mode cannot represent. The cost is M times the training and memory. This page covers the recipe and its mixture predictive, why random initialisation suffices, the Bayesian interpretation, the ambiguity decomposition, randomised prior functions, and cheaper variants. In the exercises you compute mixture moments and NLLs. You then train ensembles of 30-unit networks on data with a gap. In distribution, ten members changed the test NLL by almost nothing (−0.75 both ways). When extrapolating to x in 1.2–2, they cut the NLL from 17.1 for a single network to 3.3 but stayed overconfident (spread 0.16 against an RMSE of 0.60). Bagging did not help (6.3), while randomised prior functions did (1.9).

Level 0: Foundation

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

Ask several networks

Train the same network five times, each time starting from different random weights. On inputs like the training data they all give nearly the same answer. On unusual inputs they each guess differently, and the size of their disagreement tells you how much to trust the average. It is the simplest uncertainty method and, in many benchmarks, the best. The demo shows several networks fitted to the same data: they agree on the data and fan out away from it.

Bayesian deep learning lab
5 networks with different random hidden layers, each fitted to the same data, and the mean ± 2 std band of their predictions. They agree on the data and disagree away from it; one network alone gives no spread at all.
Why engineers care

Ensembles are the standard uncertainty baseline in autonomous driving, weather and climate emulators, and model-based reinforcement learning (PETS uses an ensemble of probabilistic dynamics models).

📝 Quick check (Level 0)
0/3 answered
  1. 1. Deep-ensemble members differ mainly by…
  2. 2. Their uncertainty is their…
  3. 3. The main cost is…

Level 1: Beginner

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

By hand

  • Recipe (Lakshminarayanan et al., 2017): M networks (typically 5), random initialisation and data order, trained with a proper scoring rule. For regression each outputs μ_m(x) and σ_m²(x) and is trained on Gaussian NLL; the ensemble predictive is the mixture, moment-matched to mean (1/M) Σ μ_m and variance (1/M) Σ (σ_m² + μ_m²) − μ̄².
  • Bagging is not needed: training on bootstrap samples (63% unique points each) usually hurts, because each member sees less data; random initialisation already gives diversity.
  • Why it works: independent runs reach different modes, which represent functionally different solutions (Fort, Hu and Lakshminarayanan, 2019). Methods that stay near one mode (Laplace, mean-field VI, SWAG) miss this.
  • Bayesian view: an ensemble is a crude sample of posterior modes; Wilson and Izmailov (2020) argue it approximates Bayesian model averaging better than single-mode methods. Randomised prior functions (Osband, Aslanides and Cassirer, 2018) add a fixed random network to each member to restore prior variance where there is no data.
  • Cheaper variants: snapshot ensembles (one run, cyclic learning rate), BatchEnsemble (rank-1 member weights, Wen, Tran and Ba, 2020), MIMO, packed ensembles, last-layer or LoRA ensembles for large models, and distillation into one network.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The moment-matched ensemble variance is…
  2. 2. Bagging usually…
  3. 3. Ensembles capture uncertainty across…

Level 2: Intermediate

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

Building an ensemble

Exercise 1.

  • Mixture moments of members’ Gaussian predictions.
  • Gaussian negative log-likelihood.
  • Member disagreement.

Practice.

  • Train M = 5 copies with different seeds; keep everything else equal. Gains flatten after 5–10 members.
  • Use a proper scoring rule (NLL, or Brier score for classification) and a variance head if aleatoric noise varies.
  • Average probabilities, not logits, for classification; report NLL, ECE and AUROC for out-of-distribution detection.
  • If the budget allows only one model, try a last-layer, LoRA or BatchEnsemble ensemble, or distil the ensemble.
  • For decisions under shift, compare with randomised prior functions or hyperparameter ensembles for extra diversity.
import torch
models = [make_net().to(dev) for _ in range(5)]
for seed, m in enumerate(models):
    torch.manual_seed(seed); train(m, train_loader)        # different init and data order
with torch.no_grad():
    mu, var = zip(*[m(x_test) for m in models])            # each returns mean and variance
mu, var = torch.stack(mu), torch.stack(var)
pred_mean = mu.mean(0)
pred_var = (var + mu ** 2).mean(0) - pred_mean ** 2        # aleatoric + epistemic
🐍 Exercise 1: mixture predictive and NLL
Compute the moment-matched mixture of member Gaussians, the Gaussian NLL and the members' disagreement.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A typical ensemble size:
  2. 2. For classification, average…
  3. 3. A cheaper variant with rank-1 member factors:

Level 3: Advanced

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

Theory

Ambiguity decomposition (Krogh and Vedelsby, 1995). For the ensemble mean f̄ = (1/M) Σ f_m and target y, (f̄ − y)² = (1/M) Σ (f_m − y)² − (1/M) Σ (f_m − f̄)². The ensemble’s squared error equals the average member’s error minus the average spread, so averaging never hurts squared error, and helps in proportion to diversity. For log-likelihood, Jensen’s inequality gives the analogous result: the mixture’s log-likelihood is at least the average member’s.

Why NLL rewards spread under shift. A member with error e and std σ pays e²/(2σ²). A single network has only the noise σ, so a large error far from the data is catastrophic. The ensemble adds the member spread to σ², which bounds the penalty when the members disagree as much as they err. It does not help if all members make the same mistake: then the spread is small and the error large.

Randomised prior functions. Member m predicts f_m(x) + β p_m(x) with p_m a fixed random network and only f_m trained, on y − β p_m(x). Where data constrain the sum, the members agree; elsewhere each keeps its own prior function, so the spread approaches the prior’s. For linear models this gives exact posterior samples (Osband et al., 2018).

📝 Quick check (Level 3)
0/3 answered
  1. 1. The ambiguity decomposition says the ensemble's squared error is…
  2. 2. Ensembles do not help when members…
  3. 3. Randomised prior functions train each member on…

Level 4: Expert

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

How much do ensembles help in and out of distribution, and do bagging or random priors add anything?

History.

  • A. Krogh and J. Vedelsby, “Neural network ensembles, cross validation, and active learning” (1995).
  • B. Lakshminarayanan, A. Pritzel, C. Blundell, “Simple and scalable predictive uncertainty estimation using deep ensembles” (2017).
  • I. Osband, J. Aslanides, A. Cassirer, “Randomized prior functions for deep reinforcement learning” (2018).
  • S. Fort, H. Hu, B. Lakshminarayanan, “Deep ensembles: a loss landscape perspective” (2019); Y. Ovadia et al. (2019).

Exercise 2. 1-30-1 tanh networks, MSE + weight decay 10⁻³, 1500 full-batch heavy-ball steps (learning rate 0.05, momentum 0.9), all members trained at once (vectorised). Data: 40 points of sin(3x) + N(0, 0.1²) with a gap; noise std 0.1 known. Test sets: 400 points from the training distribution and 400 with x in 1.2–2 (extrapolation). Single network = average NLL over the 10 members.

model NLL in distribution NLL extrapolating
one network −0.75 17.06
ensemble, 2 −0.77 4.70
ensemble, 5 −0.75 3.77
ensemble, 10 −0.75 3.28
bagged, 10 −0.75 6.26
random priors, 10 −0.76 1.93
  • In distribution ensembling changed nothing: members agree (spread below 0.005 on the data).
  • Extrapolating, the spread cut the NLL fivefold, but the ensemble remained overconfident: spread 0.16 far away against RMSE 0.60.
  • Bagging was worse than plain ensembling on seeds 0 and 2 (6.26 vs 3.28; 7.69 vs 4.41) and better on seed 1 (1.61 vs 2.35), consistent with Lakshminarayanan et al.’s advice to skip it.
  • Randomised priors gave the best extrapolation NLL on all three seeds (1.93, 0.74, 4.27).
  • The gap (spread 0.02) is not flagged: sin(3x) is nearly linear there, and all members interpolate it the same way.
  • Seeds 1 and 2: one network 11.5 and 23.2; ten members 2.35 and 4.41.
  • Caveat: small networks on 1-D data; on images the members’ diversity is larger.
🐍 Exercise 2 (research): how much do ensembles help in and out of distribution, and do bagging or random priors add anything?
Train ensembles of networks (plain, bagged and with randomised priors) and compare their NLL in distribution and under extrapolation.

Ensembles in AI, robotics and aerospace

  • Model-based RL: PETS (Chua et al., 2018) and MBPO plan with ensembles of probabilistic dynamics models; disagreement drives exploration (Pathak et al., 2019).
  • Weather and climate: ensembles of ML weather models (and of initial conditions) give probabilistic forecasts.
  • Safety monitors: ensemble disagreement flags out-of-distribution camera inputs for autonomous vehicles and drones.

References

  1. B. Lakshminarayanan, A. Pritzel, C. Blundell (2017). “Simple and scalable predictive uncertainty estimation using deep ensembles.” NeurIPS.
  2. S. Fort, H. Hu, B. Lakshminarayanan (2019). “Deep ensembles: a loss landscape perspective.” arXiv.
  3. I. Osband, J. Aslanides, A. Cassirer (2018). “Randomized prior functions for deep reinforcement learning.” NeurIPS.
  4. A. G. Wilson, P. Izmailov (2020). “Bayesian deep learning and a probabilistic perspective of generalization.” NeurIPS.
  5. Y. Wen, D. Tran, J. Ba (2020). “BatchEnsemble: an alternative approach to efficient ensemble and lifelong learning.” ICLR.