TL;DR
A Bayesian neural network is a feedforward network in which each weight is a probability distribution rather than a single number. A prediction is the average over many plausible weight settings, so the spread of those predictions measures how unsure the model is. Exact Bayesian treatment is out of reach for networks with millions of weights, so practical methods approximate it.
Builds on: Feedforward Neural Network, Posterior Predictive Distribution, Variational Lower Bound (ELBO) · Leads to: Uncertainty Quantification, Bayesian Model Averaging
Where it's used
Safety checks on predictions. A model that says "90 percent sure" is only useful if that number is honest. A Bayesian network reports a wide spread when the input looks unlike its training data, so a system can refuse to act or hand the case to a person. Any setting where a confident wrong answer is costly, such as an industrial sensor or a health-related classifier, is a candidate for this kind of flag. This describes the idea only; it is not advice for any medical decision.
Exploration in reinforcement learning. An agent that learns from rewards must try actions it is unsure about. Blundell et al. (2015) report a bandit experiment in which sampling weights from their learned distribution drives this exploration.
Regression away from the data. A plain network fitted to some points draws one confident curve, even far from the data where it knows nothing. A Bayesian network draws a band that widens where there are no points. The 2015 Blundell et al. paper used exactly this to show better behaviour in regions with little or no data.
How it works
Take the smallest possible network: one input, one weight , and the prediction . Training data does not pin down exactly, so we describe what we know about with a Gaussian distribution, , with mean and standard deviation . The question is: what does the network predict for a new input ?
-
Draw a weight. Pick a random number from a standard Gaussian (mean 0, standard deviation 1). Set the weight to . Take . Then . Check: lies within two standard deviations of , as most draws should.
-
Run the network with that weight. The prediction is . This is one plausible answer, given one plausible weight. Check: the output is a single number.
-
Repeat with more draws. Take . Then and . Take . Then and . Check: each draw gives its own weight and its own prediction.
-
Average and measure the spread. The three predictions are 2.5, 1.0 and 3.5. Their average is , which is the network's answer. Their spread is wide, so the network is unsure at this input. Check: a narrow set of predictions would mean the network is confident.
-
Training adjusts the distribution. Training does not guess once. It moves and so that the draws explain the training data well, while staying close to a simple prior belief about the weight. The "close to the prior" part is what stops the network from fitting noise, and it is also what keeps from collapsing to zero.
flowchart LR A[Input x] --> B[Draw weights from q of w] B --> C1[Forward pass 1] B --> C2[Forward pass 2] B --> C3[Forward pass S] C1 --> D[Average the predictions] C2 --> D C3 --> D C1 --> E[Spread of predictions = uncertainty] C2 --> E C3 --> E
The math
A Bayesian treatment starts from a prior over the weights and a likelihood for the training data . Training then gives a posterior . A prediction for a new input is the posterior predictive distribution:
which says: ask each weight setting for its prediction, and average those predictions, weighted by how believable each setting is after seeing the data.
Step 1 in symbols: the approximation
The integral has no closed form for a network with millions of weights, and the posterior is too hard to sample directly. So we pick a simple family , with parameters , and choose to make close to . For a diagonal Gaussian, , and each weight is written as
where is element-by-element multiplication. The function keeps positive whatever value takes. This is the same reparameterisation the paper by Blundell and colleagues uses. It lets a gradient flow through the sampled weight, which is why Backpropagation still works.
Step 4 in symbols: the prediction
The posterior predictive is approximated by averaging over sampled weight settings drawn from :
In the walkthrough, , and the average of the three predictions is the answer. The spread of those predictions is the measure of uncertainty.
Step 5 in symbols: what training maximises
Training chooses to maximise the evidence lower bound, which is the score from the variational lower bound:
The first term rewards weights that explain the data. The second term, a KL divergence, penalises a posterior that strays far from the prior. Because the expectation is hard to evaluate exactly, it is replaced by an average over a few sampled weights, and the gradient is taken with respect to and using the reparameterisation above.
Worked examples
Take and input . Use the draws , and .
- Compute the weights: , then , then .
- Compute the predictions : 2.5, 1.0 and 3.5.
- Average them: .
Interpretation: a single network would have given one number. Three draws show the answer ranges from 1.0 to 3.5, so this input is uncertain.
A common choice is a standard Gaussian prior . The KL divergence from to that prior has the closed form
Use and , so .
- Compute the parts: , , and the constant .
- Compute (rounded to four decimals).
- Add: .
- Halve: , to four decimals.
Interpretation: this weight's posterior costs about 0.82 nats of complexity in the training objective. A narrower posterior, with near zero, costs more, because grows without bound. This is the force that keeps from collapsing.
A hypothetical screening model gives, for one patient record, the probability of a positive label across four weight draws: 0.8, 0.6, 0.4 and 0.2. This is an invented illustration, not a real model or a medical result.
- Average the four numbers: .
- Find each distance from the average: , , and .
- Square each distance: , , and .
- Average the squares: . Take the square root for the spread: .
Interpretation: the averaged answer is 0.5, but the draws range across most of the probability scale. A reader should treat this record as uncertain, and a careful system would flag it rather than report 50 percent as if it were settled.
Try it: the draws are 0.9, 0.7, 0.7 and 0.5. What is the average, and does the set agree more or less than the example above? Answer: the average is . The spread is smaller, since the draws cluster between 0.5 and 0.9, so the model agrees with itself more than it did in the example.
Common mistakes and tradeoffs
It is tempting to think a Bayesian network is the same as Dropout. In fact dropout applied at prediction time can be read as an approximate Bayesian method. Gal and Ghahramani showed this in 2015 (published at ICML 2016). But the approximation family that dropout implies is narrow, and its uncertainty is not automatically well calibrated. Dropout is cheap, while full variational training is more expensive but gives the model control over each weight's spread.
It is tempting to think averaging the weights gives the Bayesian answer. In fact averaging the weights first gives one ordinary network, and for a nonlinear network that is not the average of the predictions. The posterior predictive averages predictions, not parameters. This is why the sampled average in step 4 is computed on outputs.
It is tempting to think the uncertainty is automatically honest. In fact it depends on the prior, on the approximating family, and on how many samples are used. A narrow variational family tends to understate the spread, and the result is a model that looks surer than it should. Checking calibration on held-out data is still necessary.
It is tempting to think the method is free. In fact a Gaussian over every weight doubles the number of parameters, since each weight has a and a , and each prediction needs forward passes. Use a Bayesian network when knowing what the model does not know is worth that cost, as in safety flags, exploration and small or costly datasets. Use an ensemble or a single network with a good calibration step when speed matters more. MCMC is more exact still, but it is slower than variational training for large networks.
History
In the early 1990s, David MacKay and others showed how to apply Bayesian reasoning to neural networks, using approximations to integrate over the weights. Radford Neal's work in the mid-1990s used Markov chain Monte Carlo to sample the posterior exactly, which was accurate but too slow for the networks of the day.
Alex Graves's 2011 work proposed a variational approximation to the weight posterior, and Blundell, Cornebise, Kavukcuoglu and Wierstra's 2015 paper, Weight Uncertainty in Neural Networks, made it practical. Their method, Bayes by Backprop, used the reparameterisation trick to train the weight distributions with ordinary gradient descent. Gal and Ghahramani's 2015 arXiv paper (ICML 2016) tied the widely used technique of dropout to this framework, which made the approach easy to try. Interest grew as large models needed a way to report what they did not know.
Recap
- Each weight in a Bayesian network is a distribution, described by a mean and a spread .
- A prediction is an average over several sampled weight settings, the posterior predictive approximated in Monte Carlo style.
- The spread of those predictions is the model's uncertainty, which is high far from the training data.
- Training maximises the evidence lower bound, trading data fit against a KL penalty that keeps weights close to the prior.
- Read next: Uncertainty Quantification, which asks which kinds of uncertainty a model should report, and Bayesian Model Averaging, the idea behind the averaging step.
Check yourself
QuestionIn a Bayesian neural network, what does a single weight stand for, and what is sampled to get one weight?Show answer
A weight stands for a distribution with a mean and a spread, for example . To get one weight, draw a standard Gaussian and compute .
QuestionA weight has mean 2 and standard deviation 0.5. The draw is . What is the weight?Show answer
.
QuestionWhy does the training objective include a KL term, instead of only rewarding fit to the data?Show answer
Without it, the best fit would push each towards zero and let each weight memorise the training points. The KL term penalises a posterior that moves far from the prior, which keeps the spread wide enough to reflect real uncertainty and limits overfitting.
QuestionA hypothetical model gives five predictions for one input: 0.2, 0.4, 0.4, 0.6 and 0.8. Average them and say whether the model is confident.Show answer
The sum is , so the average is . The predictions range from 0.2 to 0.8, a wide spread, so the model is not confident about this input even though the average is near one half.