Search

Search papers and lessons

Weight Uncertainty in Neural Networks

Charles Blundell, Julien Cornebise, Koray Kavukcuoglu et al. · 2015

Blundell and colleagues learn a probability distribution over every weight of a neural network with backprop-style updates, which regularises the network and gives it useful uncertainty.

On this page

  • Colour = topic mix
  • Probability
  • Inference
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Bayes by Backprop trains a neural network in which every weight is a probability distribution rather than a single number. The training objective is the variational free energy, a KL divergence term that keeps the weights close to a prior plus a data-fit term, and the authors optimise it with a small change to ordinary Backpropagation. On MNIST digit classification the method matches Dropout, and the learned uncertainty helps in a regression toy problem and in a contextual bandit task.

Builds on: Variational Lower Bound (ELBO), Reparameterization Trick, Bayesian Neural Network · Leads to: the later line of work on practical Bayesian deep learning and on uncertainty-driven exploration

Why it mattered

The paper showed that the Bayesian treatment of a network's weights could be trained at the scale of an ordinary feedforward network. Before it, variational methods for neural weights had closed-form cost terms that limited which prior and posterior pairs could be used. The authors dropped the closed form and used Monte Carlo samples throughout, which made the prior freely choosable.

Its ideas now show up in three places. First, the weight-distribution view of a Bayesian Neural Network, where each sample of the weights is one network in an ensemble. Second, the observation that a network's uncertainty about its weights gives a principled way to explore, which is the basis of Thompson sampling for bandits. Third, the reparameterisation of a random variable as a fixed function of noise, which the paper generalised from hidden units to every weight of a network.

The problem

A plain network is trained to find one weight setting by maximum likelihood. Such a network has two known weaknesses, which the introduction states directly: it overfits, and it makes overly confident decisions where the training data are thin. Regularisers such as early stopping, weight decay and Dropout address the first problem by heuristics. The paper asks for a principled regulariser that also gives calibrated uncertainty.

The Bayesian answer is to keep a distribution over the weights, the posterior given the data. That posterior cannot be computed exactly for a network with thousands or millions of weights. So the paper uses a variational approximation: choose a simple distribution q over the weights, with parameters θ, and make it as close as possible to the true posterior. Closeness is measured by a KL divergence, and minimising it reduces to minimising a single cost. Two parts of that cost are the key: a complexity term that penalises q for moving far from the prior, and a likelihood term that rewards q for explaining the data.

How it works

Take one weight, one training example and one gradient step. The weight has a distribution with mean μ = 0.5 and standard deviation σ = 0.1. Because the paper parameterises σ = log(1 + exp(ρ)), this corresponds to ρ ≈ -2.25. The numbers below are illustrative and were worked by hand. They are not from the paper.

  1. Describe the weight by two numbers. The variational posterior is a Gaussian with mean μ and standard deviation σ. Store ρ instead of σ, so that σ = log(1 + exp(ρ)) is always positive. Check: with ρ = -2.25, log(1 + exp(-2.25)) is about 0.10.
  2. Draw noise. Draw ε from a standard normal distribution, with mean 0 and variance 1. Suppose the draw is ε = 1.5. The network never sees ε directly, only the weight built from it.
  3. Build the weight from the noise. Set w = μ + σ ε = 0.5 + 0.1 × 1.5 = 0.65. This is the reparameterisation: the random choice is moved into ε, and the weight becomes a fixed function of μ, ρ and ε. Check: the weight is 0.65, which lies above the mean by 1.5 standard deviations, as the draw said it should.
  4. Run the network with this weight and measure the cost. The cost for the sample is the complexity term minus the log-likelihood term. Suppose the backward pass gives ∂f/∂w = 0.8 for this sample.
  5. Turn the gradient into updates for μ and ρ. For this sample, take the direct terms ∂f/∂μ and ∂f/∂ρ as zero, so only the path through w counts. Then Δμ = ∂f/∂w = 0.8, and Δρ = ∂f/∂w × ε / (1 + exp(-ρ)) = 0.8 × 1.5 / 10.5 ≈ 0.11. The same backward pass gives both. Check: the only extra work is multiplying by ε and by a sigmoid factor.
  6. Step downhill. With a learning rate of 0.1, μ becomes 0.5 - 0.1 × 0.8 = 0.42, and ρ changes by about -0.011. So σ shrinks from 0.100 to about 0.099. Check: μ and σ both moved in the direction that lowers the cost on this sample.
  7. Repeat with a fresh draw of ε. Each step uses new noise, so over many steps the averaged gradient estimates the true gradient of the variational cost.
