Prediction uncertainty calculation (Bayesian method) in GNN model

Viewed 16

I want to get the uncertainty interval for the prediction that the model made such as 4 +- 1 or 10 +=3. Currently I am looking at PyG to do this.

I found information pointing out Bayesian method and MC dropout but I couldn't find an example code or explanation for beginners. I am not sure how and where I can insert the layer to the model.

I thought I can insert some layers into my model to calculate it. Or, am I supposed to use only Bayesian model for the prediction and as well as uncertainty calculation?

The example code will help me the most but I appreciate any information.

1 Answers

One way to do so is to compute the mean mu and the uncertainty sigma of the prediction (in a new dedicated branch) and then sample a prediction according to a normal distribution of mean mu and std exp(sigma) (predicting the log of the std is simpler than predicting the variance directly). To do so, you need a reparametrization trick to avoid blocking the gradient (article here in the framework of variational auto-encoder). It is actually simple in the general case:

mu, log_sigma = model(inputs)
random = torch.randn(mu.size())
pred = mu + random * torch.exp(log_sigma)
loss_val = loss_func(pred, label)

You can also adapt the parametrization trick for uniform distribution by replacing torch.randn by torch.rand but it is rare.

Then log_sigma is a direct measure of the uncertainty.

Note that it is very simple to implement because the model actually learns the uncertainty of its prediction but it decreases the stability of the model (make sure that log_sigma is never too big) and it could not be as accurate as Bayesian methods.

Related