Repeated stochastic forwards can expose prediction variation, but enabling dropout by putting the entire model in training mode can corrupt normalization behavior.
Monte Carlo dropout, evaluation mode and batch-normalization boundaries
Know which randomness is active
Standard inference disables dropout. Monte Carlo dropout intentionally keeps dropout masks changing over several no-gradient forwards, then averages probabilities. A model must actually contain and have been trained with dropout for this procedure to have the intended meaning. Preserve a fixed input and preprocessing pipeline across passes; otherwise input augmentation and dropout uncertainty are mixed. The ensemble lesson offers a separate multi-checkpoint comparison.
Freeze batch normalization
Calling train on the entire network also makes batch-normalization layers use current-batch statistics and update running estimates. That can make the answer depend on neighboring requests or mutate the deployed model. Set the model to evaluation mode, then enable only the designated dropout modules. The code asserts batch normalization remains in evaluation mode and its running mean is unchanged across repeated forwards. Check any custom stochastic layers separately.
Aggregate class probabilities
Run several passes under no-grad or inference mode, collect per-class probabilities, then calculate their mean and variability. Compare the resulting review queue with a normal deterministic forward. More passes reduce Monte Carlo sampling noise but increase latency almost linearly. A high variance can flag unstable predictions; a low variance can still conceal a confidently shared error. Calibration and severe-defect recall remain external checks.
Keep request isolation
Changing module train flags on a shared model object can race with other inference requests. Use a dedicated stochastic copy, a scoped mode-restoration guard with serialization, or a serving architecture that isolates the uncertainty worker. The choice depends on concurrency requirements. Record the number of passes, seeds when reproducibility is needed and whether batch norm state remained fixed. Do not silently change normal production inference for every request.
Measure the decision benefit
Report severe-defect error capture, auto-approval risk, review volume and p95 latency at several pass counts. If a three-member deep ensemble gives better review yield at similar cost, use that evidence; if deterministic temperature-scaled confidence is sufficient, extra stochastic forwards may be wasteful. The applied project compares methods under held-out store and scanner changes.
Implementation
import torch
from torch import nn
torch.manual_seed(47)
receipt_features = torch.rand(4, 6)
review_model = nn.Sequential(nn.Linear(6, 8), nn.BatchNorm1d(8),
nn.ReLU(), nn.Dropout(p=0.35), nn.Linear(8, 2))
review_model.eval()
for layer in review_model.modules():
if isinstance(layer, nn.Dropout):
layer.train()
normalization = next(layer for layer in review_model.modules()
if isinstance(layer, nn.BatchNorm1d))
before = normalization.running_mean.clone()
with torch.inference_mode():
probabilities = torch.stack([review_model(receipt_features).softmax(dim=1)
for _ in range(7)])
mean_probability = probabilities.mean(dim=0)
assert probabilities.shape == (7, 4, 2)
assert torch.isfinite(mean_probability).all()
assert not normalization.training and torch.equal(before, normalization.running_mean)Performance and operating cost
For P stochastic passes and a model forward cost F, inference work is O(PF); storing all B-by-C probability matrices costs O(PBC), though streaming moments can reduce it to O(BC). Model weights remain one copy unless request isolation uses dedicated replicas. Batch-normalization state should not change. This synthetic mode check establishes the mechanics, not calibrated uncertainty or a production-safe concurrency strategy.
Common Mistakes
- Do not call train on the whole model just to enable dropout.
- Do not equate low dropout variance with low error risk.
- Do not mutate a shared inference model’s mode without request isolation.