flowchart LR
  A["Draw noise eps from a standard normal"] --> B["Build weights w = mu + sigma times eps"]
  B --> C["Forward pass and cost"]
  C --> D["Backprop gives the gradient with respect to w"]
  D --> E["Scale and shift it into updates for mu and rho"]
  E --> A

The math

Let D\mathcal{D} be the training data and w\mathbf{w} the weights. The variational posterior is q(w∣θ)q(\mathbf{w}|\theta), with θ\theta the parameters that the method learns. The cost is the variational free energy:

F(D,θ)=KL[q(w∣θ) ∥ P(w)]−Eq(w∣θ)[log⁡P(D∣w)]\mathcal{F}(\mathcal{D}, \theta) = \text{KL}\left[q(\mathbf{w}|\theta) \,\|\, P(\mathbf{w})\right] - \mathbb{E}_{q(\mathbf{w}|\theta)}\left[\log P(\mathcal{D}|\mathbf{w})\right]

The first term is the complexity cost, which keeps q close to the prior P(w). The second is the likelihood cost, which rewards weights that explain the data. Minimising this cost is the same as maximising the expected lower bound on the marginal likelihood, which is the evidence lower bound.

Step 3 in symbols: the reparameterisation

For a diagonal Gaussian posterior, the paper draws ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, I) and sets

w=t(θ,ϵ)=μ+log⁡(1+exp⁡(ρ))∘ϵ\mathbf{w} = t(\theta, \epsilon) = \boldsymbol{\mu} + \log(1 + \exp(\boldsymbol{\rho})) \circ \epsilon

where ∘\circ is element-wise multiplication. The paper's Proposition 1 says that the derivative of an expectation over qq can be written as an expectation of a derivative, once the sample is written as a deterministic function of noise. This is the Reparameterization Trick applied to every weight.

Step 4 and 5 in symbols: the gradients

Write f(w,θ)=log⁡q(w∣θ)−log⁡P(w)P(D∣w)f(\mathbf{w}, \theta) = \log q(\mathbf{w}|\theta) - \log P(\mathbf{w}) P(\mathcal{D}|\mathbf{w}), the cost of one sample. The gradients for the two parameter vectors are

Δμ=∂f∂w+∂f∂μ\Delta_{\boldsymbol{\mu}} = \frac{\partial f}{\partial \mathbf{w}} + \frac{\partial f}{\partial \boldsymbol{\mu}}

Δρ=∂f∂w⋅ϵ1+exp⁡(−ρ)+∂f∂ρ\Delta_{\boldsymbol{\rho}} = \frac{\partial f}{\partial \mathbf{w}} \cdot \frac{\epsilon}{1 + \exp(-\boldsymbol{\rho})} + \frac{\partial f}{\partial \boldsymbol{\rho}}

The term ∂f/∂w\partial f / \partial \mathbf{w} is the usual backpropagation gradient, shared by both. The factor 1/(1+exp⁡(−ρ))1/(1 + \exp(-\rho)) is the derivative of log⁡(1+exp⁡(ρ))\log(1 + \exp(\rho)), the function that turns ρ into σ. The paper's point is that learning a distribution costs only a scale and a shift on top of ordinary backprop.

Step 6 in symbols

The update is gradient descent with learning rate α: μ←μ−αΔμ\boldsymbol{\mu} \leftarrow \boldsymbol{\mu} - \alpha \Delta_{\boldsymbol{\mu}} and ρ←ρ−αΔρ\boldsymbol{\rho} \leftarrow \boldsymbol{\rho} - \alpha \Delta_{\boldsymbol{\rho}}.

The prior: a scale mixture

The paper uses a prior that is a mixture of two zero-mean Gaussians for each weight:

P(w)=∏j[π N(wj∣0,σ12)+(1−π) N(wj∣0,σ22)]P(\mathbf{w}) = \prod_j \left[\pi\, \mathcal{N}(\mathbf{w}_j | 0, \sigma_1^2) + (1 - \pi)\, \mathcal{N}(\mathbf{w}_j | 0, \sigma_2^2)\right]

The first component has the larger variance, which gives a heavy tail. The second has a small variance, which concentrates most weights near zero. The prior is a Mixture Model of two Gaussians, and its settings π, σ1 and σ2 are fixed during training and chosen by cross-validation where possible, rather than learned, because the paper found learning them performed worse.

Minibatches

When the data are split into M minibatches, the paper weights the complexity term for minibatch i by

πi=2M−i2M−1\pi_i = \frac{2^{M-i}}{2^M - 1}

These weights sum to 1, since the powers of 2 from 202^0 to 2M−12^{M-1} add up to 2M−12^M - 1. For M = 4 they are 8/15, 4/15, 2/15 and 1/15. The early minibatches therefore carry most of the complexity cost, and the later ones are driven mostly by the data.

