Variational Inference to Measure Model Uncertainty in Deep Neural Networks
Konstantin Posch Jan Steinbrener Jürgen Pilz












K. Posch Alpen-Adria Universität, Klagenfurt am Wörthersee, Austria 

              koposch@edu.aau.at           

           J. Steinbrener CTR Carinthian Tech Research AG, Villach, Austria 

	jan.steinbrener@ctr.at
	J. Pilz Alpen-Adria Universität, Klagenfurt am Wörthersee, Austria 

	juergen.pilz@aau.at





Variational Inference to Measure Model Uncertainty in Deep Neural Networks The results presented in this article have been obtained as part of the master thesis of Konstantin Posch in 2017. The master thesis has been peer reviewed and accepted by the thesis committee according to common University guidelines and standards on October 29th, 2017. It has been made available through the library of the University of Klagenfurt since then.  The results of the Master thesis have been presented at the 23rd Int. Meeting of Young Statisticians in Balatonfüred (Hungary) on October 14, 2018. A version of this article has been submitted to Springer International Journal of Computer Vision on May 7th, 2018 and is currently still under review. We have since been notified about another article published on Arxiv on November 14th, 2018 (https://arxiv.org/pdf/1806.05978.pdfhttps://arxiv.org/pdf/1806.05978.pdf) using essentially the same approach as we did, i.e. with variances of the variational distribution specified as multiples of the corresponding squared expectation values. However in their approach, different multiplication factors for each network weight are assumed whereas we only use one factor for all weights of one particular layer.
    Konstantin Posch*  *both authors contributed equally to this work       Jan Steinbrener* Jürgen Pilz 

    Received: date / Accepted: date
============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================





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%. In addition, the trained model contains 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.






This work was performed within the Competence Centre 'ASSIC Austrian Smart Systems Integration  Research Center', co-funded by the Federal Ministries of Transport, Innovation and Technology (bmvit) and Science, Research and Economy (bmwfw) and the Federal Provinces of Carinthia and Styria within the COMET - Competence Centers for Excellent Technologies Programme. This work was also supported by Philips Austria GmbH as part of the cooperation between Philips Austria and CTR within the COMET programme. 





§ INTRODUCTION


Deep learning has led to series of breakthroughs in many fields of applied machine learning, especially in image classification <cit.> or natural language processing <cit.>. 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 <cit.>. Currently, it has been shown that for a given bound of the approximation error, deep networks require exponentially less data than shallow ones <cit.>. 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 <cit.>.  

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 <cit.>. In addition, most network architectures today are designed based on trial and error or based on abstract, high level considerations <cit.>. 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 <cit.>. 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 <cit.>. <cit.> 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. 


§ 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 <cit.>. 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.



 §.§ 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 W. A priori knowledge regarding W is expressed in terms of the a priori distribution p(w). One is interested in updating the knowledge about W after observing data D={y,X}, where X={x_1,...,x_β} denotes a set of training examples and y=(y_1,...,y_β)^T holds the corresponding class labels. Therefore, the a posteriori distribution p(w|y,X) 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 p(y|w,X) 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 p(w|y,X) is the generally intractable integral in the denominator. Variational inference aims at approximating the posterior p(w|y,X) by optimizing a parametric distribution q_ϕ(w), such that it is most similar to p(w|y,X).

Once the variational distribution is optimized, it can be used for predicting new data and further quantifying uncertainty in predictions. The posterior predictive distribution p(y^*|x^*,y,X) reflects the belief in a class label y^* for a given example x^* after observing data y,X:

    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) ≈1/N∑_i=1^N f(x^*;w_i)_y^*   with w_ii.i.d.∼q_ϕ(w)

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

    y^*=jarg max1/N∑_i=1^N f(x^*;w_i)_j

