How does a dropout layer behave differently in training mode versus evaluation mode?
Last updated
In training mode, dropout randomly zeroes each activation with probability p and scales the survivors by 1/(1−p). In evaluation mode, it is the identity: nothing is dropped and nothing is scaled, so outputs are deterministic.
Training mode:
A fresh Bernoulli mask is sampled every forward pass, so each minibatch sees a different thinned network.
With inverted dropout, survivors are multiplied by
1/(1−p)so the expected activation is unchanged.Gradients flow only through the kept units.
Evaluation mode: The full network is used. Because scaling was already done in training, no correction is needed at inference.
How you switch modes:
PyTorch:
model.train()/model.eval(). These set thetrainingflag on everynn.Dropout(and on BatchNorm).Keras: the
trainingargument.fit()sets it to True, whilepredict()andevaluate()set it to False.
Common pitfalls:
Forgetting
model.eval()gives noisy, non-reproducible predictions and underestimated validation accuracy.model.eval()does not disable autograd. Wrap inference intorch.no_grad()for that.Intentional exception: Monte Carlo dropout keeps dropout active at inference and averages several passes to estimate uncertainty.
[lang:python]
drop = nn.Dropout(p=0.5)
x = torch.ones(6)
drop.train(); drop(x) # e.g. tensor([2., 0., 2., 2., 0., 0.])
drop.eval(); drop(x) # tensor([1., 1., 1., 1., 1., 1.])