Skip to content

MC Dropout API

This page documents the wrapper used to keep dropout active at inference time and summarize predictive spread.

Public objects

  • MCDropoutWrapper

Parameter and variable conventions

Name Meaning
model dropout-enabled base model
n_mc number of stochastic forward passes
apply_softmax convert logits to probabilities before aggregation

Workflow expectations

  1. train the base model normally with dropout layers present
  2. wrap it with MCDropoutWrapper(model, n_mc=..., apply_softmax=...)
  3. call predict(...) or predict_uq(...)

Input and output shapes

  • x can have any shape accepted by the wrapped model.
  • predict(...) returns (mean, var) with the same trailing shape as a single forward pass.
  • if apply_softmax=True, the last dimension is interpreted as class probability.

UQResult mapping

predict_uq(...) populates mean, epistemic_var, and total_var. When apply_softmax=True, it additionally populates probs and probs_var.

Common preconditions and failure modes

  • the wrapped model must already contain dropout layers for MC Dropout to have any effect
  • n_mc should be positive and large enough to stabilize the variance estimate
  • apply_softmax=True should only be used when the wrapped model emits logits

Minimal example

mc_model = MCDropoutWrapper(model, n_mc=50, apply_softmax=False)
uq = mc_model.predict_uq(x_test)

deepuq.methods.mc_dropout

Monte-Carlo dropout inference wrappers.

The wrapper in this module keeps dropout layers active at inference time, collects repeated stochastic forward passes, and summarizes their spread as a predictive uncertainty estimate.

MCDropoutWrapper

Bases: Module

Wrap a dropout-enabled model to perform MC Dropout at inference.

Parameters:

Name Type Description Default
model Module

torch.nn.Module that already contains dropout layers.

required
n_mc int

Number of stochastic forward passes used at prediction time.

20
apply_softmax bool

If True, interpret model outputs as logits and return probability-space moments.

True

predict

predict(x: Tensor)

Run stochastic dropout passes and return predictive mean/variance.

Parameters:

Name Type Description Default
x Tensor

Input batch with shape accepted by the wrapped model.

required

Returns:

Type Description
(mean, var):

Tensors with the same trailing shape as a single model prediction. For classification with apply_softmax=True, the last dimension is class probability.

predict_uq

predict_uq(x: Tensor) -> UQResult

Return predictive moments as a :class:deepuq.types.UQResult.

The wrapper reports dropout spread as epistemic_var. No explicit aleatoric component is modeled.

train

train(mode: bool = True)

Mirror the wrapped model's train/eval state.

MC Dropout still forces dropout-active behavior inside predict and predict_uq by temporarily calling self.model.train(True).