Overview

10 Bayesian neural networks: Bringing uncertainty to deep learning

Neural networks are powerful function approximators, but in their standard form they produce single point predictions and give no sense of how uncertain those predictions are. This becomes a real limitation when data are noisy, sparse, or when the model is asked to extrapolate beyond what it has seen. The Bayesian perspective addresses this by treating the weights and biases of the network as random variables with prior distributions, then updating those beliefs with data to obtain a posterior distribution over plausible networks rather than committing to just one set of parameters.

The chapter shows how Bayesian neural networks can be built and trained by adapting familiar deep learning ideas. Because exact Bayesian inference is not available for such complex models, the text uses Monte Carlo methods and variational inference to approximate the posterior. MCMC gives high-quality posterior samples and therefore strong predictive uncertainty estimates, but it is slow and can be difficult to scale. Variational inference is much faster and can work well, especially with more samples and better optimizers, though its approximations may be less accurate than MCMC in complicated settings.

The chapter also explains dropout as a practical Bayesian approximation. Instead of only treating dropout as a regularization trick, it can be interpreted as sampling from a distribution over networks by randomly turning nodes on and off during training and keeping dropout active at prediction time to generate many forward passes. These repeated passes produce a predictive distribution whose mean and variability reflect both the prediction and its uncertainty. Overall, the chapter emphasizes the tradeoff among principled inference, computational cost, and scalability, and shows that uncertainty-aware deep learning is both possible and useful.

A sample training set where data are generated from the sine function and corrupted by noise.
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.
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.
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.
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 a higher level of predictive uncertainty.
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 a 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.
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.
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.
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.
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.
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.
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.
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.
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 (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 random variables rather than fixed values. Instead of learning a single best set of parameters, it learns a posterior distribution over plausible parameters given the data.
Why do standard neural networks struggle with uncertainty?Standard neural networks usually output a single prediction for each input, so they do not naturally express how uncertain that prediction is. This means they cannot easily represent aleatoric uncertainty from noisy data or epistemic uncertainty from sparse data.
What are aleatoric and epistemic uncertainty?Aleatoric uncertainty is the irreducible noise inherent in the data, such as measurement noise. Epistemic uncertainty comes from lack of knowledge, often because there is little or no training data in a region, and it can be reduced with more data.
How does Bayesian inference change neural network training?Instead of optimizing weights to a single point estimate, Bayesian inference places priors on the weights and biases and updates them to posterior distributions using data. Predictions are then made from the resulting predictive distribution rather than a single output.
Why can’t Bayesian neural networks usually be solved in closed form?Neural networks have complex architectures and many parameters, so Bayes’ rule does not lead to a tractable closed-form posterior. Because of that, approximate inference methods such as MCMC and variational inference are used instead.
How is MCMC used for Bayesian neural networks?MCMC samples from the posterior distribution of the network’s parameters after placing priors on them. Those posterior samples are then used to make probabilistic predictions, often summarized with a mean curve and credible intervals.
What is the main drawback of MCMC for deep learning?MCMC can be very computationally expensive because it needs many high-quality samples to approximate the posterior well. For larger networks or bigger data sets, this can make it too slow to be practical.
What is variational inference in Bayesian neural networks?Variational inference learns an approximate posterior distribution by minimizing KL divergence from the true posterior. In practice, it uses gradient-based optimization of the ELBO, making it typically faster than MCMC.
Why can variational inference give lower-quality predictions than MCMC?Variational inference uses an approximation to the posterior, which may miss important structure in the true distribution. In Bayesian neural networks, this approximation can lead to less accurate predictive uncertainty and weaker fits in parts of the input space.
How does dropout relate to Bayesian neural networks?Dropout can be interpreted as an approximate Bayesian inference method. By keeping dropout active at test time and making many forward passes, we sample from a distribution of predictions, a technique known as Monte Carlo dropout.

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


Close
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 Grokking Bayes ebook for free
Close
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 Grokking Bayes ebook for free