PREP WITH PROOF
Menu
PYTORCH / PRACTICAL GUIDE

PyTorch Dropout in Train vs Eval Mode: Scaling and Debugging

See how PyTorch scales retained activations in training, why no_grad() leaves dropout active, and how to fix functional dropout that stays on in eval mode.

THE QUICK ANSWER

In training mode, PyTorch’s nn.Dropout zeros each element with probability p and divides retained values by 1 − p. In evaluation mode, nn.Dropout returns its input unchanged. Calling torch.no_grad() controls gradient recording, so it leaves training-mode dropout active. If you use torch.nn.functional.dropout in a module, pass training=self.training to make it follow model.eval().

Predict the retained value before running the code

Take an activation of 12 with p = 0.25. Training produces either 0 (25% chance) or 16 (75% chance). The average over many independent masks is 0.25 × 0 + 0.75 × 16 = 12. In evaluation, the dropout module returns 12 every time. This is called inverted dropout because the scaling happens during training. The average is a property of the distribution, so a small tensor does not need to contain exactly 25% zeros on any single forward pass.

The most common arithmetic slip is dividing by p. The mask keeps a value with probability 1 − p, so divide by that keep probability when the value survives.

Layer and modeDropped valueRetained or unchanged value
nn.Dropout(0.25), train016
nn.Dropout(0.25), evalNo dropping12

Source: PyTorch documentation — nn.Dropout ↗

Separate module mode from gradient recording

Say your validation script wraps a forward pass in no_grad(), but the same saved input still produces different logits. First check model.training and isolate the dropout layer with a constant nonzero input. Calling model.eval() switches standard dropout modules to evaluation behavior. For ordinary validation, pair it with torch.no_grad() to avoid recording gradients. If you return to training in the same process, call model.train() before training batches.

Evaluation mode can also be used with gradient recording enabled, for example when computing gradients with respect to an input for analysis. The table below refers to nn.Dropout and assumes the input requires gradients.

Module modeGradient recordingDropout behavior
train()EnabledRandom mask and scaling
train()no_grad()Random mask and scaling
eval()EnabledInput passed through
eval()no_grad()Input passed through

Source: PyTorch documentation — Autograd and evaluation mode ↗

Find functional dropout that ignores eval()

If the model is already in eval mode, look for a call to F.dropout(x, p=...) inside forward(). The function’s training argument defaults to True. A call that omits the flag will still mask activations even when the enclosing module is in eval mode. Pass training=self.training, or register an nn.Dropout layer on the module and call that layer.

Run this small CPU check on its own. Using a vector of constant threes makes the only valid active outputs 0 and 6. The final test checks that the corrected version returns the entire input; it avoids asserting exact random-mask positions. If a larger model remains stochastic after this check passes, inspect other random operations individually.

import torch
from torch import nn
from torch.nn import functional as F

class FunctionalDropout(nn.Module):
    def __init__(self, respect_mode):
        super().__init__()
        self.respect_mode = respect_mode

    def forward(self, x):
        if self.respect_mode:
            return F.dropout(x, p=0.5, training=self.training)
        return F.dropout(x, p=0.5)

x = torch.full((4096,), 3.0)
broken = FunctionalDropout(respect_mode=False).eval()
fixed = FunctionalDropout(respect_mode=True).eval()

with torch.no_grad():
    wrong = broken(x)
    assert ((wrong == 0) | (wrong == 6)).all().item()
    torch.testing.assert_close(fixed(x), x, rtol=0, atol=0)

Source: PyTorch documentation — functional dropout ↗

Try the same reasoning on a PTCA practice question

When a question gives you a probability and says an activation was retained, compute its training value conditional on survival. If it asks about the average output instead, include the zero outcome. For an evaluation-mode question, check whether the code uses a module or an explicit functional training flag before choosing an answer.

Open the free PTCA sample below, choose Study mode, and look for the question about a retained activation. Predict the value first, then check the explanation. The sample gives you different numbers from this walkthrough so you can verify that the reasoning transfers.

Source: PyTorch documentation — nn.Dropout ↗

KEEP THESE THREE THINGS

Your next steps

  1. Scale a retained training activation by the inverse keep probability.
  2. Check module mode and gradient recording independently.
  3. Pass training=self.training when functional dropout should respect eval().

Sources & official resources

Linked resources checked on 2026-11-26. Technical behavior and provider guidance can change; check the documentation for the version you use.

About this guide and its recommendations

Published by Prep With Proof with AI-assisted drafting. Study routines and examples are editorial suggestions, not official exam instructions or claims of a learner's personal results. Cited provider facts are linked; ownership of recommended Prep With Proof products is disclosed. No competitor endorsement or guaranteed certification outcome is implied.

Suggest a correction →