All Projects → kumar-shridhar → Pytorch Bayesiancnn

kumar-shridhar / Pytorch Bayesiancnn

Licence: mit
Bayesian Convolutional Neural Network with Variational Inference based on Bayes by Backprop in PyTorch.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pytorch Bayesiancnn

Rethinking Tensorflow Probability
Statistical Rethinking (2nd Ed) with Tensorflow Probability
Stars: ✭ 152 (-80.49%)
Mutual labels:  bayesian-inference, variational-inference, bayesian-statistics
Bcpd
Bayesian Coherent Point Drift (BCPD/BCPD++); Source Code Available
Stars: ✭ 116 (-85.11%)
Mutual labels:  bayesian-inference, variational-inference, bayesian-statistics
Decision Analysis Course
🎓 Uni-Bonn Decision Analysis graduate course, lectures and materials
Stars: ✭ 17 (-97.82%)
Mutual labels:  bayesian-inference, bayesian-statistics
noisy-K-FAC
Natural Gradient, Variational Inference
Stars: ✭ 29 (-96.28%)
Mutual labels:  bayesian-inference, variational-inference
Rstanarm
rstanarm R package for Bayesian applied regression modeling
Stars: ✭ 285 (-63.41%)
Mutual labels:  bayesian-inference, bayesian-statistics
PyLDA
A Latent Dirichlet Allocation implementation in Python.
Stars: ✭ 51 (-93.45%)
Mutual labels:  bayesian-inference, variational-inference
ReactiveMP.jl
Julia package for automatic Bayesian inference on a factor graph with reactive message passing
Stars: ✭ 58 (-92.55%)
Mutual labels:  bayesian-inference, variational-inference
viabel
Efficient, lightweight variational inference and approximation bounds
Stars: ✭ 27 (-96.53%)
Mutual labels:  bayesian-inference, variational-inference
blangSDK
Blang's software development kit
Stars: ✭ 21 (-97.3%)
Mutual labels:  bayesian-inference, bayesian-statistics
Bayesian Analysis Recipes
A collection of Bayesian data analysis recipes using PyMC3
Stars: ✭ 479 (-38.51%)
Mutual labels:  bayesian-inference, bayesian-statistics
Ios 10 Sampler
Code examples for new APIs of iOS 10.
Stars: ✭ 3,341 (+328.88%)
Mutual labels:  convolutional-neural-networks, image-recognition
Bayesian Stats Modelling Tutorial
How to do Bayesian statistical modelling using numpy and PyMC3
Stars: ✭ 480 (-38.38%)
Mutual labels:  bayesian-inference, bayesian-statistics
DUN
Code for "Depth Uncertainty in Neural Networks" (https://arxiv.org/abs/2006.08437)
Stars: ✭ 65 (-91.66%)
Mutual labels:  bayesian-inference, variational-inference
Bayesian Workshop
Material for a Bayesian statistics workshop
Stars: ✭ 34 (-95.64%)
Mutual labels:  bayesian-inference, bayesian-statistics
artificial neural networks
A collection of Methods and Models for various architectures of Artificial Neural Networks
Stars: ✭ 40 (-94.87%)
Mutual labels:  bayesian-inference, variational-inference
Landmark Detection Robot Tracking SLAM-
Simultaneous Localization and Mapping(SLAM) also gives you a way to track the location of a robot in the world in real-time and identify the locations of landmarks such as buildings, trees, rocks, and other world features.
Stars: ✭ 14 (-98.2%)
Mutual labels:  bayesian-inference, bayesian-statistics
models
Forecasting 🇫🇷 elections with Bayesian statistics 🥳
Stars: ✭ 24 (-96.92%)
Mutual labels:  bayesian-inference, bayesian-statistics
Pymc3
Probabilistic Programming in Python: Bayesian Modeling and Probabilistic Machine Learning with Aesara
Stars: ✭ 6,214 (+697.69%)
Mutual labels:  bayesian-inference, variational-inference
TransformVariables.jl
Transformations to contrained variables from ℝⁿ.
Stars: ✭ 52 (-93.32%)
Mutual labels:  bayesian-inference, bayesian-statistics
geostan
Bayesian spatial analysis
Stars: ✭ 40 (-94.87%)
Mutual labels:  bayesian-inference, bayesian-statistics

Python 3.7+ Pytorch 1.3 License: MIT arxiv

We introduce Bayesian convolutional neural networks with variational inference, a variant of convolutional neural networks (CNNs), in which the intractable posterior probability distributions over weights are inferred by Bayes by Backprop. We demonstrate how our proposed variational inference method achieves performances equivalent to frequentist inference in identical architectures on several datasets (MNIST, CIFAR10, CIFAR100) as described in the paper.


Filter weight distributions in a Bayesian Vs Frequentist approach

Distribution over weights in a CNN's filter.


Fully Bayesian perspective of an entire CNN

Distributions must be over weights in convolutional layers and weights in fully-connected layers.


Layer types

This repository contains two types of bayesian lauer implementation:

  • BBB (Bayes by Backprop):
    Based on this paper. This layer samples all the weights individually and then combines them with the inputs to compute a sample from the activations.

  • BBB_LRT (Bayes by Backprop w/ Local Reparametrization Trick):
    This layer combines Bayes by Backprop with local reparametrization trick from this paper. This trick makes it possible to directly sample from the distribution over activations.


Make your custom Bayesian Network?

To make a custom Bayesian Network, inherit layers.misc.ModuleWrapper instead of torch.nn.Module and use BBBLinear and BBBConv2d from any of the given layers (BBB or BBB_LRT) instead of torch.nn.Linear and torch.nn.Conv2d. Moreover, no need to define forward method. It'll automatically be taken care of by ModuleWrapper.

For example:

class Net(nn.Module):

  def __init__(self):
    super().__init__()
    self.conv = nn.Conv2d(3, 16, 5, strides=2)
    self.bn = nn.BatchNorm2d(16)
    self.relu = nn.ReLU()
    self.fc = nn.Linear(800, 10)

  def forward(self, x):
    x = self.conv(x)
    x = self.bn(x)
    x = self.relu(x)
    x = x.view(-1, 800)
    x = self.fc(x)
    return x

Above Network can be converted to Bayesian as follows:

class Net(ModuleWrapper):

  def __init__(self):
    super().__init__()
    self.conv = BBBConv2d(3, 16, 5, strides=2)
    self.bn = nn.BatchNorm2d(16)
    self.relu = nn.ReLU()
    self.flatten = FlattenLayer(800)
    self.fc = BBBLinear(800, 10)

Notes:

  1. Add FlattenLayer before first BBBLinear block.
  2. forward method of the model will return a tuple as (logits, kl).
  3. priors can be passed as an argument to the layers. Default value is:
priors={
    'prior_mu': 0,
    'prior_sigma': 0.1,
    'posterior_mu_initial': (0, 0.1),  # (mean, std) normal_
    'posterior_rho_initial': (-3, 0.1),  # (mean, std) normal_
}

How to perform standard experiments?

Currently, following datasets and models are supported.

  • Datasets: MNIST, CIFAR10, CIFAR100
  • Models: AlexNet, LeNet, 3Conv3FC

Bayesian

python main_bayesian.py

  • set hyperparameters in config_bayesian.py

Frequentist

python main_frequentist.py

  • set hyperparameters in config_frequentist.py

Directory Structure:

layers/: Contains ModuleWrapper, FlattenLayer, BBBLinear and BBBConv2d.
models/BayesianModels/: Contains standard Bayesian models (BBBLeNet, BBBAlexNet, BBB3Conv3FC).
models/NonBayesianModels/: Contains standard Non-Bayesian models (LeNet, AlexNet).
checkpoints/: Checkpoint directory: Models will be saved here.
tests/: Basic unittest cases for layers and models.
main_bayesian.py: Train and Evaluate Bayesian models.
config_bayesian.py: Hyperparameters for main_bayesian file.
main_frequentist.py: Train and Evaluate non-Bayesian (Frequentist) models.
config_frequentist.py: Hyperparameters for main_frequentist file.


Uncertainty Estimation:

There are two types of uncertainties: Aleatoric and Epistemic.
Aleatoric uncertainty is a measure for the variation of data and Epistemic uncertainty is caused by the model.
Here, two methods are provided in uncertainty_estimation.py, those are 'softmax' & 'normalized' and are respectively based on equation 4 from this paper and equation 15 from this paper.
Also, uncertainty_estimation.py can be used to compare uncertainties by a Bayesian Neural Network on MNIST and notMNIST dataset. You can provide arguments like:

  1. net_type: lenet, alexnet or 3conv3fc. Default is lenet.
  2. weights_path: Weights for the given net_type. Default is 'checkpoints/MNIST/bayesian/model_lenet.pt'.
  3. not_mnist_dir: Directory of notMNIST dataset. Default is 'data\'.
  4. num_batches: Number of batches for which uncertainties need to be calculated.

Notes:

  1. You need to download the notMNIST dataset from here.
  2. Parameters layer_type and activation_type used in uncertainty_etimation.py needs to be set from config_bayesian.py in order to match with provided weights.

If you are using this work, please cite:

@article{shridhar2019comprehensive,
  title={A comprehensive guide to bayesian convolutional neural network with variational inference},
  author={Shridhar, Kumar and Laumann, Felix and Liwicki, Marcus},
  journal={arXiv preprint arXiv:1901.02731},
  year={2019}
}
@article{shridhar2018uncertainty,
  title={Uncertainty estimations by softplus normalization in bayesian convolutional neural networks with variational inference},
  author={Shridhar, Kumar and Laumann, Felix and Liwicki, Marcus},
  journal={arXiv preprint arXiv:1806.05978},
  year={2018}
}
}

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].