# Variational Inference to Measure Model Uncertainty in Deep Neural Networks

We present a novel approach for training deep neural networks in a Bayesian way. Classical, i.e. non-Bayesian, deep learning has two major drawbacks both originating from the fact that network parameters are considered to be deterministic. First, model uncertainty cannot be measured thus limiting the use of deep learning in many fields of application and second, training of deep neural networks is often hampered by overfitting. The proposed approach uses variational inference to approximate the intractable a posteriori distribution on basis of a normal prior. The variational density is designed in such a way that the a posteriori uncertainty of the network parameters is represented per network layer and depending on the estimated parameter expectation values. This way, only a few additional parameters need to be optimized compared to a non-Bayesian network. We apply this Bayesian approach to train and test the LeNet architecture on the MNIST dataset. Compared to classical deep learning, the test error is reduced by 15 information about the parameter uncertainty in each layer. We show that this information can be used to calculate credible intervals for the prediction and to optimize the network architecture for a given training data set.

## Authors

• 4 publications
• 1 publication
• 4 publications
• ### Correlated Parameters to Accurately Measure Uncertainty in Deep Neural Networks

In this article a novel approach for training deep neural networks using...
04/02/2019 ∙ by Konstantin Posch, et al. ∙ 0

• ### Interpreting a Penalty as the Influence of a Bayesian Prior

In machine learning, it is common to optimize the parameters of a probab...
02/01/2020 ∙ by Pierre Wolinski, et al. ∙ 0

• ### Uncertainty aware multimodal activity recognition with Bayesian inference

Deep neural networks (DNNs) provide state-of-the-art results for a multi...
11/27/2018 ∙ by Mahesh Subedar, et al. ∙ 0

• ### Towards Explainable Deep Neural Networks (xDNN)

In this paper, we propose an elegant solution that is directly addressin...
12/05/2019 ∙ by Plamen Angelov, et al. ∙ 128

• ### Measurement error models: from nonparametric methods to deep neural networks

The success of deep learning has inspired recent interests in applying n...
07/15/2020 ∙ by Zhirui Hu, et al. ∙ 0

• ### Practical Deep Learning with Bayesian Principles

Bayesian methods promise to fix many shortcomings of deep learning, but ...
06/06/2019 ∙ by Kazuki Osawa, et al. ∙ 23

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Introduction

Deep learning has led to series of breakthroughs in many fields of applied machine learning, especially in image classification

(Krizhevsky et al, 2012)(Bengio et al, 2006). In 1989, the universal approximation theorem was proven, which can be summarized that a feed-forward network with one hidden layer can approximate a broad class of functions abitrarily well (Hornik et al, 1990). Currently, it has been shown that for a given bound of the approximation error, deep networks require exponentially less data than shallow ones (Liang and Srikant, 2017). The possible applications of deep neural networks for classification and detection cover a wide range including medical imaging, psychology, automotive, industry, finance and life sciences (Gulshan et al, 2016; Greenspan et al, 2016; Li et al, 2018; Banerjee et al, 2017; Jozwik et al, 2017; Heaton et al, 2017).

Despite its potential and superior accuracy for classification tasks compared to other techniques, dissemination of deep learning into real world applications and services has been limited by a lack of information about model uncertainty. This particularly affects those applications, where wrong decisions based on false classification results could have significant negative or even catastrophic impact such as in self-driving cars, finance or medical applications (Rowan, 2017). In addition, most network architectures today are designed based on trial and error or based on abstract, high level considerations (Goodfellow et al, 2016; Arora et al, 2014)

. Thus, the process of finding an optimal network architecture for the classification task at hand and given the training data can be cumbersome. Standard deep networks for classification and regression do not represent model uncertainty since network parameters are considered to be deterministic values. Sometimes in classification the probabilities obtained when running the model are falsely interpreted as the confidence of the model, see

(Gal and Ghahramani, 2016c). Indeed a network can guess randomly while returning a high class probability. Often it is essential to know how sure a network is about a special prediction and not only that it predicts on average quite well.

Besides the inability of classical deep nets to represent model uncertainty they are prone to overfitting. Modern deep models cover a huge amount of parameters and therefore require a huge amount of labeled training data as well. In many applications, such an amount cannot be provided because of financial or time constraints. To overcome this problem, the deep learning community introduced several probabilistic regularization techniques, such as dropout and dropconnect (Srivastava et al, 2014; Wan et al, 2013). Gal and Ghahramani (2016a) could show that an appropriate application of dropout can be interpreted as training networks in a Bayesian way, by approximating the a posteriori distribution via variational inference.

Both major drawbacks of deep learning, the absence of model uncertainty evaluations and the need of a large amount of training data, are well addressed by using Bayesian statistics. On the one hand Bayesian models are robust to overfitting since parameters are not forced to be fixed and on the other hand the uncertainty in the network parameters can directly be translated in uncertainty information for network predictions. Further, Bayesian deep models can help to finally understand why deep learning works. Combining the profound theoretical literature about Bayesian statistics and deep learning will lead to a better understanding and broader acceptance of the technique.

