AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Uncertainty Quantification in Neural Networks

📚 ML Theory⏱️ 23 min read🎓 Grade 12
✍️ AI Computer Institute Editorial Team Updated: September 2026 CBSE-aligned · Peer-reviewed · 23 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Why a 65% Prediction Can Mean Two Completely Different Things

At a diabetic-retinopathy screening camp run by an eye-hospital network in India, a technician photographs a patient's retina with a handheld fundus camera and hands the image to a deep convolutional classifier. The network outputs P(referable) = 0.65. In 2016, Gulshan and colleagues published exactly this kind of system in JAMA — a deep learning algorithm trained on retinal photographs that, at its chosen operating point, matched board-certified ophthalmologists on sensitivity and specificity, both comfortably above 90% on the validation sets (EyePACS-1 and Messidor-2). That is a genuinely strong result. But strong aggregate accuracy hides a per-image problem: the number 0.65 can come from two very different situations. It can come from a sharp, well-lit retina where the lesion count is genuinely borderline — a case where even two ophthalmologists would disagree, because the underlying evidence really is ambiguous. Or it can come from a hazy, poorly focused photo — bad lighting, patient blinked, camera angle off — where the network has never seen anything quite like this input during training and is, in a meaningful sense, guessing. A single softmax number cannot distinguish these. A single number is a point estimate, and a point estimate by construction throws away everything about how much the model actually knows.

This is the problem uncertainty quantification (UQ) solves: instead of asking a network "what is your prediction?", you ask "what is your prediction, and how much should I trust it?" The second question turns out to require rethinking what a neural network's output actually represents, not just adding a new output head.

Two Kinds of "I Don't Know"

Formally, a standard classifier trained by maximum likelihood gives you a single point estimate of the weights, ŵ, and computes p(y | x, ŵ) — the softmax distribution over classes for one fixed set of weights. A fully Bayesian treatment instead maintains a whole distribution over weights, p(w | D), given the training data D, and the predictive distribution marginalizes over it:

p(y | x, D) = ∫ p(y | x, w) · p(w | D) dw

This integral is where the two flavours of uncertainty live, and conflating them is the single most common conceptual error students make with this topic.

Aleatoric uncertainty is the spread already present in p(y | x, w) for a fixed, correct w — it is the irreducible noise in the data-generating process itself. A borderline retina with a genuinely ambiguous microaneurysm count is aleatoric: no amount of extra training data or a bigger model removes the ambiguity, because the ambiguity is a property of that specific image, not of the model's ignorance.

Epistemic uncertainty is the spread that comes from not knowing w — from the fact that p(w | D) is not a spike, because D is finite. A camera model or lighting condition absent from the training set produces epistemic uncertainty: a different, better-trained model (or the same model shown more data of that kind) would resolve it. Epistemic uncertainty is reducible; aleatoric uncertainty is not.

These two decompose cleanly through mutual information. Houlsby, Huszár, Ghahramani, and Lengyel formalized this in 2011 (as the "BALD" criterion for active learning), and Gal, Islam, and Ghahramani applied it to deep networks in their 2017 ICML paper "Deep Bayesian Active Learning with Image Data":

H[y | x, D]  =  𝔼p(w|D)[H[y | x, w]]  +  I(y, w | x, D)

