Overview

10 Bayesian neural networks: Bringing uncertainty to deep learning

The chapter introduces Bayesian neural networks as a way to bring uncertainty into deep learning. Standard neural networks are powerful function approximators, but they return only point predictions and give no indication of how confident they are. By placing prior distributions over weights and biases and updating them with data, a Bayesian neural network produces predictive distributions instead of single outputs, making it possible to represent both observation noise and uncertainty caused by limited data.

To build these models, the chapter frames neural network training as Bayesian inference over parameters. Because exact posterior calculation is not available for neural networks, it relies on two general-purpose approaches: MCMC, which samples from the true posterior but can be slow, and variational inference, which learns an approximate posterior more quickly by optimizing the ELBO. The chapter shows how both methods can be implemented in PyMC, and illustrates the practical tradeoff between accuracy and computational cost: MCMC tends to give higher-quality uncertainty estimates, while variational inference scales better but may produce weaker approximations if optimization is unstable or the model is too complex.

The chapter also presents dropout as a Bayesian approximation for neural networks. Originally a regularization method, dropout can be interpreted as sampling from a distribution over network architectures, and keeping dropout active at test time yields Monte Carlo samples of predictions. This MC dropout approach is lightweight and easy to integrate into existing deep learning workflows, though its uncertainty estimates are generally less accurate than those from full Bayesian inference. Overall, the chapter emphasizes that Bayesian reasoning and deep learning fit naturally together, and that the right inference method depends on the size of the problem and the level of precision required.

A sample training set where data are generated from the sine function and corrupted by noise.
The architecture of an example neural network with two hidden layers of size 6 and 4.
Predictions made by a standard neural network, which don’t include uncertainty quantification despite its importance in indicating both observation noise and data sparsity.
Parallels in non-Bayesian and Bayesian approaches to linear regression and deep learning. For each model, the non-Bayesian method finds the parameters that minimize the training error, while the Bayesian approach places a prior on each parameter and infers the posterior from data.
Predictions by a Bayesian neural network trained with MCMC. The mean line interpolates the data. The credible intervals cover most of the observed data and indicate observational noise. They also get larger in regions without observed data, indicating higher level of predictive uncertainty.
Predictions by a Bayesian neural network trained with variational inference. The predictions on the right-hand side don’t capture the downtrend of the data, illustrating the low quality of the parameters’ approximate posterior distributions.
The value of the negative ELBO as a function of the number of optimization steps. The values vary widely, indicating unstable optimization behavior.
By using more samples to approximate the ELBO’s gradient and the Adam optimizer, optimization is more stable and yields a lower loss. The trained model effectively captures the downtrend in the data.
When we don’t have access to the true observation noise level and instead model it with a half-normal distribution, the quality of the predictions by variational inference isn’t as high as that with MCMC.
Illustration of dropout. Left: a standard neural network. Right: After dropout is applied, specific nodes are randomly selected to be dropped for a given training step, producing zero output.
Each dropout step generates a new set of samples of Bernoulli variables deciding the presence of the neural network’s nodes. Each sample corresponds to a neural network drawn from a distribution of networks.
At test time, for a given input, we perform multiple forward passes with dropout enabled to obtain a distribution of predictions, which serves as our predictive distribution for that input.
Predictions by a Bayesian neural network via Monte Carlo dropout. Predictions roughly cover the observed data.