In this study, a new approach for Bayesian deep neural networks based on variational inference is proposed. It is the first approach which treats network layers as units in order to express model uncertainty. Therefore only two uncertainty parameters are introduced per layer which implies that the variational distribution requires only few additional parameters that need to be optimized compared to a non-Bayesian net. Besides good convergence properties, this way of proceeding also provides consolidated information about network uncertainty that can be readily used to optimize network architecture. Introducing too many uncertainty parameters, i.e. one for each network parameter, worsens the convergence properties and results in an excess of information about network uncertainty which is difficult to interpret.

## 2 Theoretical Background and Related Work

In this section, a short introduction to Bayesian statistics and variational inference is presented. More details can be found in (Hinton and van Camp, 1993; Jordan et al, 1999; Blei et al, 2016; Bishop, 2006; Gal and Ghahramani, 2016c, b). Further, it is summarized how variational inference was applied in the past to train deep neural networks in a Bayesian way, which also sheds some light on the limitations of each approach.

### 2.1 Bayesian and Variational Inference

The theoretical considerations are based on classification tasks in this study. For regression, the theory is quite the same and can be found in the literature recommended above. In Bayesian statistics, network parameters are considered as one large random vector

. A priori knowledge regarding is expressed in terms of the a priori distribution . One is interested in updating the knowledge about after observing data , where denotes a set of training examples and holds the corresponding class labels. Therefore, the a posteriori distribution

has to be calculated. According to the Bayes’ theorem, the corresponding density is

 p(w––|y–,X)=p(y–|w––,X)p(w––)∫p(y–|w––,X)p(w––) dw––

The joint density is given by the product of the neural network outputs for all the training examples, following the classical assumptions on stochastic independence and modelling in deep learning. Thus, the only problem in computing is the generally intractable integral in the denominator. Variational inference aims at approximating the posterior by optimizing a parametric distribution , such that it is most similar to .

Once the variational distribution is optimized, it can be used for predicting new data and further quantifying uncertainty in predictions. The posterior predictive distribution

reflects the belief in a class label for a given example after observing data :

 p(y∗|x––∗,y–,X) =∫p(y∗,w––|x––∗,y–,X) dw–– =∫p(y∗|w––,x––∗,y–,X)p(w––|y–,X) dw–– =∫p(y∗|w––,x––∗)p(w––|y–,X) dw––

Replacing the posterior with the variational distribution and further approximating the intractable integral via Monte Carlo integration results in

 p(y∗|x––∗,y–,X)≈1NN∑i=1f––(x––∗;ˆw––i)y∗   with ˆw––i∼i.i.d.qϕ––(w––)

where denotes the neural network used. Thus, predictions are made by propagating the object of interest multiple times through the network, averaging the resulting probability vectors and choosing the index of the largest element in the resulting mean:

 ˆy∗=arg maxj1NN∑i=1f––(x––∗;ˆw––i)j

To measure model uncertainty, credible intervals can be estimated. The output of a neural net with fixed parameters equals the probability . Further, the variational distribution reflects the knowledge about the random network parameters . The uncertainty in the parameters implies an uncertainty in the neural network output, i.e. the probability of class . By sampling from the variational distribution and subsequently calculating the empirical and quantiles of the corresponding network outputs an estimate of the credible interval for the probability of is found. Note that neural network outputs are uniquely determined for fixed and therefore sampling from the output distribution is equivalent to sampling from the parameters.

So far it has not been mentioned how the variational distribution is optimized in order to approximate the posterior

. This can be accomplished by minimizeing the Kullback-Leibler divergence

(KL-divergence) between the variational density and the posterior. It is defined as:

 DKL(qϕ––(w––)||p(w––|y–,X)) :=Eqϕ––(w––)(lnqϕ––(w––)p(w––|y–,X)) =∫lnqϕ––(w––)p(w––|y–,X)qϕ––(w––) dw––

The KL-divergence is not really a distance measure since it is asymmetric and the triangle inequality does not hold. Nevertheless, it is often used to measure the distance between two probability distributions, and as long as only two distributions are of interest it does not matter that the triangle inequality is violated. Obviously,

cannot be minimized directly since the a posteriori distribution is unknown. However, minimizing is equivalent to minimizing the negative log evidence lower bound (Bishop, 2006), which is given by:

 LVI =−∫qϕ––(w––)lnp(y–|w––,X) dw––+DKL(qϕ––(w––)||p(w––)) =−Eqϕ––(w––)[lnβ∏i=1p(yi|w––,x––i)]+DKL(qϕ––(w––)||p(w––)) =−β∑i=1[Eqϕ––(w––)(lnp(yi|w––,x––i))]+DKL(qϕ––(w––)||p(w––))

