我们引入了具有变化推断的贝叶斯卷积神经网络,这是卷积神经网络(CNN)的变体,其中贝叶斯由Backes推断出棘手的后验概率分布。我们演示了我们提出的变分推理方法如何实现相当于在几个数据集(MNIST,CIFAR10,CIFAR100)上相同体系结构中频繁推断的性能。


该存储库包含两种类型的贝叶斯Lauer实施:
BBB(Backprop的贝叶斯):
基于本文。该层单独采样所有权重,然后将它们与输入结合在一起,以计算激活中的样品。
bbb_lrt(贝斯(BAYES)由backprop w/ local reparametrization技巧):
该层将Backprop的贝叶斯与本文中的局部再现化技巧结合在一起。此技巧使得可以直接从激活中的分布中进行采样。
要建立torch.nn.Linear自定义的贝叶斯网络,请继承layers.misc.ModuleWrapper代替torch.nn.Module ,并使用任何给BBB_LRT层( BBB或BBBConv2d )而不是BBBLinear and torch.nn.Conv2d 。此外,无需定义forward方法。 ModuleWrapper会自动照顾它。
例如:
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上面的网络可以转换为贝叶斯人,如下所示:
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 )BBBLinear块之前添加FlattenLayer 。forward方法将返回元组为(logits, kl) 。priors可以将其作为论点传递给层。默认值是: 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_
}当前,支持遵循数据集和模型。
python main_bayesian.py
config_bayesian.py中设置超参数python main_frequentist.py
config_frequentist.py中设置超参数 layers/ :包含ModuleWrapper , FlattenLayer , BBBLinear和BBBConv2d 。
models/BayesianModels/ :包含标准的贝叶斯型号(BBBLENET,BBBALEXNET,BBB3CONV3FC)。
models/NonBayesianModels/ :包含标准的非乘式模型(Lenet,Alexnet)。
checkpoints/ :检查点目录:模型将在此处保存。
tests/ :层和模型的基本联合案例。
main_bayesian.py :火车和评估贝叶斯模型。
config_bayesian.py main_bayesian的超参数。
main_frequentist.py :训练和评估非乘式(频繁)模型。
config_frequentist.py main_frequentist的超参数。
有两种类型的不确定性:核心和认识论。
剧烈的不确定性是对数据变化的衡量标准,而认知不确定性是由模型引起的。
在这里,在uncertainty_estimation.py中提供了两种方法,这些方法是'softmax' & 'normalized' ,分别基于本文的等式4和本文等式15。
同样, uncertainty_estimation.py可以用来比较MNIST和notMNIST数据集上的贝叶斯神经网络的不确定性。您可以提供类似的参数:
net_type : lenet , alexnet或3conv3fc 。默认值是lenet 。weights_path :给定net_type权重。默认值为'checkpoints/MNIST/bayesian/model_lenet.pt' 。not_mnist_dir : notMNIST数据集的目录。默认值为'data' 。num_batches :需要计算不确定性的批次数。注意:
layer_type和activation_type在uncertainty_etimation.py中使用的py需要从config_bayesian.py设置,以便与提供的权重匹配。如果您正在使用这项工作,请引用:
@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}
}
}