Theory & Frontiers/Bayesian & Ensemble Methods

Lesson 7.42,409 words

Bayesian & Ensemble Methods

A trained network returns a single point prediction and, with the softmax, a confidence, but that confidence is usually miscalibrated, collapsing to near- certainty even on inputs the model has never seen. This lesson covers uncertainty estimation for networks: the two kinds of uncertainty, the Bayesian posterior over weights and its tractable stand-ins (MC dropout, deep ensembles), and how to check whether a model's reported confidences match observed frequencies.

╌╌╌╌

A standard classifier ends in a softmax, so it always returns a probability vector, and it is tempting to read the largest entry as the model's confidence. That reading is wrong in a specific, dangerous way. A network trained to minimize cross-entropy is pushed toward saturated outputs on the training set, and nothing in the objective constrains what it reports off that set. Feed it an input from a class it has never seen and it will still report confidence; the softmax has no way to indicate that the input lies outside everything the model was trained on.

The fix is to treat the network's output as a distribution rather than a single number. This lesson develops three routes to that distribution (the fully Bayesian posterior over weights, its cheap approximation by dropout left on at test time, and the brute-force ensemble) and closes with the question that decides whether any of it worked: is the reported confidence calibrated?

Two kinds of uncertainty

Not all uncertainty has the same cause, and the distinction governs what you can do about it. Predicting a coin flip, you are uncertain no matter how much data you collect; the noise is in the world. Predicting in a region where you have no data, you are uncertain only because you have not looked; more data would resolve it. These are the two irreducible categories.

TypeSourceReducible with more data?How to estimate
Aleatoricnoise in the data / task itselfno, irreduciblepredict a variance head ; heteroscedastic loss
Epistemicignorance about the model / weightsyes, vanishes as posterior over weights; ensemble spread; MC dropout

The total predictive variance decomposes additively into the two, and the decomposition is the whole point: it tells you whether to collect more data (epistemic) or accept the floor (aleatoric):

where the expectation and variance are taken over the posterior on . The first term is the average noise each model expects; the second is the disagreement between models. The signature of epistemic uncertainty is a predictive band that hugs the data where it is dense and fans open where it is absent.

Aleatoric vs. epistemic uncertainty: the predictive band is narrow near the data and fans out away from it, where no observations pin the function down.

To estimate aleatoric uncertainty directly, give the network a second output head that predicts the noise variance and train with the negative log-likelihood of a Gaussian, which trades fit against predicted variance:

The term is a penalty that stops the model from declaring infinite noise everywhere to zero out the first term; together they let the network learn where the data is noisy. Epistemic uncertainty needs a different mechanism: a distribution over the weights themselves.

The Bayesian view

Maximum likelihood collapses everything into one . The Bayesian treatment instead carries the whole distribution of weights consistent with the data. Bayes' rule turns a prior over weights into a posterior once the data is observed.1

A point estimate is the single tallest peak of this posterior; the Bayesian keeps the entire distribution, and its width is the epistemic uncertainty.

A point estimate keeps only the mode (left); the Bayesian posterior keeps the whole distribution over weights (right), whose spread is the epistemic uncertainty.

The predictive distribution

The point of a posterior is not the weights themselves but the predictions they imply. To predict at a new , you do not pick one — you average the prediction over all of them, each weighted by its posterior probability. This is the posterior predictive distribution:

This integral is the entire Bayesian deep-learning programme, and it is intractable.2 The posterior over millions of weights has no closed form, and the integral runs over that whole space. Every practical method is a way to approximate this expectation. Three families dominate, trading fidelity for cost.

The third route is the one that costs almost nothing to implement, because the machinery is already in the network.

MC dropout as approximate inference

Dropout randomly zeroes units during training and is normally switched off at test time. Monte Carlo dropout instead leaves it on at test time: each forward pass then samples a different sub-network, and the spread of their outputs approximates the posterior predictive. Gal and Ghahramani showed this procedure coincides with variational inference under a particular Bernoulli : dropout was a Bayesian approximation all along.34

The predictive mean is the Monte-Carlo average over the masked passes, a direct estimate of the intractable integral by sampling:

The predictive variance is the sample variance of those same passes — and for a heteroscedastic network it splits cleanly into the two uncertainty types, the average per-pass noise (aleatoric) plus the spread of the means (epistemic):