includes the KL-divergence between the variational distribution and the well known prior. The unknown expectation value can be approximated via Monte Carlo integration. Inspired by stochastic gradient descent the integration takes place with just one sample, but a new sample is drawn in each iteration of the optimization procedure used to minimize

. The re-sampling guarantees that a sufficient amount of samples is drawn, whilst using merely one sample saves memory. According to these considerations, the objective function in the -th iteration of the optimization is given by:

 LkVI=−β∑i=1lnf––(–xi;ˆw––k)yi+DKL(qϕ––(w––)||p(w––))

If one wants to use mini-batch gradient descent, the KL-divergence has to be rescaled by the factor , where denotes the number of examples one mini-batch holds. This ensures that the divergence does not get too much weight.

Summing up, training neural networks in a Bayesian way via variational inference is quite similar to frequentist training. The -norm regularization used in classical deep learning is replaced by punishing deviations from the a priori distribution. The same error function as in non-Bayesian deep learning is used with the crucial difference that the network parameters are drawn from the variational distribution during training.

### 2.2 Related Work

There is a large number of possibilities of how to choose the variational distribution. Gal and Ghahramani (2016a)

have shown how classical Bernoulli dropout can be used to define the approximating function. The network biases are assumed to be deterministic for simplicity, whilst the network weights are defined to be random according to dropout. Indeed, randomly dropping a neuron in layer

is equivalent to dropping all weights in layer which represent connections to this one neuron. In order to calculate the KL-divergence to a standard normal prior, network weights are assumed to follow a mixture of two Gaussians. Note that the KL-divergence between a discrete and a continuous distribution would diverge to infinity. Both Gaussians are defined to have a variance that is negligibly small, such that more or less only two values (zero and a variational parameter to be optimized) are taken. Finally, the KL-divergence is given by the -norm of the neural network weights. Therefore, neural nets can be learned in a Bayesian way by merely applying dropout after each weight layer except the last one. Experiments have shown that this approach results in a very good accuracy at the MNIST dataset of handwritten digits (Gal and Ghahramani, 2016a).

Blundell et al (2015)

used a normal distribution with a diagonal covariance matrix as variational distribution. The approach was evaluated with the LeNet architecture

(LeCun et al, 1998) and the MNIST dataset. While the approach was shown to work in principle, a wider application is hampered by the fact that the number of parameters to be optimized is doubled (one variance term for each expectation value) which complicates training and makes it computationally significantly more expensive. Louizos and Welling (2016) introduced a variational distribution that in contrast to the distribution of Blundell et al. does not treat each network parameter independently. In particular, they used a probability distribution on random matrices. Thus, they could reduce the number of variance-related parameters, but to a number which nonetheless is significantly higher than in the frequentist approach. Further approaches with similar restrictions are described in Kingma et al (2015) and Graves (2011).

It should be mentioned that variational Bayes is just a specific case of local -divergence minimization. The -divergence (Shun-Ichi, 1985) between two densities and is defined as

such that converges to the Kullback-Leibler divergence for .
Hernandez-Lobato et al (2015) have shown that the optimal setting for is task specific and that a nonstandard stetting can produce better prediction results. Li and Gal (2017) continued the work of Hernández-Lobato et al.. According to them, variational inference can underestimate model uncertainty and -divergences are able to avoid the underestimation. In particular they propose a simple inference technique based on a re-parametrisation of the -divergence objectives and dropout. However, our work does not focus on finding an optimal choice for . It tries to propose a good and reasonable approximating distribution. The proposed distribution can then be used with any setting of , but this is left for further research. Despite our restriction to , our best model on the MNIST dataset (see section 4.2) shows an accuracy which is slightly better than the accuracy of the best model obtained in Li and Gal (2017).

## 3 Our Work

In this work, we propose a variational distribution with the aim to satisfy the following requirements:

• uncertainty of predictions can be reliably measured

• the number of parameters to be optimized does not differ significantly from the non-Bayesian case

• model uncertainty information is given

So far, there is no variational distribution recommended in previous work which satisfies all the requirements. The approach of Gal and Ghahramani (2016a) satisfies the second requirement since the number of parameters to be optimized is exactly the same as in frequentist deep learning. In addition, uncertainty of predictions (the first requirement) can be measured, but it is questionable how well their distribution can approximate the complex a posteriori distribution and therefore how reliable the uncertainty information is. The third point is clearly not satisfied. The approach of Blundell et al (2015) satisfies the first requirement. The second requirement is violated since the amount of parameters is doubled. This also makes it hard to evaluate the model uncertainty (third requirement). Deep architectures typically hold millions of parameters. Computing individual uncertainties for each parameter makes it difficult to extract useful information for the whole network.

