Lesson 9.3 · 8 min read · about 1 h to master at a very slow pace

Dropout

Randomly switch off hidden units during training. Inverted dropout as lectured: drop with probability p, scale the survivors by 1/(1−p), change nothing at test time. Why it prevents co-adaptation and how to use it in PyTorch.

Not startedJump to quiz

Inverted dropout

Training: for each mini-batch, drop each hidden unit independently with probability (set its output to 0). With a mask , that is 1 with probability :

A new mask is drawn for every mini-batch. Test: nothing is dropped and nothing is rescaled; the full network is used.

The factor keeps the expected value of each activation unchanged: . That's why no correction is needed at test time.

Worked example

, activations , mask . Then . At test time the layer outputs unchanged.

Why it works

  • No co-adaptation: a unit can't rely on particular other units being present, so each must learn something useful on its own instead of jointly fitting the noise with a few partners.
  • An ensemble: each mask picks out a sub-network, and all of them share weights. With droppable units there are sub-networks; using the full network at test time approximately averages them, which lowers variance.

In practice

  • Put nn.Dropout(p) after the activation.
  • for large fully connected layers, 0.1–0.2 elsewhere.
  • Call model.train() while training and model.eval() before evaluating; forgetting model.eval() is a classic bug (dropout stays on at test time).
  • Never apply dropout to the output layer.

Two conventions: use the lecture's

Older sources define as the probability of keeping a unit and scale the activations at test time instead. The lectured version defines as the probability of dropping a unit and scales by during training, with no change at test time. Both give the same expected activations; state which you use.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passWhy not drop units at test time too?

At test time you want the best, most stable prediction, which is the average over all the sub-networks. Using the full network (with training-time scaling already done) approximates that average in one pass. Random dropping at test time would make predictions noisy.

Going deeperDoes PyTorch's nn.Dropout(p) use p as the drop or keep probability?

The drop probability, with inverted scaling during training, exactly as lectured. nn.Dropout(0.2) zeroes 20% of activations in training mode and multiplies the rest by 1/0.8 = 1.25; in evaluation mode it does nothing.

First passWhat does "drop each unit with probability p" actually mean?

For every hidden neuron, flip a biased coin each mini-batch. With probability the neuron is switched off: its output is set to 0 for that mini-batch. With , about half the neurons are off, and a different half next time.

Mini-batchNeuron 1Neuron 2Neuron 3
1onoffon
2offonon
3ononoff
First passWhy scale the survivors up by 1/(1 − p), and is that why dropout works?

With , half the neurons are off, so the next layer would receive about half its usual input. Doubling the survivors () keeps the total the same on average, so at test time, when every neuron is on, nothing needs changing.

The boost is only bookkeeping; it isn't why dropout reduces overfitting. That comes from the dropping: neurons can't rely on particular partners (no co-adaptation), and training many thinned networks acts like an ensemble.

Memory line: "Drop and boost in training, use everything at test."

First passWhere does dropout go in backpropagation?

Unlike L1 and L2, dropout changes the forward pass: multiply each hidden output by its mask and by . Backpropagation then follows automatically. A dropped neuron output 0, so its is 0 and the weights into and out of it get no update in that mini-batch. The survivors' gradients carry the same factor.

Worked example

, , mask : . At test time the layer passes unchanged.

Cheatsheet

Inverted dropout

Train: drop each unit with prob. , , new mask per batch. Test: no dropping, no scaling.

Dropout in PyTorch

nn.Dropout(p) after the activation; model.train() / model.eval(); never on the output layer. for big FC layers, 0.1–0.2 otherwise.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateInverted dropout with : activations , mask . What does the layer output during training?
Q2conceptIn the lectured (inverted) dropout, what happens at test time?
Q3conceptWhat does dropout prevent, according to the usual explanation?
Q4applyYour validation accuracy looks noisy and lower than expected, and predictions change each run on the same input. What did you probably forget?
Q5interpretAn older answer key says "each neuron is kept with probability p and activations are scaled at test time". How does it relate to the lectured version?