Search

Search papers and lessons

Dropout as a Bayesian Approximation

Yarin Gal, Zoubin Ghahramani · 2015

Gal and Ghahramani (2015) show that a network trained with dropout can report model uncertainty by averaging many random forward passes, with no retraining and no change to the model.

On this page

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

TL;DR

Dropout is a training trick that randomly switches off units in a neural network. Gal and Ghahramani showed that a network trained this way is mathematically tied to an approximate Bayesian model. So if you keep dropout switched on at prediction time and run the network several times, the spread of the answers is a usable measure of how unsure the model is. The trained network does not change, so the same model gives a prediction and an error bar.

Builds on: Dropout, Bayesian Neural Network, Gaussian Process Regression, Monte Carlo Method, Kullback–Leibler Divergence, Posterior Distribution · Leads to: Uncertainty Quantification, Reinforcement Learning

Why it mattered

Dropout is used in many deep learning models as a cheap way to avoid overfitting, so the paper's claim applies to all of them. Before it, the usual ways to get uncertainty from a neural network were Bayesian neural networks, whose inference is hard and costly, or ensembles of several full networks. The paper showed that the uncertainty was already inside the dropout model and could be read out at almost no extra cost, since the forward passes can run side by side.

The problem

A standard classifier ends in a softmax, which turns scores into probabilities that add to 1. Those probabilities are often read as confidence, but they are not. Far from the training data a network can output a probability of 1 for one class even though nothing in its training supports that answer. The paper's Figure 1 makes the point: a point estimate pushed through a softmax gives unjustified certainty far from the data.

Bayesian treatment fixes this in principle. Put a distribution over the weights, and the predictions spread out where the data is thin. In practice the cost is high: the paper notes that inference in Bayesian neural networks is challenging and adds computational cost, and that variational methods for them have had only limited success. The question is whether uncertainty can come from a model that is already trained with dropout.

How it works

Take a network with one input and two hidden units, with weights that all equal 1 to the output. The input produces hidden values (2, 4). Dropout would randomly zero some of these. Here is the procedure, step by step.

  1. Train as usual. Train an ordinary network with dropout and weight decay. The paper's claim is that nothing about this step changes.
  2. Keep dropout on at prediction time. Do not switch to the averaged-weights mode that standard dropout uses at test time. Each forward pass draws a fresh random mask over the units.
  3. Run T passes. Pass the same input through the network T times, each time with a new mask. Take T = 4 with masks (1, 0), (0, 1), (1, 1), (0, 1). The outputs are 2, 4, 6 and 4.
  4. Average for the prediction. The mean is (2 + 4 + 6 + 4) / 4 = 4. This is the model's predictive mean.
  5. Measure the spread. The sample variance of the four outputs is 2. Check: the deviations from 4 are −2, 0, 2, 0, their squares sum to 8, and 8 / 4 = 2.
  6. Add the noise term. The paper adds the inverse model precision τ⁻¹ to the spread. With τ⁻¹ = 0.5 the predictive variance is 2 + 0.5 = 2.5, so the standard deviation is about 1.58. The answer is 4 ± 1.58, with a wider band where the passes disagree.
flowchart LR
  X["input x"] --> M1["pass 1: random mask"]
  X --> M2["pass 2: random mask"]
  X --> MT["pass T: random mask"]
  M1 --> Y1["y1"]
  M2 --> Y2["y2"]
  MT --> YT["yT"]
  Y1 --> A["mean of y1 to yT: prediction"]
  Y2 --> A
  YT --> A
  Y1 --> V["spread of y1 to yT plus tau inverse: uncertainty"]
  Y2 --> V
  YT --> V

The math

A network with L layers has weight matrices Wi\mathbf{W}_i and bias vectors bi\mathbf{b}_i. Training minimises the average loss EE over N data points plus an L2 penalty with weight λ\lambda:

Ldropout=1N∑i=1NE(yi,y^i)+λ∑i=1L(∥Wi∥22+∥bi∥22)\mathcal{L}_{\text{dropout}} = \frac{1}{N}\sum_{i=1}^{N} E(\mathbf{y}_i, \widehat{\mathbf{y}}_i) + \lambda \sum_{i=1}^{L}\big(\|\mathbf{W}_i\|_2^2 + \|\mathbf{b}_i\|_2^2\big)

Here y^i\widehat{\mathbf{y}}_i is the network output for input xi\mathbf{x}_i, and yi\mathbf{y}_i is the observed target.

