MCDropout¶
Defined in fynance.models.uncertainty
- class MCDropout(model, n_samples=50, seed=0)[source]
Bases:
objectEpistemic-uncertainty proxy via Monte Carlo Dropout at inference.
Wraps one dropout-bearing
torch.nn.Module(typically aBaseNeuralNetsubclass such asMultiLayerPerceptronbuilt withdrop > 0) and approximates a Bayesian posterior by keeping dropout active at inference and averagingn_samplesstochastic forward passes (Gal & Ghahramani, 2016, “Dropout as a Bayesian Approximation”). The spread across those passes —predict_std— is the epistemic-uncertainty proxy.- Parameters:
- modeltorch.nn.Module
A
SignalModel-conforming net (fit(X, y)/predict(X)) with at least onetorch.nn.Dropout*submodule. A model with no dropout submodule still works but is pointless (every one of then_samplespasses is then identical, sopredict_stdis~0): aUserWarningis raised at construction in that case.- n_samplesint
Number of stochastic forward passes averaged by
predict/predict_std. Default 50.- seedint
Seed (
torch.manual_seed) applied before drawing then_samplesdropout masks, so both methods are reproducible for a given instance.
See also
fynance.models.uncertainty.DeepEnsemble
Notes
Increasing
n_samplesreduces the variance of the mean estimate returned bypredict(the mean ofni.i.d. stochastic passes has variance \(\sigma^2 / n\) by the law of large numbers) but does not shrinkpredict_stditself, which converges to the model’s intrinsic dropout-induced spread rather than to zero.Examples
>>> import numpy as np >>> import torch >>> from fynance.models.mlp import MultiLayerPerceptron >>> from fynance.models.uncertainty import MCDropout >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((40, 2)).astype(np.float32) >>> y = np.sin(X[:, :1]).astype(np.float32) >>> net = MultiLayerPerceptron(2, 1, layers=[8], activation=torch.nn.Tanh, ... drop=0.2) >>> _ = net.set_optimizer(torch.nn.MSELoss, torch.optim.Adam, lr=1e-2) >>> mc = MCDropout(net, n_samples=10, seed=0).fit(X, y) >>> mc.predict(X).shape (40,) >>> bool((mc.predict_std(X) >= 0).all()) True
- fit(X, y)[source]
Fit the wrapped model normally (dropout trains as usual).
- Parameters:
- X, yarray-like
Training data forwarded to the wrapped model’s
fit(X, y).
- Returns:
- MCDropout
self.
- predict(X)[source]
Mean over
n_samplesstochastic forward passes, shape(T,).
- predict_std(X)[source]
Std over
n_samplesstochastic forward passes — epistemic proxy.