IntelLabs / bayesian-torch

A library for Bayesian neural network layers and uncertainty estimation in Deep Learning extending the core of PyTorch
BSD 3-Clause "New" or "Revised" License
544 stars 73 forks source link

Variance/standard deviation #15

Closed jeiglsperger closed 2 years ago

jeiglsperger commented 2 years ago

Is it possible to let the net give back a variance or standard deviation with the prediction (when using the bayesian network for regression) to calculate a confidence interval? Or in other words, is it possible to calculate a let's say 95% confidence interval with the entropy?

ranganathkrishnan commented 2 years ago

@Zepp3 you can calculate the variance or standard deviation with multiple Monte Carlo samples (forward-passes) from the Bayesian neural network model for regression (example snipped below):

predictions_mc = []
for _ in range(args.num_monte_carlo):
    output = model.forward(data)
    predictions_mc.append(output)
predictions_ = torch.stack(predictions_mc)
predictive_mean = torch.mean(predictions_, dim=0)
predictive_variance = torch.var(predictions_, dim=0)