Step 1 in symbols: the random mask

Each unit in layer ii is kept with probability pip_i and dropped otherwise. The paper writes the kept indicator as a Bernoulli variable zi,j∼Bernoulli(pi)\mathtt{z}_{i,j} \sim \text{Bernoulli}(p_i), so a dropped unit has zi,j=0\mathtt{z}_{i,j} = 0. A masked weight matrix is Wi=Mi⋅diag([zi,j])\mathbf{W}_i = \mathbf{M}_i \cdot \text{diag}([\mathtt{z}_{i,j}]), where Mi\mathbf{M}_i is a matrix of learned values. The paper's pip_i is the chance a unit stays on. The experiments quote the dropout probability, which is the chance it is switched off, so the two conventions must not be mixed.

Step 2 in symbols: the approximate posterior

A Bayesian model would average predictions over the true posterior p(ω∣X,Y)p(\boldsymbol{\omega} \mid \mathbf{X}, \mathbf{Y}) over all weights ω={Wi}\boldsymbol{\omega} = \{\mathbf{W}_i\}, which is intractable. The paper replaces it with the masked distribution q(ω)q(\boldsymbol{\omega}) and minimises the KL divergence between the two, which gives the objective

−∫q(ω)log⁡p(Y∣X,ω) dω+KL(q(ω) ∥ p(ω))-\int q(\boldsymbol{\omega}) \log p(\mathbf{Y} \mid \mathbf{X}, \boldsymbol{\omega})\, d\boldsymbol{\omega} + \text{KL}\big(q(\boldsymbol{\omega}) \,\|\, p(\boldsymbol{\omega})\big)

The first term is estimated with one Monte Carlo sample per data point, which is why the training loop looks like ordinary dropout. The second term becomes the L2 penalty, with the prior length-scale ll setting its size.

Step 3 in symbols: the predictive mean

The predictive distribution is q(y∗∣x∗)=∫p(y∗∣x∗,ω) q(ω) dωq(\mathbf{y}^*\mid\mathbf{x}^*) = \int p(\mathbf{y}^*\mid\mathbf{x}^*,\boldsymbol{\omega})\,q(\boldsymbol{\omega})\,d\boldsymbol{\omega}. Draw T sets of masks, giving weights W1t,…,WLt\mathbf{W}_1^t, \dots, \mathbf{W}_L^t for t=1,…,Tt = 1, \dots, T, and average:

E[y∗]≈1T∑t=1Ty^∗(x∗,W1t,…,WLt)\mathbb{E}\big[\mathbf{y}^*\big] \approx \frac{1}{T}\sum_{t=1}^{T} \widehat{\mathbf{y}}^*\big(\mathbf{x}^*, \mathbf{W}_1^t, \dots, \mathbf{W}_L^t\big)

This is the "MC dropout" prediction: T stochastic forward passes, averaged.

Step 4 in symbols: the predictive variance

The same sampling gives the second moment, and the variance follows as the sample variance plus the noise term:

Var(y∗)≈τ−1ID+1T∑t=1Ty^t Ty^t−E[y∗]TE[y∗]\text{Var}\big(\mathbf{y}^*\big) \approx \tau^{-1}\mathbf{I}_D + \frac{1}{T}\sum_{t=1}^{T} \widehat{\mathbf{y}}_t^{\,T}\widehat{\mathbf{y}}_t - \mathbb{E}\big[\mathbf{y}^*\big]^{T}\mathbb{E}\big[\mathbf{y}^*\big]

Here y^t\widehat{\mathbf{y}}_t is the output of pass tt as a row vector, so the products are outer products. Its scalar case is the one used in the walkthrough: the mean of the squared outputs minus the square of their mean, plus τ−1\tau^{-1}. This matches step 5 above: with outputs 2, 4, 6, 4 the mean of squares is 72 / 4 = 18, and 18 − 4² = 2.

Step 5 in symbols: the precision

The model precision τ\tau (the inverse of the observation noise variance) is tied to the weight decay by

τ=p l22Nλ\tau = \frac{p\, l^2}{2 N \lambda}

where ll is the prior length-scale, pp the keep probability, NN the number of data points and λ\lambda the weight decay. Given λ\lambda and ll, this fixes τ\tau, so no extra parameter is needed for the noise term.

Worked examples