Summary

  • Traditional neural networks produce point predictions by learning a single set of weights that minimize a training loss. Because their parameters are fixed after training, standard neural networks cannot quantify predictive uncertainty.
  • Two forms of uncertainty arise in predictive modeling: aleatoric uncertainty, which comes from inherent noise in the data, and epistemic uncertainty, which arises from limited knowledge about the model parameters.
  • A Bayesian neural network places probability distributions over the network’s weights and biases instead of learning single point estimates. Predictions by such a network are therefore probabilistic.
  • Bayesian neural networks combine the expressive power of deep learning with the uncertainty quantification of Bayesian inference, enabling more reliable predictions in domains where uncertainty matters.
  • Because Bayesian neural networks do not admit closed-form posteriors, inference typically relies on approximate methods.
  • MCMC produces high-quality uncertainty estimates but can be computationally expensive for neural networks.
  • Variational inference approximates the posterior with a parameterized distribution and learns its parameters through optimization. It scales better than MCMC but may produce lower-quality posterior approximations.
  • Optimization of the variational objective (the ELBO) can be unstable in complex models like neural networks, but techniques such as additional Monte Carlo samples and adaptive optimizers like Adam can improve performance.
  • Dropout, originally introduced as a regularization technique in deep learning, can be interpreted as an approximate Bayesian inference method. Specifically, dropout introduces Bernoulli random variables that randomly remove nodes during training, implicitly defining a distribution over neural network architectures.
  • Keeping dropout active during prediction and performing multiple forward passes yields Monte Carlo samples from an approximate predictive distribution, a technique known as Monte Carlo dropout.
  • Keeping dropout active during prediction and performing multiple forward passes yields Monte Carlo samples from an approximate predictive distribution, a technique known as Monte Carlo (MC) dropout.
  • Some regularization techniques have Bayesian interpretations. For example, ridge regression corresponds to placing a Gaussian prior on model coefficients, while dropout can be interpreted as a variational approximation in a Bayesian neural network.

FAQ

What is a Bayesian neural network?

A Bayesian neural network is a neural network that treats its weights and biases as probability distributions rather than fixed values. This lets the model represent uncertainty in its parameters and make probabilistic predictions instead of only single-point outputs.

Why are standard neural networks not enough for uncertainty estimation?

Standard neural networks usually produce one prediction for each input, but they do not quantify how uncertain that prediction is. As a result, they can look overly confident even in noisy regions or where training data are sparse.

What kinds of uncertainty does this chapter discuss?

The chapter distinguishes between aleatoric uncertainty, which comes from inherent noise in the data, and epistemic uncertainty, which comes from limited knowledge or lack of data. Bayesian neural networks can help express both kinds of uncertainty.

How does a Bayesian neural network differ from a regular neural network during training?

Instead of learning a single best set of weights and biases, a Bayesian neural network places priors on the parameters and infers posterior distributions from the data. This means training focuses on distributions over parameters, not just point estimates.

Why can’t we usually solve Bayesian neural networks in closed form?

Neural networks have complex architectures and many parameters, so Bayes’ rule does not lead to a conjugate posterior that can be computed exactly. Because of this, approximate inference methods such as MCMC and variational inference are needed.

What role does MCMC play in Bayesian neural networks?

MCMC is used to draw samples from the posterior distribution over network parameters. These samples can then be used to make predictive distributions, often giving high-quality uncertainty estimates, though at a high computational cost.

What is variational inference in the context of Bayesian neural networks?

Variational inference approximates the true posterior with a simpler distribution by minimizing KL divergence between them. It is typically much faster than MCMC and is optimized using gradient-based methods such as ELBO maximization.

Why can variational inference be less accurate than MCMC?

Variational inference uses an approximate posterior, which may not capture the full complexity of the true posterior distribution. In this chapter, that approximation sometimes leads to predictions that miss important patterns in the data, especially in harder regions of the input space.

How is dropout connected to Bayesian inference?

Dropout can be interpreted as an approximate Bayesian method where each node is randomly turned on or off according to Bernoulli variables. By keeping dropout active at test time and doing multiple forward passes, we can obtain a predictive distribution.

When should you use MCMC, variational inference, or MC dropout?

MCMC is the most principled choice and works well for small problems, but it is expensive. Variational inference is faster and scales better to larger problems, while MC dropout is the lightest and easiest to integrate into deep learning pipelines, though it usually gives the roughest approximation.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bayes ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bayes ebook for free