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¶
- train the base model normally with dropout layers present
- wrap it with
MCDropoutWrapper(model, n_mc=..., apply_softmax=...) - call
predict(...)orpredict_uq(...)
Input and output shapes¶
xcan 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_mcshould be positive and large enough to stabilize the variance estimateapply_softmax=Trueshould only be used when the wrapped model emits logits
Minimal example¶
Related docs¶
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 |
| required |
n_mc | int | Number of stochastic forward passes used at prediction time. | 20 |
apply_softmax | bool | If | True |
predict ¶
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 |
predict_uq ¶
Return predictive moments as a :class:deepuq.types.UQResult.
The wrapper reports dropout spread as epistemic_var. No explicit aleatoric component is modeled.
train ¶
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).