我們引入了具有變化推斷的貝葉斯卷積神經網絡,這是卷積神經網絡(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}
}
}