MC dropout: stochastic forward passes of one input, each dropping a different subset of units, yield a spread of outputs whose mean and variance give prediction and uncertainty.

The mechanism is visible on a regression: each stochastic pass draws a slightly different curve, the curves agree where the data constrains them and scatter where it does not, and the band between them is the epistemic estimate.

MC-dropout forward passes on a 1-D regression: the sampled curves pile together near the data and scatter where it is absent, their spread being the predictive variance.

MC dropout is nearly free (no extra parameters, no retraining, just forward passes), which is why it is the default first choice for uncertainty. Its weakness is that the dropout rate fixes the approximate posterior in advance, so the bands can be poorly calibrated. The method that consistently does better is also the simplest.

Deep ensembles

Train networks independently (same architecture, different random initializations and a different data-shuffling order) and average their predictions.5 Because the loss surface is riddled with many distinct minima, the networks settle into different functions that nonetheless all fit the training data, and their disagreement off the data is a good estimate of epistemic uncertainty.

The ensemble is itself a crude posterior: each independently trained network is one sample from the (multi-modal) distribution of good solutions, so averaging the members is a Monte-Carlo estimate of the same predictive integral, but with samples drawn from genuinely different modes rather than around a single one.

This is why ensembles often beat single Bayesian nets in practice: variational methods and the Laplace approximation hug one mode of the posterior, while the ensemble's members land in different basins and so capture the genuine multi-modal disagreement that epistemic uncertainty is made of. The cost is linear ( full training runs and forward passes), but the members are independent, so they train and serve in parallel.

Ensemble disagreement: members from different initializations agree where the data constrains them and diverge off-data, their spread being the epistemic uncertainty.

The four methods occupy a clean cost-quality frontier: pick by how much compute and implementation effort the uncertainty is worth.

MethodExtra costPosterior capturedQuality
Deep ensemble training, inferencemulti-modal (different basins)best, but expensive
MC dropoutnone to train, inferencesingle mode, fixed by dropout ratecheap, often under-confident
Variational inference (Bayes-by-backprop) parameters, harder to trainone mode, learned mean+variancemoderate; sensitive to priors
Laplace approximationone Hessian/curvature passone mode, local Gaussiancheap post-hoc; only local

A single network's confidence may still be useful if it matches reality, and that match has a precise, measurable definition.

Calibration

A model is calibrated if its confidence matches its accuracy: of all the predictions it makes with confidence, should be correct. Modern deep networks are systematically over-confident: high accuracy, but confidence that runs well ahead of it.6 Calibration is checked, not assumed.

The diagnostic is a reliability diagram: bin predictions by confidence, and for each bin plot mean confidence against measured accuracy. Perfect calibration is the diagonal; an over-confident model bows below it (confidence exceeds accuracy).

A reliability diagram: the diagonal is perfect calibration, and a curve bowing below it marks an over-confident model whose accuracy lags its reported confidence.

The gap is summarized by one number. Partition the predictions into confidence bins ; the Expected Calibration Error is the average bin-level gap between accuracy and confidence, weighted by bin population:

where is the fraction correct in bin and is the mean confidence there. ECE is perfect calibration; a typical over-confident network sits at several percent.

For example, take predictions sorted into three confidence bins. Bin holds predictions at mean confidence of which are correct (accuracy ); bin holds at mean confidence with correct (accuracy ); bin holds at mean confidence with correct (accuracy ). Each bin's gap is confidence minus accuracy, weighted by how many predictions it holds:

binconfaccgap weight contribution

Summing the contributions, : the model is over-confident by percentage points on average, and the populous high-confidence bin — where the gap is widest and the weight is largest — supplies most of it. This is the usual signature of a modern network: it is most miscalibrated exactly where it is most sure, so the largest bin dominates the error.

Temperature scaling

The cheapest fix is post-hoc: divide the pre-softmax logits by a single scalar before the softmax, and tune on a held-out set to minimize negative log-likelihood. The argmax (and hence the accuracy) is unchanged, since dividing all logits by the same never reorders them; only the sharpness of the distribution moves.