In order to satisfy all three requirements, our approach expresses model uncertainty layer-wise with respect to the parameter expectation values. Therefore, only two uncertainty parameters are introduced per layer which leads to good convergence properties. Interpreting network layers as units in terms of uncertainty enables a quick understanding of the model considered. Strong uncertainty in some layers indicate that the architectural designs of them might not be optimal and should be reconsidered. Thus in addition to allow for the uncertainty of the predictions to be measured, our approach also helps for finetuning network architectures. At first glance, the requirement number one -reliable uncertainty information of predictions- might not be satisfied as well by our approach as by Blundell et al (2015). For sure, the more complex distribution proposed by them should theoretically lead to a better approximation of the posterior and therefore more reliable uncertainty information. However, in practice, optimizing this complex distribution for deep architectures leads to convergence issues (as mentioned before) such that the more simple distribution proposed by us leads to a more reliable approximation.

### 3.1 Methodology

Let denote the random weights of the -th network layer and further let
denote the corresponding random biases. In addition, let and be multivariate standard normal distributed. To set up the variational distribution, the random weights and the random biases are defined by

 W–––i :=m––i⊙(1–Ki+τi–Ei) (1) τi :=ln(1+exp(δi)) (2) B––i :=m––bi⊙(1–ki+τbiE––bi) (3) τbi :=ln(1+exp(δbi)) (4)

where , , and are variational parameters and denotes the Hadamard product, i.e. element-wise multiplication. This implies that and are multivariate normal distributed according to

 W–––i ∼N(m––i,τ2idiag(m––i)2) (5) B––i ∼N(m––bi,τ2bidiag(m––bi)2). (6)

The reason why weights and biases are not directly defined by the Gaussians given in Eqs. 5 and 6, can be found in the network optimization. During optimization in each iteration, a sample is drawn from the random network parameters in order to adjust the variational parameters by gradient descent. A direct sampling from Eqs. 5 and 6 would mask the variational parameters and therefore exclude them from optimization. The indirect sampling according to Eqs. 1 and 3 ensures that the variational parameters can be updated. In addition, and are reparametrized according to Eqs. 2 and 4 since they regulate the variance of the random network parameters and therefore should not take values less than zero. As one can see in Figure1 the reparameterization used guarantees that and stay positive during optimization.

Finally we define the overall variational distribution by

 qϕ––(w––)=d∏i=1qϕ––i(w––i)qϕ––bi(b–i)
 with    ϕ––i:={m––i,δi}, ϕ––bi:={m––bi,δbi}

by assuming that and are stochastically independent and with and denoting densities of normal distributions according to Eqs. 5 and 6. The depth of the network is denoted by . Thus, parameter uncertainty is expressed layer-wise and relative to the parameter expectation values. It is assumed that the uncertainty in network parameters depends on the layers they belong to and not on single parameters or neurons. In analogy to the variational density, the normal prior is defined as

 p(w––):=d∏i=1p(w––i)p(b–i)
 with   W–––i∼N(μ––i,ζ2iIKi×Ki)

 and   b–i∼N(μ––bi,ζ2biIki×ki).

