Package: torchbnn
Bayesian-Neural-Network-PytorchThis is a lightweight repository of bayesian neural network for Pytorch.
There are bayesian versions of pytorch layers and some utils.
To help construct bayesian neural network intuitively, all codes are modified based on the original pytorch codes.Here is adocumentationfor this package.UsageDependenciestorch 1.2.0python 3.6Installationpip install torchbnnorgit clone https://github.com/Harry24k/bayesian-neural-network-pytorchimporttorchbnnDemosBayesian Neural Network with Iris Data(code):
To classify Iris data, in this demo, two-layer bayesian neural network is constructed and tested with plots. It shows how bayesian-neural-network works and randomness of the model.Convert to Bayesian Neural Network(code):
To convert a basic neural network to a bayesian neural network, this demo shows how 'nonbayes_to_bayes' and 'bayes_to_nonbayes' work.Freeze Bayesian Neural Network(code):
To freeze a bayesian neural network, which means force a bayesian neural network to output same result for same input, this demo shows the effect of 'freeze' and 'unfreeze'.Thanks to@kumar-shridhargithub:PyTorch-BayesianCNN@xuanqing94github:BayesianDefenseUpdate RecordsVersion 0.1modules: BayesLinear, BayesConv2d, BayesBatchNorm2d are added.utils: convert_model(nonbayes_to_bayes, bayes_to_nonbayes) is added.functional.py: bayesian_kl_loss is added.Version 0.2prior_sigmais used when initialize modules and functions instead ofprior_log_sigma.modulesare re-defined with prior_sigma instead of prior_log_sigma.utils/convert_model.pyis also changed with prior_sigma instead of prior_log_sigma.modules: Base initialization method is changed to the method of Adv-BNN from the original torch method.functional.py:bayesian_kl_lossis changed similar to ones intorch.functional.modules/loss.py:BKLLossis added based on bayesian_kl_loss similar to ones intorch.loss.Version 0.3functional.py:'bayesian_kl_loss' returns tensor.Tensor([0]) as default: In the previous version, bayesian_kl_loss returns 0 of int type if there is no Bayesian layers. However, considering all torch loss returns tensor and .item() is used to make them to int type, they are changed to return tensor.Tensor([0]) if there is no Bayesian layers.Version 0.4functional.py:'bayesian_kl_loss' is modified: In some cases, the device(cuda/cpu) error has occurred. Thus, losses are initialized with tensor.Tensor([0]) on the device on which the model is.Version 0.5utils/convert_model.py:'nonbayes_to_bayes', 'bayes_to_nonbayes' methods are modified: Before this version, they always replace the original model. From now on, we can handle it with the 'inplace' argument. Set 'inplace=True' for replace the input model and 'inplace=False' for getting a new model. 'inplace=True' is recommended cause it shortens memories and there is no future problems with deepcopy.Version 0.6utils/freeze_model.py:'freeze', 'unfreeze' methods are added: Bayesian modules always returns different outputs even if inputs are same. It is because of their randomized forward propagation. Sometimes, however, we need to freeze this randomized process for analyzing the model deeply. Then you can use this freeze method for changing the bayesian model into non-bayesian model with same parameters.modules: For supportingfreezemethod, freeze, weight_eps and bias_eps is added to each modules. If freeze is False (Defalt), weight_eps and bias_eps will be initialized with normal noise at every forward. If freeze is True, weight_eps and bias_eps won't be changed.Version 0.7DO NOT USEVersion 0.8modules: To supportfreezemethod, weight_eps and bias_eps is changed to buffer with register_buffer method. Thorugh this change, it provides save and load even if bayesian neural network is freezed.BayesModule is added: Bayesian version of torch.nn.Module. Not being used currently.utils/freeze_model.py:'freeze', 'unfreeze' methods are modified: Previous methods didn't work on single layer network.Demos are uploaded: "Bayesian Neural Network with Iris Data".Version 0.9modules:Variable 'freeze' is deleted: The status, which indicates whether this bayesian module is freezed, is deleted. Instead of 'freeze', we can determine by checking 'eps' is set to None. For example, if 'weight_eps' is None, the BayesLinear module is freezed now. The reason of this update is to solve backpropagation error occured by inplacing eps.Method 'freeze' and 'unfreeze' are added: These methods will change 'eps' to None or random normal values.utils/freeze_model.py:freeze, unfreeze methods are modified: These methods in utils are changed due to the above.Demos are uploaded: "Convert to Bayesian Neural Network", "Freeze Bayesian Neural Network".Version 1.0modules: BayesLinear, BayesConv2d are modified.BayesLinear: Bias will set to False if the bias in args is None or Flase. Otherwise, it set to True.BayesConv2d: Bias will set to False if the bias in args is None or Flase. Otherwise, it set to True. In addition, re-defined with prior_sigma instead of prior_log_sigma.utils/convert_model.py:Depreciated. Please refer themodified demo.Version 1.1Pip Package Re-uploadedVersion 1.2Bug fixed