Temperature on the same logits. (left) is spiky and over-confident; (right) softens the distribution toward the true accuracy while the tallest bar, and thus the prediction, is unchanged.
Algorithm:Calibrate(z1:n,y1:n)\textsc{Calibrate}(z_{1:n}, y_{1:n}) — fit temperature on a held-out set
  1. 1
    T1T \gets 1
  2. 2
    repeat
  3. 3
    pisoftmax(zi/T)p_i \gets \softmax(z_i / T) for each validation example ii
  4. 4
    L1nilogpi[yi]L \gets -\frac{1}{n}\sum_i \log p_i[y_i]
    validation negative log-likelihood
  5. 5
    TTηLTT \gets T - \eta\,\dfrac{\partial L}{\partial T}
    one scalar to tune
  6. 6
    until LL converged
  7. 7
    return TT

The modern uncertainty toolbox

Goodfellow's §5.6 covers the Bayesian machinery but names none of the practical methods here, all of which postdate the 2016 text; the public papers are the citation of record. Three results built the modern default stack.

MC dropout (Gal & Ghahramani, 2016) supplied the theoretical bridge — the proof that leaving dropout on at test time is variational inference under a Bernoulli posterior — which is what turned a training regularizer into an uncertainty estimator. Deep ensembles (Lakshminarayanan et al., 2017) then showed that simply training a handful of networks from different seeds and averaging them produces better-calibrated uncertainty than most single-model approximate posteriors, because the members land in genuinely different loss basins. Temperature scaling (Guo et al., 2017) closed the loop on calibration: the paper that documented modern networks are systematically over-confident and that a single scalar, fit post-hoc, fixes most of it without touching accuracy.

Later work has pushed the cost-quality frontier from both ends. SWAG (Maddox et al., 2019) fits a Gaussian posterior cheaply by tracking the running mean and covariance of the SGD iterates themselves — a Bayesian read-off of the training trajectory rather than a separate inference run. The Laplace approximation has been revived as a fast, scalable post-hoc method (Daxberger et al., 2021) that fits the curvature around an already-trained model. And evidential deep learning (Sensoy et al., 2018) trains a network to output the parameters of a distribution over the softmax probabilities, so a single forward pass reports both the prediction and its epistemic uncertainty without any sampling at all. As in the cost-quality table above, every method trades how much of the posterior it captures against how much compute it spends, and deep ensembles remain the quality baseline the cheaper methods are measured against.

Takeaways

  • A softmax confidence is not an uncertainty estimate — networks stay near-certain even out-of-distribution, which is the root of their opacity.
  • Uncertainty splits into aleatoric (data noise, irreducible) and epistemic (model ignorance, vanishing with data); the total variance is their sum, and the split tells you whether more data would help.
  • The Bayesian object is the posterior over weights and the predictive integral , which is intractable and must be approximated.
  • MC dropout leaves dropout on at test time; stochastic passes give a predictive mean and a variance that decomposes into aleatoric and epistemic parts — nearly free.
  • Deep ensembles train nets from different inits; their disagreement estimates epistemic uncertainty and, by sampling distinct loss basins, they often outperform single Bayesian approximations.
  • Calibration requires that confidence match accuracy; read it off a reliability diagram, summarize it with ECE, and fix over-confidence cheaply with temperature scaling, which rescales the logits without touching accuracy.

Footnotes

  1. Goodfellow, Deep Learning, §5.6 — Bayesian Statistics: carrying the full posterior rather than collapsing to a point estimate.
  2. Goodfellow, Deep Learning, §5.5 — Maximum Likelihood Estimation: the point estimate the Bayesian predictive integral generalizes, and why that integral is intractable for deep nets.
  3. Goodfellow, Deep Learning, §7.12 — Dropout: random unit-masking as an implicit ensemble, the mechanism MC dropout repurposes at test time.
  4. MC dropout as variational inference (Gal & Ghahramani, 2016), deep ensembles for predictive uncertainty (Lakshminarayanan et al., 2017), and temperature-scaling calibration (Guo et al., 2017) all postdate Goodfellow (2016), which covers the underlying dropout (§7.12) and Bayesian (§5.6) machinery but not these uncertainty methods by name.
  5. Goodfellow, Deep Learning, §7.11 — Bagging and Other Ensemble Methods: averaging independently trained models to reduce variance — the deep-ensemble template.
  6. Chollet, Deep Learning with Python, Ch. 4 — softmax outputs are not calibrated probabilities; high-confidence errors are routine and must be measured, not assumed away.

╌╌ END ╌╌