Topic 24 of 62 · Generalizing and measuring it
Dropout and evaluation mode
EasyHow does inverted dropout work during training, and why must you switch to eval mode at inference?
1 · Start with the intuition
During training, dropout zeroes out each activation coordinate independently with probability p so downstream layers cannot rely on brittle co-adaptations of specific features. To keep the expected magnitude of activations identical between training and inference, frameworks use inverted dropout: surviving activations are multiplied by 1/(1 − p) during training, so at evaluation time the layer simply becomes the identity function.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Sample Bernoulli mask
For each coordinate, draw mᵢ ~ Bernoulli(1 − p) independently on every forward pass during training.
Next, we will look at: Rescale survivors by 1/(1 − p). Move on when this operation makes sense.
Visualizing the behavior
Inverted dropout with p = 0.25 (keep probability 0.75)
Derived3 · The governing equation
mᵢ ~ Bernoulli(1 − p), 0 ≤ p < 1 Training: yᵢ = mᵢ · xᵢ / (1 − p) ⇒ E[yᵢ | xᵢ] = (1 − p) · xᵢ / (1 − p) = xᵢ Evaluation: yᵢ = xᵢ
Notation: p is the drop probability (the fraction of activations zeroed); 1 − p is the keep probability.
Scaling by 1/(1 − p) during training rather than multiplying by (1 − p) at inference means inference code needs zero extra arithmetic—dropout layers become no-ops in eval mode. In PyTorch, two controls are often confused: model.eval() flips a boolean flag on modules like Dropout and BatchNorm to use their inference behavior, whereas torch.no_grad() tells the autograd engine not to record operations or store intermediate activations for backward. You need both at evaluation time.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Thinking torch.no_grad() turns off dropout, or that dropout permanently removes parameters to shrink the model. Wrapping an evaluation loop in torch.no_grad() without calling model.eval() leaves dropout randomly zeroing and rescaling activations on every test example.
In practice · implementation & engineering rule of thumb
Use a context manager or explicit try/finally so switching a model into eval() mode for mid-training validation never accidentally leaves dropout disabled for the rest of training.
was_training = model.training
model.eval()
with torch.inference_mode():
val_metrics = run_validation(model)
if was_training:
model.train() # Restore training mode so dropout re-engages!Go one level deeper
Follow-ups that often come after a correct first answer.
- Dropout in large-scale pretraining
- Single-epoch pretraining on a very large corpus often uses little or no dropout, because each example is seen about once and overfitting pressure is low. It is tuned more often when fine-tuning or in smaller-data settings.
- Where it goes in a Transformer
- The original Transformer applied dropout (P = 0.1) to each sublayer’s output before the residual add, and to the embedding sums. Many implementations also drop attention probabilities. Each placement regularizes something different, so name which one you mean.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhat happens if you accidentally leave dropout enabled during a pairwise LLM evaluation?
Show follow-up reasoning
Every forward pass injects random noise into the hidden states, increasing output variance, degrading generation quality, and making identical greedy decoding runs non-deterministic.