Example
1. Two passes, by hand
  1. Two passes give outputs 1 and 3.
  2. The mean is (1 + 3) / 2 = 2.
  3. The mean of the squares is (1 + 9) / 2 = 5.
  4. The sample spread is 5 − 2² = 1.
  5. Add the noise term τ⁻¹ = 0.25: the predictive variance is 1 + 0.25 = 1.25.
  6. Standard deviation is √1.25 ≈ 1.12. The answer is 2 ± 1.12.
Example
2. Five passes, the standard case
  1. Five passes with dropout on give outputs 2, 4, 4, 6, 4.
  2. Mean: (2 + 4 + 4 + 6 + 4) / 5 = 20 / 5 = 4.
  3. Mean of squares: (4 + 16 + 16 + 36 + 16) / 5 = 88 / 5 = 17.6.
  4. Sample spread: 17.6 − 4² = 1.6. Check: the deviations are −2, 0, 0, 2, 0; their squares sum to 8; 8 / 5 = 1.6.
  5. Add τ⁻¹ = 0.4: the predictive variance is 2.0.
  6. Standard deviation is √2 ≈ 1.41. The answer is 4 ± 1.41, and the spread comes only from the passes.
Example
3. A realistic case: classifying a rotated digit

The paper tests a LeNet convolutional network trained on MNIST handwritten digits, with dropout (probability 0.5) placed before the last fully connected layer. It rotates one image of the digit 1 continuously and runs 100 stochastic forward passes at each angle.

  1. For the 12 rotation angles shown, the model's predicted class is 1 for the first five images, 5 for the next two, and 7 for the last five. The predicted classes are [1 1 1 1 1 5 5 7 7 7 7 7].
  2. For the leftmost image, the uncertainty band of class 1 sits well above the other classes. The model is confident, and the paper reads that input as classified with high confidence.
  3. For the middle image, the bands of two classes overlap. Even if one class has a high mean, its band can reach across the whole probability range.
  4. The paper's conclusion for that middle image is that it would be unreasonable to output class 5 when the uncertainty is that large. The right action is to ask an external annotator for a label.
  5. The measure to read is the spread of the sampled outputs for each class, not the single softmax value.

Try it: a model run 4 times gives outputs 3, 5, 3, 5, and τ⁻¹ = 1. Find the mean, the predictive variance and the standard deviation. Answer: mean = 16 / 4 = 4. Mean of squares = (9 + 25 + 9 + 25) / 4 = 17, so the spread is 17 − 16 = 1. The variance is 1 + 1 = 2, and the standard deviation is √2 ≈ 1.41.

Common mistakes and tradeoffs

It is tempting to think that a softmax probability is a measure of how sure the model is. In fact it is only the model's output for its own scores. Far from the training data it can be close to 1 while the model has no basis for that answer. Averaging passes with dropout on is what exposes the doubt.

It is tempting to think that dropout at test time just means "the usual test-time network". Standard dropout turns dropout off and scales the weights; that gives a point estimate with no uncertainty. MC dropout keeps the masks random and averages the outputs; that is where the uncertainty comes from.

It is tempting to think that the variance is only the spread of the passes. In fact the noise term τ⁻¹ is part of the answer. With few passes or a small spread, the noise term may dominate, which is why the variance never drops below the noise floor.

It is tempting to think that the average over passes is the exact Bayesian answer. In fact it is an approximation: the paper's derivation replaces the true posterior with a masked distribution, and the approximation is only as good as that choice. The paper also notes that the predictive distribution is highly multi-modal, so mean and variance give only a partial picture.

When to use it: you already have a dropout network and need a cheap uncertainty estimate, or you need a decision rule that can hand uncertain inputs to a person. The cost is T forward passes, and the passes can run in parallel.

When not to use it: when the network was never trained with dropout, when T must be very small and the spread is unreliable, or when the uncertainty must follow a principled posterior over the weights. Alternatives include Bayesian neural networks, ensembles of separately trained networks (see Ensemble Learning), and calibration methods.

Results

