Giter Site home page Giter Site logo

bnn-predictions's Introduction

Improving predictions of Bayesian neural nets via local linearization

Accompanying code for the paper

Immer, A.*, Korzepa, M., Bauer, M.*, Improving predictions of Bayesian neural nets via local linearization, AISTATS 2021.

Predictions with Laplace-GGN

We simply replace the BNN predictive with a GLM predictive for the Laplace-GGN posterior approximation. The code provides means to compute the Laplace-GGN posterior (diagonal, KFAC, full) and use it to make predictions. In the following example, we construct the Laplace approximation from a trained model model. The 'kron' cov-type corresponds to KFAC. The inferred posterior enables sampling from the posterior predictive for a batch of input data X using posterior.predictive_samples_glm(X, n_samples=1000).

from preds.likelihoods import CategoricalLh
from preds.laplace import Laplace

# infer posterior with Laplace-GGN
lh = CategoricalLh()  # likelihood 
prior_precision = 1.  # prior
posterior = Laplace(model, prior_precision, lh)
posterior.infer(train_loader, cov_type='kron', dampen_kron=False)  # or 'full', 'diag'

# GLM predictions
glm_samples = posterior.predictive_samples_glm(X, n_samples=1000)
# BNN predictions
bnn_samples = posterior.predictive_samples_bnn(X, n_samples=1000)

For a running and worked example, see the two examples on regression and classification:

The two examples train a neural network until convergence and construct variants of the Laplace-GGN posterior approximation. The script plots the posterior predictive of the proposed GLM in comparison to the heavily underfitting BNN predictive. The underfitting can only be resolved by artificially reducing the posterior variance; using a different prior does not help as it fails across the entire range of values.

Regression example

The resulting plot compares the proposed GLM to the BNN predictive:

image

Classification example

In the following example, the BNN predictive underfits severely so that the contours are almost invisible:

image

Setup

We use python >=3.7. To install the dependencies, run pip install -r requirements.txt. To use a GPU, additional installations might be necessary (CUDA, etc.). Then, install the cml package with pip install .. Create the result directories mkdir run_results and mkdir runs.

# install requirements
pip install -r requirements.txt

# install the `preds` package
pip install .

# run tests (optional)
pip install pytest
pytest tests

bnn-predictions's People

Contributors

aleximmer avatar maciejkorzepa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bnn-predictions's Issues

Error when compiling classification.py

Hi, I am trying to reproduce the paper results, however I got this error:

Traceback (most recent call last):
File "/Users/vince/BNN-predictions-main/experiments/classification.py", line 236, in
train=True, double=double)
File "/Users/vince/BNN-predictions-main/preds/datasets.py", line 167, in init
'waveform', 'satellite', 'digits', 'banana'], error_msg
AssertionError: invalid UCI classification dataset

If you have some thought how to fix it I will appreciate.
Thank you again!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.