Total predictive entropy (left side) splits into the expected entropy of individual predictions across the weight posterior (aleatoric — how uncertain the model is even when you fix a plausible w) plus the mutual information between the prediction and the weights (epistemic — how much the different plausible w's disagree with each other). If every sampled w gives roughly the same distribution over y, mutual information is near zero and almost all the uncertainty is aleatoric. If the sampled w's disagree wildly with each other even though each one is individually confident, that disagreement is epistemic — and it is exactly what a single deterministic forward pass cannot see, because a single forward pass only ever samples one w.

The Softmax Lie

Common misconception: "the model said 97%, so it's 97% confident." This conflates two unrelated things — the softmax output, and a calibrated probability. Softmax values are whatever a network trained by cross-entropy happens to produce; nothing in that training objective forces them to equal the empirical accuracy rate. Guo, Pleiss, Sun, and Weinberger measured this directly in their 2017 ICML paper "On Calibration of Modern Neural Networks": modern architectures are frequently overconfident, and — counterintuitively — calibration error tends to get worse as networks get deeper and wider, even while top-1 accuracy improves. Bigger, more accurate models are not automatically more honest about their own uncertainty; cross-entropy training has every incentive to push correct-class logits toward extreme values, since doing so keeps lowering the loss long after the argmax prediction has stopped changing.

The failure mode is even starker for inputs unlike anything in training. Nguyen, Yosinski, and Clune showed in their 2015 CVPR paper "Deep Neural Networks are Easily Fooled" that you can construct images which look like pure static or abstract noise to a human, yet a trained classifier assigns them upward of 99% confidence to a specific class. A single deterministic forward pass has no built-in mechanism to say "I have never seen anything remotely like this" — it only ever computes p(y | x, ŵ) for one fixed ŵ, so however extreme or unfamiliar x is, out comes a normalized probability vector that sums to one and looks exactly as legitimate as a confident, correct prediction. Confidence needs to be checked against reality, not read off the softmax layer.

Monte Carlo Dropout: Turning a Regularizer Into a Sampler

Gal and Ghahramani's 2016 ICML paper "Dropout as a Bayesian Approximation: Representing Model Uncertainty in Deep Learning" made a striking observation: dropout, ordinarily switched off at test time, can instead be left active at test time, and doing so is mathematically equivalent to drawing an approximate sample from p(w | D) under a specific variational family. Each stochastic forward pass zeroes out a different random subset of units — a different sub-network — and running the same input through T different sub-networks gives T samples from an approximate posterior predictive distribution, rather than one point estimate.

Concretely: run T forward passes with dropout masks m₁ … m_T active, collect the T softmax outputs p₁ … p_T for the class of interest, and estimate:

μ(x) ≈ (1/T) Σt pt     (predictive mean)
σ²(x) ≈ (1/T) Σt (pt − μ(x))²     (predictive variance — an epistemic-uncertainty proxy)

The variance term is the payoff: two inputs can produce an identical mean prediction while disagreeing enormously on how stable that prediction is across sub-networks, and that disagreement is precisely what a single forward pass throws away. Leibig, Allken, Ayhan, Berens, and Wahl applied this exact technique to diabetic-retinopathy grading in their 2017 Scientific Reports paper "Leveraging uncertainty information from deep neural networks for disease detection": by computing MC-dropout predictive variance on retinal images and routing the highest-variance images to a human grader instead of trusting the point estimate, the achievable accuracy on the automatically-graded remainder improved — rejecting the small fraction of genuinely uncertain cases did more for reliability than simply adding more training data would have.

Worked Example: Same Prediction, Fourteen Times the Doubt

Suppose a trained network is run with MC Dropout (T = 5, dropout rate p = 0.3) on two fundus photographs — one clear, one blurry from a screening camp with poor lighting. The five stochastic softmax outputs for P(referable) are:

Clear photo:  0.66, 0.64, 0.65, 0.67, 0.63
Blurry photo: 0.85, 0.30, 0.71, 0.55, 0.84

Both sets average to exactly the same mean — by construction, to isolate what variance adds. Sum the clear-photo values: 0.66 + 0.64 + 0.65 + 0.67 + 0.63 = 3.25, so μ = 3.25 / 5 = 0.650. Deviations from the mean are +0.01, −0.01, 0.00, +0.02, −0.02; squared, these are 0.0001, 0.0001, 0.0000, 0.0004, 0.0004, summing to 0.0010. Dividing by T = 5 gives variance 0.00020, and σ = √0.00020 ≈ 0.0141.

For the blurry photo: 0.85 + 0.30 + 0.71 + 0.55 + 0.84 = 3.25, so μ = 0.650 — identical to the clear photo. But the deviations are +0.20, −0.35, +0.06, −0.10, +0.19; squared, these are 0.0400, 0.1225, 0.0036, 0.0100, 0.0361, summing to 0.2122. Dividing by 5 gives variance 0.04244, and σ = √0.04244 ≈ 0.2060.

Two images, the identical point prediction of 0.65 — and standard deviations of 0.0141 versus 0.2060, a ratio of roughly 14.6×. Anyone reading only the mean would treat both images identically. The code below reproduces both numbers exactly:

import numpy as np

# Softmax "referable DR" probability from T=5 stochastic forward
# passes (dropout active, p=0.3), for two fundus photographs.
# Obtained via run_mc_dropout_passes(model, image, T=5, p_dropout=0.3)
# (assumed helper, not shown -- draws T Bernoulli dropout masks and
# performs one forward pass under each mask)

passes_clear_photo  = np.array([0.66, 0.64, 0.65, 0.67, 0.63])
passes_blurry_photo = np.array([0.85, 0.30, 0.71, 0.55, 0.84])

def mc_dropout_summary(passes):
    mean = passes.mean()
    variance = passes.var()   # population variance, ddof=0, matches 1/T
    std = passes.std()
    return mean, variance, std

for name, passes in [("clear photo", passes_clear_photo),
                      ("blurry photo", passes_blurry_photo)]:
    mean, var, std = mc_dropout_summary(passes)
    print(f"{name}: mean={mean:.3f}  variance={var:.5f}  std={std:.4f}")

# Output:
# clear photo: mean=0.650  variance=0.00020  std=0.0141
# blurry photo: mean=0.650  variance=0.04244  std=0.2060

With a deployment rule such as "flag for human review if σ > 0.10," the clear photo (σ = 0.0141) auto-clears, while the blurry photo (σ = 0.2060) gets flagged — even though a naive rule based only on how close the mean is to the 0.5 decision boundary (both means sit at 0.65, equally far from 0.5) would have treated the two images identically and caught neither as special.

Deep Ensembles: Uncertainty by Committee

Lakshminarayanan, Pritzel, and Blundell proposed an alternative in their 2017 NeurIPS paper "Simple and Scalable Predictive Uncertainty Estimation Using Deep Ensembles": instead of sampling many sub-networks from one trained model, train M full networks independently — different random weight initializations and different data-shuffling order, each optimized normally with a proper scoring rule (cross-entropy for classification). At inference, average their outputs for the point prediction and use their disagreement as the uncertainty signal, exactly as with MC Dropout's T passes, but now each "sample" is an entirely separately-trained network rather than a masked sub-network of one.

The production tradeoff is a genuine systems decision, not a detail. MC Dropout trains one network (1× training compute, 1× resident GPU memory) but needs T forward passes at inference (T× inference compute) to get a stable variance estimate. Deep Ensembles train M networks (M× training compute and, if serving them in parallel, M× memory footprint) but typically need fewer members than MC Dropout needs passes — M = 5 is a common default versus T = 20–100 often needed to stabilize an MC Dropout estimate — and it comes with a robustness advantage: Ovadia, Fertig, Ren, Nado, Sculley, Nowozin, Dillon, Lakshminarayanan, and Snoek benchmarked both methods under real distribution shift in their 2019 NeurIPS paper "Can You Trust Your Model's Uncertainty? Evaluating Predictive Uncertainty Under Dataset Shift," and found deep ensembles held up better than MC Dropout as inputs drifted away from the training distribution — precisely the screening-camp scenario of an unfamiliar camera or lighting condition where epistemic uncertainty matters most. The cost is that you must budget for training and, if you want low-latency serving, hosting several full models rather than one.

Calibration: Making the Numbers Mean What They Say

UQ methods like MC Dropout and ensembles surface relative uncertainty — which inputs are riskier than others. A separate, complementary question is whether the raw numbers are calibrated in absolute terms: does "70% confident" actually correspond to being correct 70% of the time? Bin predictions by confidence into M bins and compute the Expected Calibration Error:

ECE = Σm=1M (|Bm| / n) · |acc(Bm) − conf(Bm)|

where Bm is the set of predictions falling in confidence bin m, acc(Bm) is their actual accuracy, and conf(Bm) is their average reported confidence. A perfectly calibrated model has ECE = 0: in every bin, stated confidence matches empirical accuracy, and a reliability diagram plotting acc(Bm) against conf(Bm) sits exactly on the diagonal.

Guo et al. (2017), in the same paper cited above, showed that a remarkably simple fix — temperature scaling — closes most of the gap. Divide the pre-softmax logits z by a single learned scalar T > 1 before applying softmax: softmax(z / T). T is fit by minimizing negative log-likelihood on a held-out validation set, with the network weights frozen. Because T only rescales the logits, it never changes their ranking, so accuracy (which depends only on the argmax) is completely unaffected — only the sharpness of the confidence values changes. Despite its simplicity, Guo et al. found temperature scaling matched or beat more elaborate calibration methods (Platt scaling, isotonic regression, histogram binning) on modern deep architectures. It is worth stressing that calibration and epistemic-uncertainty detection are different tools solving different problems: a temperature-scaled model can be well calibrated on the training distribution and still be silently overconfident on inputs from a shifted distribution it has never seen, which is exactly the gap MC Dropout and deep ensembles are built to expose.

The Mechanism, Diagrammed

MC Dropout: one network, five stochastic sub-networks Blurry fundus photo — each pass drops a different random set of hidden units x (blurry photo) Pass 1 (mask m₁) × × p₁ = 0.85 Pass 2 (mask m₂) × × p₂ = 0.30 Pass 3 (mask m₃) × × × p₃ = 0.71 5 stochastic passes (T=5): p₁=0.85 p₂=0.30 p₃=0.71 p₄=0.55 p₅=0.84 (passes 4-5 not drawn) μ = mean(p₁..p₅) = 0.650 σ = std(p₁..p₅) = 0.206 compare: clear photo σ = 0.014 → 14.6× higher spread, same μ σ > 0.10 ? (referral threshold) yes: 0.206 > 0.10 FLAG for human review refer to ophthalmologist For comparison — clear photo: σ=0.014 ≤ 0.10 → trust automatically, no flag active unit (this pass) dropped unit (masked out) active connection connection removed by mask

Active Recall

Attempt every question before reading the answer beneath it.

Q1. Define aleatoric and epistemic uncertainty, each with a concrete example from retinal screening.

Q2. Why must dropout stay active at test time for MC Dropout to work, when ordinary inference always disables it?

Q3. In the worked example, suppose T is increased from 5 to 50 for the blurry photo, with p_dropout held at 0.3. What happens to the reported standard deviation, and why — does the underlying uncertainty shrink?

Q4. A colleague says: "our softmax outputs already sum to one, so they're already probabilities — we don't need temperature scaling." What is wrong with this claim?

Q5 (ripple effect). Starting from the worked example, suppose p_dropout is now raised from 0.3 to 0.6 for both photographs. Trace the effect on: (a) both standard deviations, (b) both means, (c) the ratio between the two standard deviations, and (d) the fixed referral rule "flag if σ > 0.10."

Q6. Compare the training-time and inference-time compute cost of MC Dropout (T = 20 passes on one trained network) against Deep Ensembles (M = 5 independently trained networks).

A1. Aleatoric uncertainty is the irreducible noise in the data itself, present even for a perfectly-trained model — e.g. a sharp, well-focused retinal image with a genuinely borderline microaneurysm count, where two expert ophthalmologists would disagree because the evidence itself is ambiguous. Epistemic uncertainty is the model's ignorance from limited or unrepresentative training data — e.g. a photo taken with a camera model or lighting setup absent from the training set, which more or better-chosen training data could fix.

A2. Gal and Ghahramani (2016) show that a stochastic forward pass with dropout active is mathematically equivalent to sampling one plausible weight configuration from an approximate posterior p(w | D). A single such pass therefore gives one sample, not a distribution. Only by running many passes — each with an independently sampled mask, hence a different sub-network — do you obtain multiple samples of the predictive distribution, from which a mean and variance can be estimated. Disabling dropout at test time (as ordinary inference does) instead computes the expected output of all sub-networks at once, collapsing back to a single point estimate with no variance information at all.

A3. Increasing T from 5 to 50 does not change p_dropout, so it does not change the approximate posterior being sampled from — it only draws more samples from the same distribution. This reduces the estimation error of the reported σ (the standard error of a sample-variance estimate shrinks roughly as 1/√T, so the true underlying variance of that posterior is pinned down more precisely), but it does not shrink the underlying epistemic uncertainty itself, which is fixed by the trained weights and p_dropout. More passes give a more trustworthy number, not a smaller one.

A4. Summing to one only makes the softmax output a valid probability distribution over classes in the mathematical sense — it says nothing about whether the reported confidence matches empirical accuracy. Guo et al. (2017) showed modern deep networks are often systematically overconfident, with calibration error tending to worsen as networks get deeper and more accurate, precisely because cross-entropy training keeps pushing correct-class logits toward extreme values well after accuracy has plateaued. Nguyen et al. (2015) pushed this further, showing unrecognizable noise images can receive over 99% softmax confidence. "Sums to one" and "is calibrated" are unrelated properties; only the second one requires checking accuracy against confidence bins (ECE / a reliability diagram) and, typically, correcting with something like temperature scaling.

A5. (a) A higher dropout rate zeros out more units per pass, so each stochastic sub-network diverges further from the full trained network; the spread across passes grows for both images, so both σ values increase. (b) The reported means can shift too, not just stay fixed at 0.650: averaging the outputs of more heavily perturbed sub-networks is not guaranteed to reproduce the same expected value as before, and in practice higher dropout tends to pull ensemble-mean predictions toward less extreme, more "regularized" values. (c) The two σ's will not necessarily scale by the same factor: the blurry-photo case already relies on fragile, non-redundant evidence that heavier masking disrupts disproportionately, while the clear photo's signal is typically spread redundantly across many units and more robust to losing any given subset — so the gap between the two σ's, roughly 14.6× at p=0.3, is likely to widen rather than narrow, though the exact new ratio is an empirical question that would need to be measured, not derived from p_dropout alone. (d) The threshold of 0.10 was implicitly calibrated against p_dropout = 0.3. Because raising p_dropout inflates variance for essentially every input — including previously-confident, genuinely clear images — reusing the same 0.10 cutoff risks pushing the clear photo's σ above the threshold too, causing false-positive referrals on easy cases. The threshold is a property of the specific stochastic-forward-pass configuration used to derive it, and must be re-validated whenever p_dropout changes, not treated as fixed to the screening problem itself.

A6. MC Dropout trains a single network once (1× training compute, 1× resident parameter memory), but needs T = 20 forward passes per input at inference (20× inference compute on that one model). Deep Ensembles train M = 5 independent networks (5× training compute, parallelizable across GPUs since the networks don't share weights), and need only M = 5 forward passes at inference (5× inference compute) — less inference compute than the MC Dropout example here — but at the cost of 5× the parameter memory if all five are to be served simultaneously for low latency, versus MC Dropout's constant 1× memory footprint throughout. The net tradeoff: ensembles spend more upfront on training and serving memory in exchange for uncertainty estimates that Ovadia et al. (2019) found hold up better under real distribution shift, and often need fewer forward samples than MC Dropout to reach a stable estimate.

Think About It

Think about this: How would you explain uncertainty quantification in neural networks to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

← Analog AI Accelerators: Computing with PhysicsAdversarial Robustness: Defending Against Attacks →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn