Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC004 · Algorithm
Monte Carlo dropout for approximate Bayesian prediction
Retain stochastic dropout at prediction time and integrate multiple forward passes. This entry concerns approximate posterior prediction, whereas the existing Dropout entry concerns regularization during training.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- Deep neural network (torch MLP) Verified in Python and R
Do it in CodeSplash Run it on your own data, in Python or R.
- Availability
- Runs in CodeSplash
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through learn.torch_mlp. The reference's example ran here in one language.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Retain stochastic dropout at prediction time and integrate multiple forward passes. This entry concerns approximate posterior prediction, whereas the existing Dropout entry concerns regularization during training. Reusing the same operation for a different inferential role does not make the uncertainty guarantee automatic.
Question / hypotheses
What predictive distribution results from the fitted dropout variational approximation, and how well does its dispersion track errors? Test that relationship with held-out proper scores and coverage rather than assuming all mask variability is calibrated epistemic uncertainty.
Architecture / computational structure
A dropout-trained network defines a distribution of masked subnetworks. Draw T masks and aggregate their predictions while keeping non-dropout state, especially batch-normalization running statistics, fixed. For heteroscedastic regression, combine predicted observation variances with variability of sampled means.
Assumptions
Training and inference must use compatible dropout placement and scaling. The Bayesian interpretation depends on the prior/regularization approximation; arbitrary dropout added after training does not establish it. Correlated samples or too few passes make Monte Carlo error substantial.
Formula / computational definition
with dropout-induced . For sampled means , and in a homoscedastic likelihood convention. Omit only when reporting latent-function dispersion.
Parameters and notation
Learned: retained network weights and any observation-noise head. Hyperparameters: dropout probability and placement, weight decay/prior convention, sample count T, stochastic seed, observation precision and calibration policy. T controls integration accuracy, not training capacity; estimate its convergence separately from model fit.
Fitting, fine-tuning, and adaptation
Fine-tune using the same intended stochastic architecture, learning rates and target likelihood. Merely toggling a deterministic pretrained network into training mode can change unrelated layers. If the dropout rate changes, refit or explicitly validate the changed approximation; recalibrate target-domain outputs.
How to use and calibrate
Evaluate probability-averaged predictions; average logits only when a specifically justified approximation calls for it. Plot uncertainty versus errors and an integration-convergence curve over T. Check repeated predictions to ensure masks change and batch-normalization state does not.
Example application
Produce an uncertainty band for a neural sensor-calibration curve when repeated stochastic passes are affordable. Separate observation noise from mask-induced variation in the plotted band.
Interpretation / pitfalls
Watch out
Dropout can underestimate uncertainty under misspecification or shift. Report the dispersion convention and Monte Carlo sample budget. Dropout regularization, posterior uncertainty, and conformal coverage are three different concepts.
Python and R code examples
Scope: Monte Carlo latent-mean and predictive-variance component for a fixed one-hidden-layer readout. It uses inverted dropout and a stated observation variance; no network is fitted.
Dependencies / validation: use the shared NumPy/SciPy or base-R setup above. Python executed successfully with assertions; R reviewed but not executed.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Monte Carlo latent-mean and predictive-variance component for a fixed one-hidden-layer readout. It uses inverted dropout and a stated observation variance; no network is fitted.
Shared setup for this chapter
import numpy as np
from scipy.special import logsumexp, gammaln
from scipy.linalg import expm
from itertools import product
def rbf(a, b, ell=1.0):
a, b = np.atleast_2d(a), np.atleast_2d(b)
return np.exp(-np.sum((a[:, None, :] - b[None, :, :])**2, axis=2)/(2*ell**2))
rng = np.random.default_rng(7)
hidden = np.array([.3,.8,-.2,.5]); w = np.array([1.,-.4,.6,.2])
p = .25; T = 4000
mask = (rng.random((T,4)) >= p)/(1-p)
f = (mask*hidden) @ w
latent = np.mean((f-f.mean())**2); observation = .05
exact_mean = float(hidden @ w)
assert abs(f.mean()-exact_mean)<.03
print({'mc_mean':float(f.mean()),'latent_variance':float(latent),'predictive_variance':float(latent+observation)})
Component demo, not a trained model — Monte Carlo latent-mean and predictive-variance component for a fixed one-hidden-layer readout. It uses inverted dropout and a stated observation variance; no network is fitted.
set.seed(7)
hidden <- c(.3,.8,-.2,.5); w <- c(1,-.4,.6,.2)
p <- .25; T <- 4000
mask <- matrix(as.numeric(runif(T*4)>=p)/(1-p),T,4)
f <- as.vector(sweep(mask,2,hidden,"*")%*%w)
latent <- mean((f-mean(f))^2); observation <- .05
stopifnot(abs(mean(f)-sum(hidden*w))<.03)
print(c(mc_mean=mean(f),latent_variance=latent,predictive_variance=latent+observation))
Sources
- Gal–Ghahramani Bayesian dropout paperproceedings.mlr.press
From the CodeSplash reference, entry UNC004.