Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Uncertainty Quantification in Neural Networks

Uncertainty quantification addresses a critical limitation of standard neural networks: they output point estimates (single predictions) without expressing confidence. In high-stakes domains (medical diagnosis, autonomous vehicles), knowing when a model is uncertain is as important as the prediction itself.

Two types of uncertainty matter:

  • Aleatoric uncertainty: Inherent randomness in data (e.g., measurement noise).
  • Epistemic uncertainty: Model’s lack of knowledge (e.g., out-of-distribution inputs).

Why Uncertainty Matters

A neural network trained on natural images might classify a corrupted or adversarial image with 99% confidence — misleading. Proper uncertainty estimates trigger fallback mechanisms: ask a human, collect more data, or abstain from deciding.

Uncertainty is essential for:

  • Safety-critical systems: Autonomous vehicles should know when uncertain.
  • Active learning: Query examples where the model is most uncertain.
  • Bayesian optimization: Balance exploration (uncertain) and exploitation (confident).
  • Calibration: Build trust by making predictions reliably calibrated to true likelihood.

Bayesian Neural Networks

Bayesian neural networks treat weights as distributions, not point estimates:

$$w \sim P(w)$$

Instead of learning weights $w^*$, learn a posterior distribution $P(w | D)$ over weights.

Variational Inference

Approximate the intractable posterior $P(w | D)$ with a tractable variational distribution $q(w)$:

$$\min_q \text{KL}(q(w) || P(w | D)) = \min_q \left( -\mathbb{E}_{w \sim q} [\log P(D | w)] + \text{KL}(q(w) || P(w)) \right)$$

Practical approach: Parameterize $q(w) = \mathcal{N}(\mu, \sigma^2)$ (diagonal Gaussian). Learn $\mu$ and $\sigma$ (means and variances of weight distributions).

Prediction: Integrate over the posterior:

$$P(y | x, D) = \int P(y | x, w) P(w | D) dw$$

Intractable; approximate via sampling or closed-form for specific models.

Predictive Uncertainty

For a new input $x$:

$$P(y | x, D) \approx \int P(y | x, w) q(w) dw$$

Sample weights $w_1, …, w_S$ from $q(w)$; average predictions:

$$\mathbb{E}[y | x] = \frac{1}{S} \sum_s P(y | x, w_s)$$

Variance across samples estimates aleatoric + epistemic uncertainty:

$$\text{Var}[y | x] = \mathbb{E}[y^2 | x] - \mathbb{E}[y | x]^2$$

Monte Carlo Dropout

Key insight: Dropout at test time approximates Bayesian inference.

During training, dropout randomly zeros activations:

$$h_t^{(l)} = z_t^{(l)} \odot h^{(l)} \quad z_t^{(l)} \sim \text{Bernoulli}(p)$$

Standard practice: Disable dropout at test time (use all units).

Bayesian interpretation: Keep dropout enabled at test time. Each forward pass samples a different subnetwork. Predictions vary; variance estimates uncertainty.

$$P(y | x, D) \approx \frac{1}{T} \sum_t f_{\text{dropout}}(x)$$

where $T$ is the number of forward passes with dropout enabled.

Advantages:

  • Easy to implement (no changes to architecture).
  • Works with existing trained models.
  • Computationally cheaper than sampling full weight distributions.

Limitation: Approximate; assumptions don’t strictly hold.

Ensemble Methods

Combine multiple models to estimate uncertainty:

$$P(y | x) = \frac{1}{M} \sum_m P(y | x, \theta_m)$$

Variance across ensemble members estimates model uncertainty.

Advantages:

  • Simple; independent models can be trained in parallel.
  • Diverse architectures/initializations reduce correlation.

Limitation: Requires training multiple models (computational cost).

Deep Ensembles

Train $M$ models with different random initializations and data orderings. Remarkable finding: ensembles often outperform Bayesian approaches for uncertainty estimation despite theoretical simplicity.

Confidence Calibration

Calibration: Model’s predicted confidence should match empirical accuracy.

A perfectly calibrated model predicts 90% probability for events that occur 90% of the time.

Calibration Metrics

Expected Calibration Error (ECE):

$$\text{ECE} = \sum_{b=1}^B \frac{|{i: \hat{p}_i \in B_b}|}{N} |\text{avg accuracy}_b - \text{avg confidence}_b|$$

where $B_b$ are confidence bins. Lower ECE indicates better calibration.

Brier Score:

$$\text{BS} = \frac{1}{N} \sum_i (\hat{p}_i - y_i)^2$$

Measures prediction error; combines accuracy and confidence.

Calibration Methods

Temperature Scaling:

$$\hat{p}_{\text{calibrated}} = \text{softmax}(\text{logits} / T)$$

Learn a single temperature parameter $T$ to scale logits. Higher $T$ (>1) reduces peak probabilities; lower $T$ (<1) sharpens them.

Platt Scaling:

Apply a logistic transformation:

$$\hat{p}_{\text{calibrated}} = \sigma(a \cdot \text{score} + b)$$

Fit $a, b$ on a held-out validation set.

Histogram Binning:

Empirically estimate the mapping from predicted probability to true frequency. Non-parametric but requires careful bin selection.

Out-of-Distribution Detection

Recognize when inputs are far from training data (OOD):

Methods

Softmax entropy: High entropy (uniform predictions) suggests OOD.

Maximum softmax probability: Low max probability suggests OOD.

Mahalanobis distance: Measure distance from learned class means in latent space.

Energy-based: Use energy of output distribution; OOD samples have lower energy.

Evaluation

AUROC: Distinguish OOD from in-distribution samples.

False Positive Rate at X% True Positive Rate: More stringent measure for safety.

Challenges

Computational Cost

Bayesian approaches require either sampling (multiple forward passes) or learning distributions (overhead). Ensembles require multiple models.

Uncertainty Collapse

Models can be overconfident despite poor calibration. Epistemic uncertainty decreases with data, even if aleatoric uncertainty remains high.

Distribution Shift

Uncertainty estimates trained on one distribution may not generalize to shifted data. Continual evaluation is necessary.

Applications

Medical Diagnosis

Flag uncertain predictions for human review. Combined with calibration, enables reliable triage systems.

Autonomous Vehicles

Uncertainty estimates help decide when to hand control to humans or request intervention.

Active Learning

Prioritize uncertain examples for labeling, improving sample efficiency.

Anomaly Detection

Identify OOD anomalies using uncertainty; threshold on confidence.

Best Practices

  1. Always evaluate calibration: Don’t assume softmax probabilities reflect true confidence.
  2. Use multiple approaches: Combine Bayesian, ensemble, and MC dropout estimates.
  3. Test on shifted data: Evaluate uncertainty generalization to distribution shift.
  4. Provide uncertainty in deployment: Report confidence intervals, not just point estimates.
  5. Human-in-the-loop: Use uncertainty for adaptive decision-making with human oversight.

Research Directions

  • Scalable Bayesian inference: Efficient approaches for large models.
  • Uncertainty for large language models: How certain are LLMs? Emerging research.
  • Adversarial robustness of uncertainty: Do uncertainty estimates remain reliable under attack?
  • Principled distribution shift: Theoretical frameworks for uncertainty under distribution shift.

Uncertainty quantification transforms neural networks from black-box predictors into principled probabilistic systems, essential for safe, interpretable AI deployment.