Equations · Srivastava et al., “Dropout: A Simple Way to Prevent Neural Networks from Overfitting” (2014)

The dropout formula, explained

During training, dropout zeroes a random subset of a layer's outputs on every step, so no unit can rely on any particular other unit being there. At test time nothing is dropped, and the outputs are rescaled so their expected size matches training.

r(l)∼Bernoulli⁡(p),y~(l)=r(l)⊙y(l)\mathbf{r}^{(l)} \sim \operatorname{Bernoulli}(p), \quad \tilde{\mathbf{y}}^{(l)} = \mathbf{r}^{(l)} \odot \mathbf{y}^{(l)}

Open in the decoder →

Term by term

  1. y(l)\mathbf{y}^{(l)}

    The outputs of layer ll. The (l)(l) is a layer index, not a power.

  2. r(l)∼Bernoulli⁡(p)\mathbf{r}^{(l)} \sim \operatorname{Bernoulli}(p)

    A random mask, drawn fresh for every example and every step: each entry is independently 1 with probability pp and 0 otherwise.

  3. pp

    In the paper, pp is the probability of keeping a unit (0.5 for hidden layers, 0.8 for inputs). PyTorch's dropout argument p means the opposite: the probability of dropping. Check which one a paper means.

  4. y~(l)=r(l)⊙y(l)\tilde{\mathbf{y}}^{(l)} = \mathbf{r}^{(l)} \odot \mathbf{y}^{(l)}

    The thinned outputs: the mask times the outputs, entry by entry. The tilde marks a modified version of y\mathbf{y}. This is what the next layer sees.

A small worked example

With p=0.8p = 0.8, y=(2,4,6,8)\mathbf{y} = (2, 4, 6, 8) and a drawn mask r=(1,0,1,1)\mathbf{r} = (1, 0, 1, 1): y~=(2,0,6,8)\tilde{\mathbf{y}} = (2, 0, 6, 8). The paper scales the weights by pp at test time, so outputs average what training saw. Most code uses "inverted dropout" instead: divide by pp during training, giving (2.5,0,7.5,10)(2.5, 0, 7.5, 10), and do nothing at test time.

In code

import numpy as np

def dropout(y, keep=0.8, training=True, rng=np.random.default_rng(0)):
    if not training:
        return y
    r = rng.random(y.shape) < keep    # Bernoulli(keep) mask
    return y * r / keep               # inverted dropout: rescale now, not at test time

Every symbol in it

x∈Rd\mathbf{x} \in \mathbb{R}^{d}Bold lowercase (vector)x(i),  W[l]\mathbf{x}^{(i)},\; W^{[l]}Superscript in parenthesesx∼pdatax \sim p_{\text{data}}Distributed as (tilde)m∼Bernoulli⁡(p)m \sim \operatorname{Bernoulli}(p)Bernoulli distributionp(x),  q(z)p(\mathbf{x}), \; q(\mathbf{z})Probability p(x)y=Wx+by = Wx + bItalic letters (variables)x~\tilde{x}Tilde accenta⊙b\mathbf{a} \odot \mathbf{b}Hadamard (elementwise) product

Other equations