The paper's main quantitative claim is on UCI regression benchmarks, with RMSE (root mean squared error, lower is better) and average test log-likelihood (higher is better). Dropout is compared with a variational inference baseline (VI, Graves 2011) and with probabilistic backpropagation (PBP, Hernández-Lobato and Adams 2015). Numbers are mean ± standard error over random data splits.

  • Boston Housing (N = 506, Q = 13): dropout RMSE 2.97 ± 0.19, against 3.01 ± 0.18 for PBP and 4.32 ± 0.29 for VI. Test log-likelihood −2.46 ± 0.06, against −2.57 ± 0.09 for PBP.
  • Concrete Strength (N = 1,030): dropout RMSE 5.23 ± 0.12, against 5.67 ± 0.09 for PBP.
  • Kin8nm (N = 8,192): dropout RMSE 0.10 ± 0.00, the same as PBP and VI; test log-likelihood 0.95 ± 0.01, against 0.90 ± 0.01 for PBP.
  • Yacht Hydrodynamics (N = 308): PBP wins on RMSE, 1.02 ± 0.05 against dropout's 1.11 ± 0.09. The paper reports dropout ahead on the other datasets, both in RMSE and in test log-likelihood.

The paper's own limits are also clear. The precision τ was tuned by Bayesian optimisation over validation log-likelihood, with the length-scale set to 10⁻² for most datasets. The results were averaged over 20 random splits (5 for Protein, 1 for Year). Dropout with probability zero gave worse results on Boston Housing (RMSE 3.07, log-likelihood −2.59), so the randomness itself matters. The authors used the network as it was, without tuning its architecture, and say better architectures may give better uncertainty.

For the reinforcement learning test, a 2D agent with 9 eyes and 5 actions learned faster when its dropout Q-network was used with Thompson sampling: its reward exceeded 1 within 25 batches after burn-in, while epsilon-greedy needed 175 batches.

Reading guide

The main text is short and defers its derivations to a companion appendix (arXiv:1506.02157).

  1. Introduction and Section 2 (related research): read with Bayesian Neural Network and Gaussian Process Regression. The claim is that infinitely wide networks become Gaussian processes, and that finite Bayesian networks are costly.
  2. Section 3 (dropout as a Bayesian approximation): needs Dropout, Kullback–Leibler Divergence and Monte Carlo Method. The mask is Bernoulli Distribution. The key objective is the KL divergence to the posterior, see Posterior Distribution.
  3. Section 4 (obtaining model uncertainty): needs Expected Value and Mean and Variance for the moment formulas, and the regularisation view in L2 Regularization.
  4. Section 5 (experiments): needs Mean Squared Error for RMSE, Softmax for the classification part, and Epsilon-Greedy Policy and Reinforcement Learning for the last experiment.

Check yourself

QuestionWhy does the paper keep dropout switched on at prediction time, and what does that buy?Show answer

Keeping dropout on makes each forward pass use a different random subnetwork. The spread of their outputs measures how much the prediction depends on which units are present, and that spread is the uncertainty. Standard dropout turns dropout off and averages the weights, which gives one answer with no uncertainty.

QuestionFive passes give outputs 1, 1, 3, 5, 5 and τ⁻¹ = 0.5. What are the mean and predictive variance?Show answer

Mean = (1 + 1 + 3 + 5 + 5) / 5 = 3. Mean of squares = (1 + 1 + 9 + 25 + 25) / 5 = 61 / 5 = 12.2. Sample spread = 12.2 − 9 = 3.2. Predictive variance = 3.2 + 0.5 = 3.7.

QuestionWhy is the variance formula not just the sample variance of the outputs?Show answer

The formula adds τ⁻¹, the inverse model precision. The sample variance measures how much the sampled networks disagree. The noise term adds the variance the model expects in the data itself, which the sampling cannot show.

QuestionA classifier gives a softmax probability of 0.99 for class A on an input far from its training data. Using the paper's argument, what should you check, and how?Show answer

Run several stochastic passes with dropout on and look at the spread of the outputs for each class, or compute the entropy or variation ratio of the predicted classes. If the uncertainty bands of class A and another class overlap widely, the 0.99 says little about the model's actual knowledge, and the input should go to a person.

Sources

  • Gal, Y. and Ghahramani, Z. (2015). Dropout as a Bayesian Approximation: Representing Model Uncertainty in Deep Learning. arXiv:1506.02142. https://arxiv.org/abs/1506.02142
  • Srivastava, N. et al. (2014). Dropout: A simple way to prevent neural networks from overfitting. Journal of Machine Learning Research 15(1).
  • Neal, R. M. (1995). Bayesian learning for neural networks. PhD thesis, University of Toronto.

Read the full paper

Yarin Gal, Zoubin Ghahramani, 2015. arXiv:1506.02142