To measure model uncertainty, credible intervals can be estimated. The output of a neural net f(x^*,w)_y^* with fixed parameters equals the probability p(y^*|w,x^*). Further, the variational distribution q_ϕ(w) reflects the knowledge about the random network parameters W. The uncertainty in the parameters implies an uncertainty in the neural network output, i.e. the probability of class y^*. By sampling from the variational distribution and subsequently calculating the empirical  α/2 and 1-α/2 quantiles of the corresponding network outputs f(x^*;w_1)_y^*,...,f(x^*;w_N)_y^* an estimate of the 1-α credible interval for the probability of y^* is found. Note that neural network outputs are uniquely determined for fixed w 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 p(w|y,X). This can be accomplished by minimizeing the Kullback-Leibler divergence (KL-divergence) D_KL(q_ϕ(w)||p(w|y,X)) between the variational density and the posterior. It is defined as:

    D_KL(q_ϕ(w)||p(w|y,X))   :=𝔼_q_ϕ(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, D_KL(q_ϕ(w)||p(w|y,X)) cannot be minimized directly since the a posteriori distribution is unknown. However, minimizing  D_KL(q_ϕ(w)||p(w|y,X)) is equivalent to minimizing the negative log evidence lower bound L_VI <cit.>, which is given by:

    L_VI   =-∫ q_ϕ(w)lnp(y|w,X) dw+D_KL(q_ϕ(w)||p(w))
       =-𝔼_q_ϕ(w)[ln∏_i=1^βp(y_i|w, x_i)]+D_KL(q_ϕ(w)||p(w))
       =-∑_i=1^β[𝔼_q_ϕ(w)(ln p(y_i|w, x_i))]+D_KL(q_ϕ(w)||p(w))

L_VI 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 L_VI. 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 k-th iteration of the optimization is given by:

    L_VI^k=-∑_i=1^βlnf(x_i;w_k)_y_i+D_KL(q_ϕ(w)||p(w))

If one wants to use mini-batch gradient descent, the KL-divergence has to be rescaled by the factor m/•β, where m 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 L^2-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.



 §.§ Related Work


There is a large number of possibilities of how to choose the variational distribution. <cit.> 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 i-1 is equivalent to dropping all weights in layer i 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 L^2-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 <cit.>.

<cit.> used a normal distribution with a diagonal covariance matrix as variational distribution. The approach was evaluated with the LeNet architecture <cit.> 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. <cit.> 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 <cit.> and <cit.>.

It should be mentioned that variational Bayes is just a specific case of local α-divergence minimization. The α-divergence <cit.> between two densities p(w) and q(w) is defined as

    D_α(p(w)||q(w))=1/α(1-α)(1-∫ p(w)^αq(w)^1-α dw)

such that D_α(p(w)||q(w)) converges to the Kullback-Leibler divergence D_KL(q(w)||p(w)) for α→ 0. 
<cit.> have shown that the optimal setting for α is task specific and that a nonstandard stetting α≠ 0 can produce better prediction results. <cit.> 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 α=0, 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 <cit.>.



§ 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 <cit.> 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 <cit.> 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 <cit.>. 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.



 §.§ Methodology


Let W_i=(W_i1,...,W_iK_i)^T denote the random weights of the i-th network layer and further let 
B_i=(B_i1,...,B_ik_i) denote the corresponding random biases. In addition, let ℰ_i=(ℰ_i1,...,ℰ_iK_i)^T and ℰ_bi=(ℰ_bi1,...,ℰ_bik_i)^T be multivariate standard normal distributed. To set up the variational distribution, the random weights W_i and the random biases B_i are defined by

    W_i    :=m_i⊙(1_K_i +τ_iℰ_i) 
    τ_i    :=ln(1+exp(δ_i)) 
    B_i    :=m_bi⊙(1_k_i+τ_biℰ_bi) 
    τ_bi   :=ln(1+exp(δ_bi))

where m_i∈ℝ^K_i, m_bi∈ℝ^k_i, δ_i∈ℝ and δ_bi∈ℝ are variational parameters and ⊙ denotes the Hadamard product, i.e. element-wise multiplication. This implies that W_i and B_i are multivariate normal distributed according to

    W_i    ∼ N(m_i,τ_i^2 diag(m_i)^2) 
    B_i    ∼ N(m_bi, τ_bi^2diag(m_bi)^2).

The reason why weights and biases are not directly defined by the Gaussians given in Eqs. <ref> and <ref>, 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. <ref> and <ref> would mask the variational parameters and therefore exclude them from optimization. The indirect sampling according to Eqs. <ref> and <ref> ensures that the variational parameters can be updated. In addition, τ_i and τ_bi are reparametrized according to Eqs. <ref> and <ref> since they regulate the variance of the random network parameters and therefore should not take values less than zero. As one can see in <ref> the reparameterization  used guarantees that τ_i and τ_bi stay positive during optimization.

Finally we define the overall variational distribution by

    q_ϕ(w)=∏_i=1^dq_ϕ_i(w_i)q_ϕ_bi(b_i)


    with    ϕ_i:={m_i,δ_i}, ϕ_bi:={m_bi,δ_bi}

by assuming that W_1,...,W_d and B_1,...,B_d are stochastically independent and with q_ϕ_i(w_i) and q_ϕ_bi(b_i) denoting densities of normal distributions according to Eqs. <ref> and <ref>. The depth of the network is denoted by d. 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 p(w)  is defined as

    p(w):=∏_i=1^dp(w_i)p(b_i)


    with   W_i∼ N(μ_i,ζ_i^2 I_K_i× K_i)
 
 
    and   b_i∼ N(μ_bi, ζ_bi^2I_k_i× k_i).

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:


    D_KL(q_ϕ(w)||p(w))=𝔼_q_ϕ(w)(ln∏_i=1^dq_ϕ_i(w_i)q_ϕ_bi(b_i)/∏_i=1^dp(w_i)p(b_i))
       =𝔼_q_ϕ(w){∑_i=1^d[ln(q_ϕ_i(w_i))-ln(p(w_i))].
       .+∑_i=1^d[ln(q_ϕ_bi(b_i))-ln(p(b_i))]}
       =∑_i=1^d[𝔼_q_ϕ_i(w_i)lnq_ϕ_i(w_i)/p(w_i)]+∑_i=1^d[𝔼_q_ϕ_bi(b_i)lnq_ϕ_bi(b_i)/p(b_i)]
       =∑_i=1^d[D_KL(q_ϕ_i(w_i)||p(w_i))]+∑_i=1^d[D_KL(q_ϕ_bi(b_i)||p(b_i))]


Given that the KL-divergence between two p-dimensional Gaussians h(x)=N(x;μ_h,Σ_h) and g(x)=N(x;μ_g,Σ_g) is given by <cit.>

    D_KL(h||g)=1/2   [ln|Σ_g|/|Σ_h|+tr(Σ_g^-1Σ_h)-p.
       .+(μ_h-μ_g)^TΣ_g^-1(μ_h-μ_g)]

it is easy to calculate D_KL(q_ϕ(w)||p(w)). Up to an additive constant, which plays no role in the optimization, it is given by:


    D_KL(q_ϕ(w)||p(w))∝
       1/2∑_i=1^d[-ln(τ_i^2K_i|diag(m_i)^2|)+τ_i^2/ζ_i^2||m_i||_2^2+1/ζ_i^2||m_i-μ_i||_2^2.
       .-ln(τ_bi^2k_i|diag(m_bi)^2|)+τ_bi^2/ζ_bi^2||m_bi||_2^2+1/ζ_bi^2||m_bi-μ_bi||_2^2]



In order to train a network f in a Bayesian way according to our approach, i.e. with weights sampled from q_ϕ(w) and D_KL(q_ϕ(w)||p(w)) as regularization, the partial derivatives with respect to m_ij,m_bij,δ_i and δ_bi 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 m_ij and m_bij are given by

    ∂f/∂ m_ij   =∂f/∂ w_ij·∂ w_ij/∂ m_ij=∂f/∂ w_ij·(1+τ_iε_ij) 
    ∂f/∂ m_bij   =∂f/∂ b_ij·∂ b_ij/∂ m_bij=∂f/∂ b_ij·(1+τ_biε_bij)

where ∂f/∂ w_ij and ∂f/∂ b_ij are calculated as in the non-Bayesain case. Note that

    w_ij   =m_ij(1+τ_iε_ij)
    
    b_ij   =m_bij(1+τ_biε_bij)

are denoting samples from W_ij and B_ij, respectively.
Analogously, the derivatives with respect to δ_i and δ_bi are given by:

    ∂f/∂δ_i   =∑_j∂f/∂ w_ij· m_ijε_ije^δ_i/1+e^δ_i
    ∂f/∂δ_bi   =∑_j∂f/∂ b_ij· m_bijε_bije^δ_bi/1+e^δ_bi

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

    ∂/∂ m_ijD_KL   =-1/m_ij+τ_i^2/ζ_i^2m_ij+1/ζ_i^2(m_ij-μ_ij) 
    ∂/∂δ_iD_KL   =exp(δ_i)/1+exp(δ_i)[τ_i/ζ_i^2||m_i||_2^2-K_i/τ_i]
    ∂/∂ m_bijD_KL   =-1/m_bij+τ_bi^2/ζ_bi^2m_bij+1/ζ_bi^2(m_bij-μ_bij) 
    ∂/∂δ_biD_KL   =exp(δ_bi)/1+exp(δ_bi)[τ_bi/ζ_bi^2||m_bi||_2^2-k_i/τ_bi]





 §.§ Implementation


We implemented the approach illustrated above by modifying the popular open-source Caffe framework <cit.>. 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, δ_i and δ_bi respectively, as well as the current realizations of ℰ_i and ℰ_bi. The weights and biases of the classical imlpementation are here interpreted as the variational parameters m_i and m_bi. In addition, for each layer, static arrays to hold the prior expectation values for the weights and biases μ_ij and μ_bij and the corresponding a priori variance terms ζ_i and ζ_bi 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 δ_i and δ_bi can be set for each layer in the network definiton prototext file. During each forward pass, one sample is drawn from the ℰ_i and the ℰ_bi. From this, the random weights and biases used in the forward pass, W_i and B_i, are calculated according to Eqs. <ref>-<ref> using the current variational parameters and the current realizations of ℰ_i and ℰ_bi. During the backward pass, the gradients of the weights and biases are adapted according to Eqs. <ref> and <ref>. In addition, the gradients for the new variance parameters are calculated according to Eqs. <ref> and <ref>. For each gradient, the additional term due to the Kullback-Leibler divergence is added following Eqs. <ref>-<ref>. 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. 



§ EXPERIMENTAL RESULTS
 



In this section, the proposed approach is tested. Basis of all experiments is the benchmark dataset MNIST <cit.> together with the architecture LeNet <cit.>. The MNIST dataset consists of 70,000 images of handwritten digits, from which 60,000 build up the training dataset and the remaining 10,000 build up the testing data. The specific version of LeNet used is the same described in <cit.>. Therefore the first convolutional layer generates 20 feature maps, while the second one extracts 50 features. Both layers use (5× 5) kernels. Max-pooling with kernel size (2× 2) and stride 2 is applied after both convolutional layers. The first fully connected layer consists of 500 neurons, the second one covers only 10 since there are 10 different digits. Moreover, the first fully connected layer uses the rectified linear unit as activation and the other ones the identity function.




 §.§ 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 0.5) 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 60,000 to 10,000 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 0.0005. As usual in deep learning, the optimization procedure applied is mini-batch gradient descent. A batch size of 64 is chosen. The learning rate used in the i-th iteration is given by 0.01*(1+0.0001*i)^-0.75. Momentum is used and set to 0.9. The accuracies achieved are given in Table <ref> 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 <ref>. This figure will serve for comparison of the Bayesian and the frequentist training process.






 §.§ 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 <ref>), 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 5 for all weights and 10 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 <ref> because of convergence problems. Empirically, we found that we have to scale the penalization strength down by a factor of 100 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 <cit.>, 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 τ_i and τ_bi (see Section <ref>) which specify the a posteriori uncertainty have to be initialized carefully. Therefore, τ_i is initialized as 0.4 and τ_bi as 0.1 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 <ref>.

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 400,000 weights, while all the other layers together only contain 30,500 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 τ_i is initialized with 1 and τ_bi with 0.2.

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 100 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 <ref>.

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 10,000 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 <ref> and <ref> 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 <ref>) 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 <ref>, 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.




 §.§ Reduced Model





In Section <ref> LeNet was trained in a Bayesian way (with dropout), resulting in a high model uncertainty of the first fully connected Layer. A value of 0.76 for τ_3 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 500 to 250. 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 <ref>.

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 2 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.



 §.§ 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 <ref> 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 <ref> summarizes the results for the model without dropout, see section <ref>.

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 <ref>. The uncertainty analysis presented here was performed separately. From an application point of view, the latter case is the most critical. Figure <ref> 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. 


§ 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. 








































§ CONFLICT OF INTEREST

The authors declare that they have no conflict of interest.


spbasic      


   
