Results

MNIST classification. The data are the standard MNIST digits, with 60,000 training images and 10,000 test images of size 28 by 28. To tune hyperparameters, the authors trained on 50,000 digits and kept 10,000 for validation. The network has two hidden layers of rectified linear units and a softmax output with 10 units. The test error results are in the paper's Table 1:

MethodUnits per layerWeightsTest error
SGD, dropout400500k1.51%
SGD, dropout8001.3m1.33%
SGD, dropout12002.4m1.36%
Bayes by Backprop, Gaussian prior400500k1.82%
Bayes by Backprop, Gaussian prior8001.3m1.99%
Bayes by Backprop, Gaussian prior12002.4m2.04%
Bayes by Backprop, scale mixture prior400500k1.36%
Bayes by Backprop, scale mixture prior8001.3m1.34%
Bayes by Backprop, scale mixture prior12002.4m1.32%

The scale mixture prior is the variant that matches dropout, with 1.32% at the largest size. The Gaussian prior is clearly worse, which is why the mixture is central to the paper. The authors also quote a plain SGD network without regularisation at 1.6%, from earlier work. They note the Bayesian method takes about twice as long per iteration as dropout. Over long training it eventually reaches a lower test error than dropout, after about 600 epochs.

Pruning. The authors rank weights by the ratio of their mean to their standard deviation, the signal-to-noise ratio, and remove the lowest-ranked. Table 2 of the paper gives the error of a 1200-unit, two-layer network after pruning:

Proportion removedWeights remainingTest error
0%2.4m1.24%
50%1.2m1.24%
75%600k1.24%
95%120k1.29%
98%48k1.39%

Removing 95% of weights costs 0.05 percentage points. The drop becomes significant only at 98%. This is a useful result, but the paper is careful to say that the network used for pruning is the one from the end of training, not the one with the lowest validation cost.

Regression. On a noisy one-dimensional curve, a standard network becomes overconfident where no training data lie. The Bayesian network's interquartile ranges widen in those gaps. This is an illustration on toy data, not a benchmark.

Contextual bandit. On the UCI Mushroom data, an oracle that eats every edible mushroom and skips every poisonous one earns 5 per edible mushroom and 0 per poisonous one. Regret is the gap between the oracle's total reward and the agent's. The Bayesian agent scores each action by averaging two outputs from two samples of its weights, so its exploration comes from the weight uncertainty itself. This is close in spirit to Thompson sampling, which draws one plausible model and acts as if it were true. It is compared with epsilon-greedy agents with ε of 0%, 1% and 5%, averaged over five runs. The Bayesian agent explores from the start, and its cumulative regret is almost flat after it converges. The greedy agent eats nothing for the first roughly 1,000 steps. The paper's own reading is that its 5% agent over-explores.

Limits. The MNIST comparison is on one dataset, the regression is a toy, and the bandit is a single simulated task. The prior's settings were chosen by cross-validation where possible, and the paper reports that learning them during training performed worse.

Reading guide

Check yourself

QuestionRecall: the variational free energy has two parts. Name them and say what each one rewards or penalises.Show answer
The complexity cost is the KL divergence between the variational posterior and the prior. It penalises a posterior that moves far from the prior. The likelihood cost is the negative expected log-likelihood of the data under the posterior. It rewards weights that explain the data.
QuestionComputation: Table 2 keeps 120k of 2.4m weights at 95% removal, and 48k at 98%. What share of the weights remains at 98%, and how many percentage points did the test error rise from the unpruned network?Show answer
The share remaining is 48k / 2.4m = 2%. The test error rose from 1.24% to 1.39%, which is 0.15 percentage points.
QuestionWhy: the paper uses a scale mixture prior rather than a single Gaussian. Why is the mixture the better choice for MNIST, according to Table 1?Show answer
The mixture has a heavy tail and a sharp spike at zero. It lets many weights stay near zero while others take large values. In Table 1 it reaches 1.32% to 1.36% test error, while the single Gaussian prior is between 1.82% and 2.04%.
QuestionApplied: a team wants a network that says when it does not know, and their current model is confident in the regions of input space with no training data. Using this paper, what would they change and what should they expect?Show answer
They would place a distribution over the weights and train it with the free energy, using a scale mixture prior chosen on validation data. Predictions in the empty regions should widen, as in the regression figure. They should expect each iteration to take about twice as long as dropout, and they should validate the gain on their own data, since the paper's evidence is limited.

Sources

Read the full paper

Charles Blundell, Julien Cornebise, Koray Kavukcuoglu et al., 2015. arXiv:1505.05424