Therefore, the a priori uncertainty is again expressed layer-wise. The fact that both the variational distribution and the prior factorize simplifies the calculation of the Kullback Leibler divergence between those two. It is given by the sum of the layer-wise divergences:

 DKL(qϕ––(w––)||p(w––))=Eqϕ––(w––)⎛⎜ ⎜ ⎜ ⎜⎝lnd∏i=1qϕ––i(w––i)q–ϕbi(b–i)d∏i=1p(w––i)p(b–i)⎞⎟ ⎟ ⎟ ⎟⎠ =Eqϕ––(w––){d∑i=1[ln(qϕ––i(w––i))−ln(p(w––i))] =d∑i=1[Eqϕ––i(w––i)lnqϕ––i(w––i)p(w––i)]+d∑i=1[Eqϕ––bi(b–i)lnqϕ––bi(b–i)p(b–i)] =d∑i=1[DKL(qϕ––i(w––i)||p(w––i))]+d∑i=1[DKL(qϕ––bi(b–i)||p(b–i))]

Given that the KL-divergence between two -dimensional Gaussians and is given by (Hershey and Olsen, 2007)

 DKL(h||g)=12 [ln|Σg||Σh|+tr(Σ−1gΣh)−p +(μ––h−μ––g)TΣ−1g(μ––h−μ––g)]

it is easy to calculate . Up to an additive constant, which plays no role in the optimization, it is given by:

 DKL(qϕ––(w––)||p(w––))∝ 12d∑i=1[−ln(τ2Kii|diag(m––i)2|)+τ2iζ2i||m––i||22+1ζ2i||m––i−μ––i||22 −ln(τ2kibi|diag(m––bi)2|)+τ2biζ2bi||m––bi||22+1ζ2bi||m––bi−μ––bi||22]

In order to train a network in a Bayesian way according to our approach, i.e. with weights sampled from and as regularization, the partial derivatives with respect to and

are needed. The derivatives of the neural net can easily be expressed in terms of the classical derivatives in non-Bayesian deep learning. According to the chain rule the derivatives with respect to

and are given by

 ∂f––∂mij =∂f––∂wij⋅∂wij∂mij=∂f––∂wij⋅(1+τiεij) (7) ∂f––∂mbij =∂f––∂bij⋅∂bij∂mbij=∂f––∂bij⋅(1+τbiεbij) (8)

where and are calculated as in the non-Bayesain case. Note that

 wij =mij(1+τiεij) bij =mbij(1+τbiεbij)

are denoting samples from and , respectively. Analogously, the derivatives with respect to and are given by:

 ∂f––∂δi =∑j∂f––∂wij⋅mijεijeδi1+eδi (9) ∂f––∂δbi =∑j∂f––∂bij⋅mbijεbijeδbi1+eδbi (10)

Further, one can easily verify that the partial derivatives of the KL-divergence are given by:

 ∂∂mijDKL =−1mij+τ2iζ2imij+1ζ2i(mij−μij) (11) ∂∂δiDKL =exp(δi)1+exp(δi)[τiζ2i||m––i||22−Kiτi] (12) ∂∂mbijDKL =−1mbij+τ2biζ2bimbij+1ζ2bi(mbij−μbij) (13) ∂∂δbiDKL =exp(δbi)1+exp(δbi)[τbiζ2bi||m––bi||22−kiτbi] (14)

### 3.2 Implementation

We implemented the approach illustrated above by modifying the popular open-source Caffe framework

(Jia et al, 2014; BVLC, 2016). For the convolutional layer and the inner product layer, the layer parameter ”blobs” were extended to include the additional variance terms for the weights and biases, and respectively, as well as the current realizations of and . The weights and biases of the classical imlpementation are here interpreted as the variational parameters and . In addition, for each layer, static arrays to hold the prior expectation values for the weights and biases and and the corresponding a priori variance terms and were introduced. The variances and expectation values of the prior distributions and the starting values for the variances and expectation values of the variational distributions and can be set for each layer in the network definiton prototext file. During each forward pass, one sample is drawn from the and the . From this, the random weights and biases used in the forward pass, and , are calculated according to Eqs. 1-4 using the current variational parameters and the current realizations of and . During the backward pass, the gradients of the weights and biases are adapted according to Eqs. 7 and 8. In addition, the gradients for the new variance parameters are calculated according to Eqs. 9 and 10. For each gradient, the additional term due to the Kullback-Leibler divergence is added following Eqs. 11-14

. These gradients are then used to calculate the updated variational parameters and thus the updated weights and biases to be used for the next forward pass. The additional term to the loss function due to the Kullback-Leibler divergence was not computed as it is not needed for the optimization.

## 4 Experimental Results

In this section, the proposed approach is tested. Basis of all experiments is the benchmark dataset MNIST (LeCun et al, 2018) together with the architecture LeNet (LeCun et al, 1998). The MNIST dataset consists of images of handwritten digits, from which build up the training dataset and the remaining build up the testing data. The specific version of LeNet used is the same described in Gal and Ghahramani (2016a). Therefore the first convolutional layer generates feature maps, while the second one extracts features. Both layers use

kernels. Max-pooling with kernel size

and stride

is applied after both convolutional layers. The first fully connected layer consists of neurons, the second one covers only since there are

different digits. Moreover, the first fully connected layer uses the rectified linear unit as activation and the other ones the identity function.

### 4.1 Frequentist Models

In order to get an idea how well our Bayesian approach performs it should be compared to the classical, i.e. the frequentist, approach. Therefore, LeNet is trained three times in the classical way. First, without dropout, then with dropout (dropping rate ) applied after the first inner product layer, and finally, with dropout applied as before and exchanged training and testing datasets. Exchanging training and testing data results in a significant reduction of the training data from to and should give an intuition how well bayesian models work for limited training data.

All three models are optimized the same way. To prevent overfitting, the Euclidean norm of the network weights is penalized with a factor of . As usual in deep learning, the optimization procedure applied is mini-batch gradient descent. A batch size of is chosen. The learning rate used in the -th iteration is given by . Momentum is used and set to . The accuracies achieved are given in Table 1 and are expressed by the corresponding test error.

The training converged quite similar in all three cases. A visualization of the training loss and test error for the second model, i.e. the model trained with dropout, is shown in Figure2. This figure will serve for comparison of the Bayesian and the frequentist training process.

### 4.2 Bayesian Models

To test and verify our Bayesian approach, LeNet is trained three times with the MNIST dataset. In analogy to the frequentisitc training (see section Section 4.1), LeNet is trained first without dropout, then with dropout, and finally, with dropout and exchanged training and testing data. In contrast to Gal and Ghahramani, we interpret dropout training as simultaneous training of multiple Bayesian models and assume that combining multiple models will result in a better accuracy than using just one model. Thus, during testing, the weight scaling inference rule, which states that each neuron should be used but multiplied with the dropping ratio, is not applied. Rather in the testing phase, neurons are randomly dropped in order to sample from the set of simultaneously trained Bayesian models and combine their predictions to one overall prediction.

In contrast to the non-Bayesian case, a penalization of the Euclidean norm does not take place since in the Bayesian case deviations from the a priori distribution are penalized. As there is not really a priori information available, the prior is used to express the wish that values should not diverge. Thus, the a priori expectation value is specified as zero for all network parameters, and further, the a priori standard deviation is chosen to be

for all weights and for all biases. The variance for the biases is chosen to be larger since biases act on linear combinations of neuron outputs with network weights as coefficients and therefore may take on larger values.

It should be mentioned that the penalization strength of the KL-divergence between the variational distribution and the a priori distribution is chosen smaller than recommended in the theoretical considerations in Section 3.1 because of convergence problems. Empirically, we found that we have to scale the penalization strength down by a factor of

to ensure convergence. While somewhat puzzling, this does not matter since there is not really a priori information available and the network parameters took small values in all experiments even with the reduced penalization. It should also be mentioned, that our implementation easily lends itself to ’Bayesian transfer learning’ in analogy to classical transfer learning

(Bengio, 2012), where the results of a previous training run with a large dataset are optimized for a more targeted application by finetuning the network with a smaller but specific training dataset. In the Bayesian case, the information about the posterior distribution of the network parameters in the pre-trained network will then be used to specify a prior for the finetuning step. This is subject to future work.

In order for the Bayesian networks to converge, the parameters and (see Section 3.1) which specify the a posteriori uncertainty have to be initialized carefully. Therefore, is initialized as and as in all network layers except for the first fully connected one which is treated separately. In this initialization, neural net weights can differ at most by the size of their expectation value from their expectation value, see Figure4.

This is a reasonable way to start since stronger deviations from the expectation values would mean that weights are even unsure about their algebraic sign, which might lead to convergence issues, if assumed for a majority of the network layers. In addition, assuming biases to vary less is not unusual since there are relatively few of them and they have a strong influence on the model since they act on sums. The reason why the first fully connected layer is treated differently is that it covers much more parameters than the other layers. Indeed it includes weights, while all the other layers together only contain weights. Due to the large number of parameters in the first fully connected layer, we assume that the model will be more uncertain in the network parameters of this layer. So is initialized with and with .

Finally, all Bayesian models are optimized with the same mini-batch optimization procedure as their frequentist analogues. For computing the model accuracies, each test example is propagated times through the network using Caffe’s bindings to Python. The test errors achieved and the absolute and relative decreases in the error with respect to the non-Bayesian models are given in Table 2.

One can see that the Bayesian models always perform better than their frequentist analogues. For the first two models the accuracy is only slightly better, while the third model shows a significant improvement, especially if one considers the relative decrease of the test error. It is not surprising that the increase in accuracy is only small for the first two models since all models considered converge very well and do not suffer from overfitting because there is plenty of training data available. The third model which is trained using only images shows signs of overfitting in the non-Bayesian case. The Bayesian network however is more robust towards overfitting and thus performs significantly better. This illustrates the advantage of Bayesian deep learning in the presence of only a limited number of training images.

It is interesting to see how LeNet converges following our Bayesian approach. In Figure5 and Figure6 the training is visualized for the first and the second model, i.e. the model trained without dropout and the model trained with dropout.

In contrast to the frequentist case, only the approximate test error is plotted. This means that only one sample of each testing image is used for predictions and that the weight scaling inference rule is applied. Currently, the Caffe framework does not provide other options for the testing phase during optimization. Nonetheless, the imprecise approximation of the test error gives a rough estimate of the real test error and therefore helps to understand what happens with the model accuracy during training. One can see that the loss (plotted without the term due to the KL-divergence) fluctuates heavily during training due to the random samples drawn from the variational distribution. However, the test error decreases quickly as in the non-Bayesian case and seems to keep decreasing as training goes on. This is not the case for the frequentist model (see Figure2) for which the test error seems to increase slowly. This, again, indicates the strength of our approach against overfitting.

The a posteriori uncertainties are quite the same for all three Bayesian models. In Table 3, the uncertainties for the second model, i.e. the model trained with dropout, are given. One can see that the model uncertainty is small for all layers except for the first fully connected one.

### 4.3 Reduced Model

In Section 4.2 LeNet was trained in a Bayesian way (with dropout), resulting in a high model uncertainty of the first fully connected Layer. A value of for indicates that the network is not even sure about the algebraic sign of the weights in this layer. Therefore, we assume that the network architecture is not optimal and reduce the number of output neurons for the first fully connected layer from to . In the Bayesian case, this does not lead to a significant increase of the network accuracy but the network uncertainty for the first fully connected layer decreases significantly as one can see in Table 4.

This result indicates that the Bayesian approach can be used to optimize the model architecture both in terms of accuracy and model size for a given training and testing dataset. In this particular case, we were able to reduce the number of parameters by almost a factor of while achieving the same accuracy. Even more interesing, when the reduced model is trained the classical way, the achieved accuracies become as good as for the Bayesian model, indicating again that the initial model was suffering from overfitting.

### 4.4 Prediction uncertainty

In addition to providing information about the model uncertainty, our approach can also be used to determine the uncertainties of the predictions. Due to the random sampling of the weights and biases during each forward pass, accurate credible intervals can be estimated by performing multiple forward passes per image. This information can be used in applications using our algorithm for classification. For example, a check for statistical significance for the classification result can be performed and the result can be used to decide about the next steps in the application (e.g. proceed autonomously, repeat classification, escalate to user, etc.). Figure 7

shows two boxplots of the random network outputs (model without dropout) for two representative images from the MNIST test data set. On the left, the boxplot for an image with correct classification result is shown. Clearly, the network is very certain about this classification result. On the right, the boxplot for an image with wrong classification result is shown. As can be seen, the result for the wrongly predicted label is not statistically significant as there is a clear overlap between the boxes of the true label and the predicted one. These boxplots were computed by performing the inference 100 times for each image. It is interesting to note that in the case of the wrongly predicted image on the right, the network produces very high outlier probabilities for other classes besides the true and the predicted label. This illustrates the potential for deterministic networks to produce wrong classifications with very high class probabilities. Checking for all images if the estimated 95% credible intervals of the predicted classes overlap with the 95% intervals of the other classes gives further insight into the prediction capabilities of the network. Table

5 summarizes the results for the model without dropout, see section 4.2.

As can be seen, the overwhelming majority of classification results is correct and the network is also confident about these predictions. About 300 images are classified correctly, but the network is not sure within 95% credible intervals. A total of 94 images are classified incorrectly. In the vast majority of these cases, the network is unsure about the classification result. In only 14 cases, the network is quite sure about its wrong classification. Please note, that due to the random sampling of network parameters, the results are slightly different each time they are computed unless a very large number of forward passes is performed for each image. This is also the reason why the number of missclassified images in this section differs from the one obtained in section

4.2. The uncertainty analysis presented here was performed separately. From an application point of view, the latter case is the most critical. Figure 8 shows all of the 14 images which have been classified wrongly with confidence by the network. More than half of these images visually resemble the predicted label at least as much as they resemble the true label. The remaining images are without a doubt wrongly classified. Some of these images can be excluded by raising the confidence level requirement. A detailed investigation into wrong yet confident classification results is left for further study.

## 5 Conclusion

We present here for the first time a Bayesian approach to deep learning that allows for accurate calculation of the uncertainty of network predictions as well as the uncertainty of the model parameters while introducing only few additional parameters to be optimized. In particular, we introduce two variance terms per layer (one for the weight parameters, one for the biases) that are optimized during training along with the other network parameters. Compared to classical, frequentist models, our approach is more robust against overfitting. Especially for small training datasets, a significant improvement in accuracy is obtained with our approach. In addition, information about network uncertainty can be readily interpreted and used for improvements of network architecture. Finally, our approach provides accurate uncertainty information about the predictions of the network with potentially significant impact for real world applications.

## 6 Conflict of interest

The authors declare that they have no conflict of interest.

## References

• Arora et al (2014) Arora S, Bhaskara A, Ge R, Ma T (2014) Provable bounds for learning some deep representations. In: Proceedings of the 31st International Conference on Machine Learning
• Banerjee et al (2017)

Banerjee K, Dinh TV, Levkova L (2017) Velocity estimation from monocular video for automotive applications using convolutional neural networks. In: IEEE Intelligent Vehicles Symposium

• Bengio (2012) Bengio Y (2012) Deep learning of representations for unsupervised and transfer learning. In: JMLR: Workshop and Conference Proceedings, vol 27, pp 17–37
• Bengio et al (2006) Bengio Y, Schwenk H, Senécal JS, Morin F, Gauvain JL (2006) Neural Probabilistic Language Models. Springer, in Innovations in Machine Learning
• Bishop (2006)

Bishop CM (2006) Pattern Recognition and Machine Learning. Springer

• Blei et al (2016) Blei DM, Kucukelbir A, McAuliffe JD (2016) Variational inference: A review for statisticians. arXiv:160100670 [statCO]
• Blundell et al (2015) Blundell C, Cornebise J, Kavukcuoglu K, Wierstra D (2015) Weight uncertainty in neural networks. ICML
• BVLC (2016) BVLC (2016) Caffe. https://github.com/BVLC/caffe
• Gal and Ghahramani (2016a) Gal Y, Ghahramani Z (2016a) Bayesian convolutional neural networks with bernoulli approximate variational inference. ICLR
• Gal and Ghahramani (2016b) Gal Y, Ghahramani Z (2016b) Dropout as a bayesian approximation: Appendix. arXiv:150602157 [statML]
• Gal and Ghahramani (2016c) Gal Y, Ghahramani Z (2016c) Dropout as a bayesian approximation: Representing model uncertainty in deep learning. ICML
• Goodfellow et al (2016) Goodfellow I, Bengio Y, Courville A (2016) Deep Learning. MIT Press
• Graves (2011) Graves A (2011) Practical variational inference for neural networks. NIPS
• Greenspan et al (2016) Greenspan H, van Ginneken B, Summers RM (2016) Deep learning in medical imaging: Overview and future promise of an exciting new technique. IEEE Transactions on Medical Imaging 35(5)
• Gulshan et al (2016) Gulshan V, Peng L, Coram M, Stumpe MC, Wu D, Narayanaswamy A, Venugopalan S, Widner K, Madams T, Cuadros J, Kim R, Raman R, Nelson PC, Mega JL, Webster DR (2016) Development and validation of a deep learning algorithm for detection of diabetic retinopathy in retinal fundus photographs. JAMA, the Journal of the American Medical Association 316(22)
• Heaton et al (2017) Heaton J, Polson N, Witte J (2017) Deep learning for finance: deep portfolios. Applied Stochastic Models in Business and Industry 33(1)
• Hernandez-Lobato et al (2015) Hernandez-Lobato JM, Li Y, Rowland M, Hernandez-Lobato D, Bui T, Turner RE (2015) Black-box alpha divergence minimization. arXiv:151103243 [statML]
• Hershey and Olsen (2007)

Hershey JR, Olsen PA (2007) Approximating the kullback leibler divergence between gaussian mixture models. ICASSP

• Hinton and van Camp (1993)

Hinton GE, van Camp D (1993) Keeping the neural networks simple by minimizing the description length of the weights. In: Proceedings of the sixth annual conference on Computational learning theory

• Hornik et al (1990) Hornik K, Stinchcombe M, White H (1990) Universal approximation of an unknown mapping and its derivatives using multilayer feedforward networks. Elsevier Neural Networks
• Jia et al (2014) Jia Y, Shelhamer E, Donahue J, Karayev S, Long J, Girshick R, Guadarrama S, Darrell T (2014) Caffe: Convolutional architecture for fast feature embedding. arXiv preprint arXiv:14085093
• Jordan et al (1999) Jordan MI, Ghahramani Z, Jaakkola TS, Saul LK (1999) An introduction to variational methods for graphical models. Machine Learning 37(2):183–233
• Jozwik et al (2017) Jozwik KM, Kriegeskorte N, Storrs KR, Mur M (2017) Deep convolutional neural networks outperform feature-based but not categorical models in explaining object similarity judgements. Frontiers in Psychology 8
• Kingma et al (2015) Kingma DP, Salimans T, Welling M (2015) Variational dropout and the local reparameterization trick. arXiv:150602557 [statML]
• Krizhevsky et al (2012)

Krizhevsky A, Sutskever I, Hinton GE (2012) Imagenet classification with deep convolutional neural networks. NIPS

• LeCun et al (1998) LeCun Y, Bottou L, Bengio Y, Haffner P (1998) Gradient-based learning applied to document recognition. IEEE
• LeCun et al (2018)

LeCun Y, Cortes C, Burges CJ (2018) The mnist database of handwritten digits. URL

http://yann.lecun.com/exdb/mnist/
• Li et al (2018) Li X, Ding Q, Sun JQ (2018) Remaining useful life estimation in prognostics using deep convolution neural networks. Reliability Engineering & System Safety 172
• Li and Gal (2017) Li Y, Gal Y (2017) Dropout inference in bayesian neural networks with alpha-divergences. arXiv:170302914 [csLG]
• Liang and Srikant (2017) Liang S, Srikant R (2017) Why deep neural networks for function approximation? ICLR
• Louizos and Welling (2016) Louizos C, Welling M (2016) Structured and efficient variational deep learning with matrix gaussian posteriors. arXiv:160304733 [statML]
• Rowan (2017) Rowan A (2017) Bayesian deep learning with edward (and a trick using dropout). PyData
• Shun-Ichi (1985) Shun-Ichi A (1985) Differential-Geometrical Methods in Statistics. Springer
• Srivastava et al (2014) Srivastava N, Hinton G, Krizhevsky A, Sutskever I, Salakhutdinov R (2014) Dropout: A simple way to prevent neural networks from overfitting. Journal of Machine Learning
• Wan et al (2013) Wan L, Zeiler M, Zhang S, LeCun Y, Fergus R (2013) Regularization of neural networks using dropconnect. ICML