A Simple Baseline for Bayesian Uncertainty in Deep Learning

  • 2019-02-07 05:15:46
  • Wesley Maddox, Timur Garipov, Pavel Izmailov, Dmitry Vetrov, Andrew Gordon Wilson
  • 42

Abstract

We propose SWA-Gaussian (SWAG), a simple, scalable, and general purposeapproach for uncertainty representation and calibration in deep learning.Stochastic Weight Averaging (SWA), which computes the first moment ofstochastic gradient descent (SGD) iterates with a modified learning rateschedule, has recently been shown to improve generalization in deep learning.With SWAG, we fit a Gaussian using the SWA solution as the first moment and alow rank plus diagonal covariance also derived from the SGD iterates, formingan approximate posterior distribution over neural network weights; we thensample from this Gaussian distribution to perform Bayesian model averaging. Weempirically find that SWAG approximates the shape of the true posterior, inaccordance with results describing the stationary distribution of SGD iterates.Moreover, we demonstrate that SWAG performs well on a wide variety of computervision tasks, including out of sample detection, calibration, and transferlearning, in comparison to many popular alternatives including MC dropout, KFACLaplace, and temperature scaling.

 

Quick Read (beta)

Abstract

We propose SWA-Gaussian (SWAG), a simple, scalable, and general purpose approach for uncertainty representation and calibration in deep learning. Stochastic Weight Averaging (SWA), which computes the first moment of stochastic gradient descent (SGD) iterates with a modified learning rate schedule, has recently been shown to improve generalization in deep learning. With SWAG, we fit a Gaussian using the SWA solution as the first moment and a low rank plus diagonal covariance also derived from the SGD iterates, forming an approximate posterior distribution over neural network weights; we then sample from this Gaussian distribution to perform Bayesian model averaging. We empirically find that SWAG approximates the shape of the true posterior, in accordance with results describing the stationary distribution of SGD iterates. Moreover, we demonstrate that SWAG performs well on a wide variety of computer vision tasks, including out of sample detection, calibration, and transfer learning, in comparison to many popular alternatives including MC dropout, KFAC Laplace, and temperature scaling.

oddsidemargin has been altered.
marginparsep has been altered.
topmargin has been altered.
marginparwidth has been altered.
marginparpush has been altered.
paperheight has been altered.
The page layout violates the ICML style. Please do not change the page layout, or include packages like geometry, savetrees, or fullpage, which change it for you. We’re not able to reliably undo arbitrary changes to the style. Please remove the offending package(s), or layout-changing commands and try again.

 

A Simple Baseline for Bayesian Uncertainty in Deep Learning

 

Wesley Maddox* 0  Timur Garipov* 0  Pavel Izmailov* 0  Dmitry Vetrov0 0  Andrew Gordon Wilson0 


footnotetext: *Equal contribution \forloop@affilnum1¡ 0National Research University Higher School of Economics. Correspondence to: Wesley Maddox <[email protected]>.  
\@xsect

Ultimately, machine learning models are used to make decisions. Representing uncertainty is crucial for decision making. For example, in medical diagnoses and autonomous vehicles we want to protect against rare but costly mistakes. Deep learning models typically lack a representation of uncertainty, and provide overconfident and miscalibrated predictions (e.g., Kendall & Gal, 2017; Guo et al., 2017).

Bayesian methods provide a natural probabilistic representation of uncertainty in deep learning (e.g., Blundell et al., 2015; Kingma et al., 2015b; Chen et al., 2014), and previously had been a gold standard for inference with neural networks (Neal, 1996). However, existing approaches are often highly sensitive to hyperparameter choices, and hard to scale to modern datasets and architectures, which limits their general applicability in modern deep learning.

In this paper we propose a different approach to Bayesian deep learning: we use the information contained in the SGD trajectory to efficiently approximate the posterior distribution over the weights of the neural network. We find that the Gaussian distribution fitted to the first two moments of SGD iterates, with a modified learning rate schedule, captures the local geometry of the posterior surprisingly well. Using this Gaussian distribution we are able to obtain convenient, efficient, accurate and well-calibrated predictions in a broad range of tasks in computer vision. In particular, our contributions are the following:

  • In this work we propose SWAG (SWA-Gaussian), a scalable approximate Bayesian inference technique for deep learning. SWAG builds on Stochastic Weight Averaging (Izmailov et al., 2018), which computes an average of SGD iterates with a high constant learning rate schedule, to provide improved generalization in deep learning. SWAG additionally computes a low-rank plus diagonal approximation to the covariance of the iterates, which is used together with the SWA mean, to define a Gaussian posterior approximation over neural network weights.

  • SWAG is motivated by the theoretical analysis of the stationary distribution of SGD iterates (e.g., Mandt et al., 2017; Chen et al., 2016), which suggests that the SGD trajectory contains useful information about the geometry of the posterior. In Section id1 we show that the assumptions of Mandt et al. (2017) do not hold for deep neural networks, due to non-convexity and over-parameterization. However, we find that in the subspace spanned by SGD iterates the shape of the posterior distribution is approximately Gaussian. Further, SWAG is able to capture the geometry of this posterior remarkably well.

  • On a thorough empirical evaluation we show that SWAG can provide well-calibrated uncertainty estimates for neural networks across many settings in computer vision. In particular SWAG achieves higher test likelihood compared to many state-of-the-art approaches, including MC-Dropout (Gal & Ghahramani, 2016), temperature scaling (Guo et al., 2017), KFAC-Laplace (Ritter et al., 2018b) and SWA (Izmailov et al., 2018) on CIFAR-10, CIFAR-100 and ImageNet, on a range of architectures. We also demonstrate the effectiveness of SWAG for out-of-domain detection, and transfer learning.

  • We release PyTorch code at
    github.com/wjmaddox/swa_gaussian

\@xsect\@xsect

Bayesian approaches represent uncertainty by placing a distribution over model parameters, and then marginalizing these parameters to form a whole predictive distribution, in a procedure known as Bayesian model averaging. In the late 1990s, Bayesian methods were the state-of-the-art approach to learning with neural networks, through the seminal works of Neal (1996) and MacKay (1992a). However, modern neural networks often contain millions of parameters, the posterior over these parameters (and thus the loss surface) is highly non-convex, and mini-batch approaches are often needed to move to a space of good solutions (Keskar et al., 2017). For these reasons, Bayesian approaches have largely been intractable for modern neural networks. Here, we review several modern approaches to Bayesian deep learning.

\@xsect

was at one time a gold standard for inference with neural networks, through the Hamiltonian Monte Carlo (HMC) work of Neal (1996). However, HMC requires full gradients, which is computationally intractable for modern neural networks. To extend the HMC framework, stochastic gradient HMC (SGHMC) was introduced by Chen et al. (2014) and allows for stochastic gradients to be used in Bayesian inference, crucial for both scalability and exploring a space of solutions that provide good generalization. While SGHMC has been successfully applied in Bayesian deep learning (e.g., Saatci & Wilson, 2017), tuning SGHMC can be quite difficult, and many settings of SGHMC parameters are quite similar to standard SGD.

\@xsect

Graves (2011) suggested fitting a Gaussian variational posterior approximation over the weights of neural networks. This technique was generalized by Kingma & Welling (2013) which proposed the reparameterization trick for training deep latent variable models; multiple variational inference methods based on reparameterization trick were proposed for DNNs (e.g., Kingma et al., 2015a; Blundell et al., 2015; Molchanov et al., 2017; Louizos & Welling, 2017). While variational methods achieve strong performance for moderately sized networks, they can be difficult to train on larger architectures such as deep residual networks (He et al., 2016), due to the looseness of the optimization objective by Blier & Ollivier (2018).

An alternative line of work re-interprets noisy versions of optimization algorithms: for example, noisy Adam (Khan et al., 2018) and noisy KFAC (Zhang et al., 2017), as approximate variational inference.

\@xsect

Gal & Ghahramani (2016) used a spike and slab variational distribution to view dropout at test time as approximate variational Bayesian inference. Concrete dropout (Gal et al., 2017) extends this idea to optimize the dropout probabilities as well. From a practical perspective, these approaches are quite appealing as they only require ensembling dropout predictions at test time, and they were succesfully applied to several downstream tasks (Kendall & Gal, 2017; Mukhoti & Gal, 2018).

\@xsect

assume a Gaussian posterior, 𝒩(θ*,(θ*)-1), where θ* is a MAP estimate and (θ*)-1 is the inverse of the Fisher information matrix (expected value of the Hessian evaluated at θ*). It was notably used for Bayesian neural networks in MacKay (1992b), where a diagonal approximation to the inverse of the Hessian was utilized for computational reasons. More recently, Kirkpatrick et al. (2017) proposed to use diagonal Laplace approximations to overcome catastrophic forgetting in deep learning. Ritter et al. (2018b) proposed the use of either a diagonal or block Kronecker factored (KFAC) approximation to the Hessian matrix for Laplace approximations, and Ritter et al. (2018a) successfully applied the KFAC approach to online learning situations.

\@xsect

Alternatively, Lakshminarayanan et al. (2017) proposed using ensembles of several networks for enhanced calibration, and incorporated an adversarial loss function to be used when possible as well. Outside of probabilistic neural networks, Guo et al. (2017) proposed temperature scaling, a procedure which uses a validation set and a single hyperparameter to rescale the logits of DNN outputs for enhanced calibration. Kuleshov et al. (2018) propose calibrated regression using a similar rescaling technique.

\@xsect

In this section we propose SWA-Gaussian (SWAG) for Bayesian model averaging and uncertainty estimation. In Section id1, we review stochastic weight averaging (SWA) (Izmailov et al., 2018), which we view as estimating the mean of the stationary distribution of SGD iterates. We then propose SWA-Gaussian in Sections id1 and id1 to estimate the covariance of the stationary distribution, forming a Gaussian approximation to the posterior over weight parameters. With SWAG, uncertainty in weight space is captured with minimal modifications to the SWA training procedure. We then present further theoretical and empirical analysis for SWAG in Section id1.

\@xsect

The main idea of SWA (Izmailov et al., 2018) is to run SGD with a constant learning rate schedule starting from a pre-trained solution, and to average the weights of the models it traverses. Denoting the weights of the network obtained after epoch i of SWA training θi, the SWA solution after T epochs is given by θSWA=1Ti=1Tθi. A high constant learning rate schedule ensures that SGD explores the set of possible solutions instead of simply converging to a single point in the weight space. Izmailov et al. (2018) argue that conventional SGD training converges to the boundary of the set of high-performing solutions; SWA on the other hand is able to find a more centered solution that is robust to the shift between train and test distributions, leading to improved generalization performance. SWA and related ideas have been successfully applied to a wide range of applications (see e.g. Athiwaratkun et al., 2019; Yazici et al., 2019; Nikishin et al., 2018).

A related but different procedure is Polyak-Ruppert averaging (Polyak & Juditsky, 1992; Ruppert, 1988) in stochastic convex optimization, which uses a learning rate decaying to zero. Burn-in periods and thinning are known to accelerate the convergence speed of Polyak-Ruppert averaging (Rakhlin et al., 2011), which provides some evidence for the effectiveness of SWA in practice.

\@xsect

We first consider a simple diagonal format for the covariance matrix. Diagonal covariance matrices are standard for variational inference and Laplace approximations in Bayesian deep learning (e.g., Blundell et al., 2015; Kirkpatrick et al., 2017). In order to fit a diagonal covariance approximation, we maintain a running average of the second uncentered moment for each weight, and then compute the covariance using the following standard identity at the end of training:

θ2¯=1Ti=1Tθi2,Σdiag=diag(θ2¯-θSWA2),

where the squares in θSWA2 and θi2 are applied elementwise. The resulting approximate posterior distribution is then 𝒩(θSWA,ΣDiag). In our experiments, we term this method SWAG-Diagonal.

Constructing the SWAG-Diagonal posterior approximation requires storing two additional copies of DNN weights: θSWA and θ2¯. Note that these models do not have to be stored on the GPU. The additional computational complexity of constructing SWAG-Diagonal compared to standard training is negligible, as it only requires updating the running averages of weights once per epoch.

Algorithm 1 Bayesian Model Averaging with SWAG
  θ0: pretrained weights; η: learning rate; T: number of steps; c: moment update frequency; K: maximum number of columns in deviation matrix; S: number of samples in Bayesian model averaging;
  Train SWAG
      θ¯θ0,θ2¯θ02\hfill{Initialize moments}
      for i1,2,,T do
          θiθi-1-ηθ(θi-1)\hfill{Perform SGD update}
          if 𝙼𝙾𝙳(i,c)=0 then
              ni/c\hfill{Number of models}
              θ¯nθ¯+θin+1,θ2¯nθ2¯+θi2n+1\hfill{Update moments}
              if 𝙽𝚄𝙼_𝙲𝙾𝙻𝚂(D^)=K then
                  𝚁𝙴𝙼𝙾𝚅𝙴_𝙲𝙾𝙻(D^[:,1])
              𝙰𝙿𝙿𝙴𝙽𝙳_𝙲𝙾𝙻(D^,θi-θ¯)\hfill{Store deviation}
      return θSWA=θ¯,Σdiag=θ2¯-θ¯2,D^
  Test Bayesian Model Averaging
      for i1,2,,S do
           Draw θ~i𝒩(θSWA,12Σdiag+D^D^2(K-1)) using (1)
           Update batch norm statistics with new sample.
           p(y*|Data)+=1Sp(y*|θ~i)
      return p(y*|Data)
\@xsect

While the diagonal covariance approximation is standard in Bayesian deep learning, it can be too restrictive. We extend the idea of SWAG-Diagonal to use a more flexible low-rank plus diagonal form of the approximate covariance.

Note that the sample covariance matrix can be written as the sum of outer products, Σ=1T-1i=1T(θi-θSWA)(θi-θSWA), and is of rank T. As we do not have access to the value of θSWA during training, we approximate the sample covariance with Σ1T-1i=1T(θi-θ¯i)(θi-θ¯i)=1T-1DD, where D is the deviation matrix comprised of columns Di=(θi-θ¯i), and θ¯i is the running estimate of the parameters’ mean obtained from the first i samples. In practice to limit the rank of the estimated covariance matrix we only use the last K of Di vectors corresponding to the last K epochs of training. Here K is the rank of the resulting approximation and is a hyperparameter of the method. We define D^ to be the matrix with columns equal to Di for i=T-K+1,,T.

We then combine the resulting low-rank approximation Σlow-rank=1K-1D^D^ with the diagonal approximation Σdiag of section id1. The resulting approximate posterior distribution is a Gaussian with the SWA mean θSWA and summed covariance: 𝒩(θSWA,12(Σdiag+Σlow-rank)).11 1 We use one half as the scale here because both the diagonal and low rank terms include the variance of the weights. We tested several other scales in Appendix id1. In our experiments, we term this method SWAG. Computing this approximate posterior distribution requires storing K vectors Di of the same size as the model as well as the vectors θSWA and θ2¯. These models do not have to be stored on a GPU.

Related methods for estimating the covariance of SGD iterates were considered in Mandt et al. (2017) and Chen et al. (2016), but store full-rank covariance Σ and thus scale quadratically in the number of parameters, which is prohibitively expensive for deep learning applications. We additionally note that using the deviation matrix for online covariance matrix estimation comes from viewing the online updates used in Dasgupta & Hsu (2007) in matrix fashion.

\@xsect

We sample from SWAG-diagonal by taking

θ~=θSWA+Σdiag12z1,z1𝒩(0,Id),

where d is the number of parameters in the network. Note that Σdiag is diagonal, and the product Σdiag12z1 can be computed in 𝒪(d) time. Similarly, to sample from SWAG we use the following identity

θ~=θSWA+12Σdiag12z1+12(K-1)D^z2, (1)
z1𝒩(0,Id),z2𝒩(0,IK).
\@xsect

In both cases, Bayesian predictions on the testing data, y*, using the approximate posterior, q, can now be made using Bayesian model averaging:

p(y*|Data) =𝔼p(θ|Data)(p(y*|θ))
1Si=1Sp(y*|θ~i),θ~iq(θ),

where q can be either the SWAG or SWAG-diagonal approximate posterior distribution, and S is the number of samples. The full Bayesian model averaging procedure for SWAG is described in Algorithm 1. We note that like Izmailov et al. (2018) (SWA) we update the batch normalization statistics after sampling weights for models that use batch normalization (Ioffe & Szegedy, 2015); an investigation into the necessity of this update is in Appendix id1.

\@xsect

Maximum a-posteriori (MAP) optimization is a procedure whereby one maximizes the (log) posterior with respect to parameters θ:

logp(θ|𝒟)=logp(𝒟|θ)+logp(θ). (2)

Here, the prior p(θ) is viewed as a regularizer in optimization. However, MAP is not Bayesian inference, since one only considers a single setting of the parameters θ^MAP=argmaxθp(θ|𝒟) in making predictions, forming p(y*|θ^MAP,x*), where x* and y* are test inputs and outputs.

A Bayesian procedure instead marginalizes the posterior distribution over θ, in a Bayesian model average, for the unconditional predictive distribution:

p(y*|𝒟,x*)=p(y*|θ,x*)p(θ|𝒟)𝑑θ. (3)

In practice, this integral is computed through a Monte Carlo sampling procedure:

p(y*|𝒟,x*)1Tt=1Tp(y*|θt,x*),θtp(θ|𝒟). (4)

We emphasize that we are performing full Bayesian inference, rather than MAP optimization. We develop a Gaussian approximation to the posterior from SGD iterates, p(θ|𝒟)𝒩(θ;μ,Σ), and then sample from this posterior distribution to perform a Bayesian model average. In our procedure, optimization with different regularizers, to characterize the Gaussian posterior approximation, corresponds to fully Bayesian inference with different priors p(θ).

Typically, weight decay is used to regularize DNNs, corresponding to explicit L2 regularization when SGD without momentum is used to train the model. When SGD is used with momentum, as is typically the case, implicit regularization still occurs, producing a vague prior on the weights of the DNN in our procedure. This regularizer can be given an explicit Gaussian-like form (see Proposition 3 of Loshchilov & Hutter (2019)), corresponding to a prior distribution on the weights. Thus, SWAG is an approximate Bayesian inference algorithm in our experiments (see Section id1) and can be applied to most DNNs without any modifications of the training procedure (as long as SGD is used with weight decay or explicit L2 regularization). Alternative regularization techniques could also be used, producing different priors on the weights.

\@xsect
Figure 1: Left: Posterior joint density cross-sections along the rays corresponding to different eigenvectors of SWAG covariance matrix. Middle: Posterior joint density surface in the plane spanned by eigenvectors of SWAG covariance matrix corresponding to the first and second largest eigenvalues and (Right:) the third and fourth largest eigenvalues. All plots are produced using PreResNet-164 on CIFAR-100. The SWAG distribution projected onto these directions fits the geometry of the posterior density remarkably well.

Standard training of deep neural networks (DNNs) proceeds by applying stochastic gradient descent on the model weights θ with the following update rule:

Δθt=-η(1Bi=1Bθ(logp(yi|fθ(xi)))-θlogp(θ)N),

where the learning rate is η, the ith input (e.g.  image) and label are {xi,yi}, the size of the whole training set is N, the size of the batch is B, and the DNN, f, has weight parameters θ.22 2 We ignore momentum for simplicity in this update; however the following theoretical results can be extended to include momentum. The loss function is a negative log likelihood -ilogp(yi|fθ(xi)), combined with a regularizer logp(θ). This type of maximum likelihood training does not represent uncertainty in the predictions or parameters θ.

\@xsect

Under conditions of decaying learning rates, smoothness of gradients, and the existence of a full rank stationary distribution, martingale based analyses of stochastic gradient descent (e.g., Asmussen & Glynn, 2007, Chapter 8) show that SGD has a Gaussian limiting distribution. That is, in the infinite time step limit, t1/2(θt-θ*)𝒩(0,R), where R is some covariance matrix and θ* is a stationary point or minima. Mandt et al. (2017), and Chen et al. (2016) are additional examples of this style of analysis. We focus on the assumptions of Mandt et al. (2017) in the next section. We note that these are essentially the same conditions as for the Bernstein von Mises Theorem (e.g., Vaart, 1998, Chapter 10) which shows that the asymptotic posterior will also be Gaussian asymptotically.

\@xsect

In this section, we investigate the results of Mandt et al. (2017) in the context of deep learning. Mandt et al. (2017) uses the following assumptions:

  1. 1.

    Gradient noise at each point θ is 𝒩(0,C).

  2. 2.

    C is independent of θ and full rank.

  3. 3.

    The learning rates, η, are small enough that we can approximate the dynamics of SGD by a continuous-time dynamic described by the corresponding stochastic differential equation.

  4. 4.

    In the stationary distribution, the loss is approximately quadratic near the optima, i.e. approximately (θ-θ*)(θ)(θ-θ*), where (θ*) is the Hessian at the optimum; further, the Hessian is assumed to be positive definite.

Assumption 1 is motivated by the central limit theorem, and Assumption 3 is necessary for the analysis in Mandt et al. (2017). Assumptions 2 and 4 may or may not hold for deep neural networks (as well as other models). Under these assumptions, Theorem 1 of Mandt et al. (2017) derives the optimal constant learning rate that minimizes the KL-divergence between the SGD stationary distribution and the posterior33 3 An optimal diagonal preconditioner is also derived; our empirical work applies to that setting as well. A similar analysis with momentum holds as well, adding in only the momentum coefficient.:

η*=2BNdtr(C), (5)

where N is the size of the dataset, d is the dimension of the model, B is the minibatch size and C is the gradient noise covariance.

We computed Equation 5 over the course of training for two neural networks in Figure A.3(a), finding that the predicted optimal learning rate was orders of magnitude larger than what would be used in practice to explore the loss surface in a reasonable time (about 4×105 compared to 0.1). See Appendix id1 for further details.

We now focus on seeing how Assumptions 2 and 4 fail for DNNs; this will give further insight into what portions of the theory do hold, and may give insights into a corrected version of the optimal learning rate.

\@xsect

In Figure A.3(b), the trace of the gradient noise covariance and thus the optimal learning rates are nearly constant; however, the total variance is much too small to induce effective learning rates, probably due to over-parameterization effects inducing non full rank gradient covariances as was found in Chaudhari & Soatto (2018). We note that this experiment is not sufficient to be fully confident that C is independent of the parameterization near the local optima, but rather that tr(C) is close to constant; further experiments in this vein are necessary to test if the diagonals of C are constant. The result that tr(C) is close to constant suggests that a constant learning rate could be used for sampling in a stationary phase of training. The dimensionality parameter in Equation 5 could be modified to use the number of effective parameters or the rank of the gradient noise to reduce the optimal learning rate to a feasible number.

\@xsect

To test assumption 4, we used a GPU-enabled Lanczos method from GPyTorch (Gardner et al., 2018) and used restarting to compute the minimum eigenvalue of the train loss of a pre-trained PreResNet-164 on CIFAR-100. We found that even at the end of training, the minimum eigenvalue was -272 (the maximum eigenvalue was 3580 for comparison), indicating that the Hessian is not positive definite. This result harmonizes with other work analyzing the spectra of the Hessian for DNN training (Li et al., 2018; Sagun et al., 2018). Further, Garipov et al. (2018) and Draxler et al. (2018) argue that the loss surfaces of DNNs have directions along which the loss is completely flat, suggesting that the loss is nowhere near a positive-definite quadratic form.

\@xsect

As we have seen in Section id1, the theoretical results of Mandt et al. (2017) may not directly apply to DNNs. This is slightly different from our intuition about the problem; we expect the SGD trajectory to contain information about the shape of the loss surface (and thus also the posterior distribution), because the loss surface informs how we step from one point to the next. In this section we empirically verify that SWAG can capture the local geometry of the posterior distribution.

In order to analyze the quality of the SWAG approximation, we study the posterior density along the directions corresponding to the eigenvectors of the SWAG covariance matrix for PreResNet-164 on CIFAR-100. In order to find these eigenvectors we use randomized SVD (Halko et al., 2011).44 4 From sklearn.decomposition.TruncatedSVD. In the left panel of Figure 1 we visualize the 2-regularized cross-entropy loss L() (equivalent to the joint density of the weights and the loss with a Gaussian prior) as a function of distance t from the SWA solution θSWA along the i-th eigenvector vi of the SWAG covariance:

ϕ(t)=L(θSWA+tvivi).

As we can see in the left panel of Figure 1, there is a clear correlation between the variance of the SWAG approximation and the width of the posterior along the directions vi. The SGD iterates indeed contain useful information about the shape of the posterior distribution, and SWAG is able to capture this information. We repeated the same experiment for SWAG-Diagonal, finding that there was almost no variance in these eigen-directions. Next, we plot the posterior density surface in the 2-dimensional plane in the weight space spanning the two top eigenvectors v1 and v2 of the SWAG covariance

ψ(t1,t2)=L(θSWA+t1v1v1+t2v2v2).

We plot the results in the middle panel of Figure 1. Again, we can see that SWAG is able to capture the geometry of the posterior. The contours of constant posterior density appear to be remarkably well aligned with the eigenvalues of the SWAG covariance. We also present the analogous plot for the third and fourth top eigenvectors in the right panel of Figure 1. In Appendix id1, we additionally present similar results for PreResNet-164 on CIFAR-10 and VGG-16 on CIFAR-100.

As we can see, SWAG is able to capture the geometry of the posterior in the subspace spanned by SGD iterates. However, the dimensionality of this subspace is very low compared to the dimensionality of the weight space, and we can not guarantee that SWAG variance estimates are adequate along all directions in weight space. In particular, we would expect SWAG to under-estimate the variances along random directions, as the SGD trajectory is in a low-dimensional subspace of the weight space, and a random vector has a close-to-zero projection on this subspace with high probability.

Figure 2: Negative log likelihoods for SWAG and baselines. Mean and standard deviation (shown with error-bars) over 3 runs are reported for each experiment on CIFAR datasets. SWAG (blue star) consistently outperforms alternatives, with lower negative log likelihood, with the largest improvements on transfer learning. Temperature scaling applied on top of SWA (SWA-Temp) often performs close to as well on the non-transfer learning tasks, but requires a validation set.
\@xsect

We conduct a thorough empirical evaluation of SWAG, comparing to a range of high performing baselines, including MC dropout (Gal & Ghahramani, 2016), temperature scaling (Guo et al., 2017), and Laplace approximations (Ritter et al., 2018b). In Section id1 we evaluate SWAG predictions and uncertainty estimates on image classification tasks. We also evaluate SWAG for transfer learning and out-of-domain data detection. We present experiments investigating the effect of hyperparameter choices and practical limitations in SWAG in Appendix id1.

\@xsect
Figure 3: Reliability diagrams for WideResNet28x10 on CIFAR-100 and transfer task; ResNet-152 and DenseNet-161 on ImageNet. Confidence is the value of the max softmax output. A perfectly calibrated network has no difference between confidence and accuracy, represented by a dashed black line. Points below this line correspond to under-confident predictions, whereas points above the line are overconfident predictions. SWAG is able to substantially improve calibration over standard training (SGD), as well as SWA. Additionally, SWAG significantly outperforms temperature scaling for transfer learning (CIFAR-10 to STL), where the target data are not from the same distribution as the training data.

In this section we evaluate the quality of uncertainty estimates as well as predictive accuracy for SWAG and SWAG-Diagonal and baselines on CIFAR-10, CIFAR-100 and ImageNet ILSVRC-2012 (Russakovsky et al., 2015).

For all methods we analyze test negative log-likelihood, which reflects both the accuracy and the quality of predictive uncertainty. Following Guo et al. (2017) we also consider a variant of reliability diagrams to evaluate the calibration of uncertainty estimates (see Figure 3) and to show the difference between a method’s confidence in its predictions and its accuracy. To produce this plot for a given method we split the test data into 20 bins uniformly based on the confidence of a method (maximum predicted probability). We then evaluate the accuracy and mean confidence of the method on the images from each bin, and plot the difference between confidence and accuracy. For a well-calibrated model, this difference should be close to zero for each bin. We found that this procedure gives a more effective visualization of the actual confidence distribution of DNN predictions than the standard reliability diagrams used in Guo et al. (2017) and Niculescu-Mizil & Caruana (2005).

We provide tables containing the test accuracy, negative log likelihood and expected calibration error for all methods and datasets in Appendix id1.

\@xsect

On CIFAR datasets we run experiments with VGG-16, PreResNet-164 and WideResNet-28x10 networks. In order to compare SWAG with existing alternatives we report the results for standard SGD and SWA (Izmailov et al., 2018) solutions (single models), MC-Dropout (Gal & Ghahramani, 2016), temperature scaling (Guo et al., 2017) applied to SWA and SGD solutions, and K-FAC Laplace (Ritter et al., 2018b) methods. For all the methods we use our implementations in PyTorch (see Appendix id1). We train all networks for 300 epochs, starting to collect models for SWA and SWAG approximations once per epoch after epoch 160. For SWAG, K-FAC Laplace, and Dropout we use 30 samples at test time.

\@xsect

On ImageNet we report our results for SWAG, SWAG-Diagonal, SWA and SGD. We run experiments with DenseNet-161 (Huang et al., 2017) and Resnet-152 (He et al., 2016). For each model we start from a pre-trained model available in the torchvision package, and run SGD with a constant learning rate for 10 epochs. We collect models for the SWAG versions and SWA 4 times per epoch. For SWAG we use 30 samples from the posterior over network weights at test-time, and use randomly sampled 10% of the training data to update batch-normalization statistics for each of the samples. For SGD with temperature scaling, we use the results reported in Guo et al. (2017).

We provide further details on the architectures and hyperparameter choices in Appendix id1.

\@xsect

We visualize the negative log-likelihood for all methods and datasets in Figure 2. On all considered tasks SWAG and SWAG diagonal perform comparably or better than all the considered alternatives, SWAG being best overall. We note that the combination of SWA and temperature scaling presents a competitive baseline. However, unlike SWAG it requires using a validation set to tune the temperature; further, temperature scaling is not effective when the test data distribution differs from train. We consider this setting in Sections id1 and id1.

Next, we analyze the calibration of uncertainty estimates provided by different methods. In Figure 3 we present reliability plots for WideResNet on CIFAR-100, DenseNet-161 and ResNet-152 on ImageNet. The reliability diagrams for all other datasets and architectures are presented in the Appendix id1. As we can see, SWAG and SWAG-Diagonal both achieve good calibration across the board. The low-rank plus diagonal version of SWAG is generally better calibrated than SWAG-Diagonal. We also present the expected calibration error for each of the methods, architectures and datasets in Table A.2.

Finally, in Table A.4 we present the predictive accuracy for all of the methods. SWAG verions generally perform comparably to SWA in terms of predictive accuracy and outperform the other methods that do not use SWA.

Table 1: Symmetrized, discretized KL divergence between the distributions of predictive entropies for data from the first and last five classes of CIFAR-10 for models trained only on the first five classes. The entropy distributions for SWAG are more different than the baseline models.
Method JS-Distance
SWAG 3.31
SWAG-Diag 2.27
MC Dropout 3.04
SWA 1.68
SGD (Baseline) 3.14
SGD + Temp. Scaling 2.98
\@xsect

We evaluate the uncertainties provided by SWAG, SWAG-Diagonal and baselines in the transfer-learning setting. We use the models trained on CIFAR-10 and evaluate them on STL-10 (Coates et al., 2011). STL-10 has a similar set of classes as CIFAR-10, but the image distribution is different, so adapting the model from CIFAR-10 to STL-10 is a commonly used benchmark in transfer learning.

The test negative log-likelihood for all methods is given in Figure 2, and Figure 3 contains the reliability diagram for WideResNet-28x10. As we can see, in transfer learning setting SWAG is significantly outperforming the existing alternatives both in terms of likelihood and calibration. In particular SWA with temperature scaling does not match the performance of SWAG, since the data distribution used to scale the temperature is different from the test distribution. See Appendix id1 for detailed results for all architectures.

\@xsect

Next, we evaluate the SWAG variants along with the baselines on out-of-sample data detection. To do so we train a WideResNet as described in section id1 on the data from five classes of the CIFAR-10 dataset, and then analyze their predictions on the full test set. We expect the outputted class probabilities on objects that belong to classes that were not present in the training data to have high-entropy reflecting the model’s high uncertainty in its predictions, and considerably lower entropy on the images that are similar to those on which the network was trained.

To make this comparison quantitative, we computed the symmetrized KL divergence between the binned in and out of sample distributions in Table 1, finding that SWAG and Dropout perform best on this measure. We plot the histograms of predictive entropies on the in-domain (classes that were trained on) and out-of-domain (classes that were not trained on) in Figure A.9 for a qualitative comparison.

\@xsect

In this paper we developed SWA-Gaussian (SWAG) for approximate Bayesian inference in deep learning. There has been a great desire to apply Bayesian methods in deep learning due to their theoretical properties and past success with small neural networks. We view SWAG as a step towards practical, scalable, and accurate Bayesian deep learning for large modern neural networks.

A key geometric observation in this paper is that the posterior distribution over neural network parameters is close to Gaussian in the subspace spanned by the trajectory of SGD. Further, this Gaussian distribution is remarkably well-aligned with the principal components (eigenvectors of sample covariance matrix) of the set of SGD iterates. The subspace spanned by SGD iterates is of interest for several reasons. First, our work shows that Bayesian model averaging within this subspace can improve predictions over SGD or SWA solutions. Second, Gur-Ari et al. (2019) argue that the SGD trajectory lies in the subspace spanned by the eigenvectors of the Hessian corresponding to the few top eigenvalues. This result suggests that we would expect the subspace containing SGD trajectory to correspond to directions of rapid change in the predictions of the model.

\@ssect

Acknowledgements WM, PI, and AGW were supported by an Amazon Research Award, Facebook Research, and NSF IIS-1563887. WM was additionally supported by an NSF Graduate Research Fellowship under Grant No. DGE-1650441. We would like to thank Jacob Gardner for helpful discussions.

References

  • Asmussen & Glynn (2007) Asmussen, S. and Glynn, P. W. Stochastic simulation: algorithms and analysis. Number 57 in Stochastic modelling and applied probability. Springer, New York, 2007. ISBN 978-0-387-30679-7 978-0-387-69033-9. OCLC: ocn123113652.
  • Athiwaratkun et al. (2019) Athiwaratkun, B., Finzi, M., Izmailov, P., and Wilson, A. G. There are many consistent explanations for unlabeled data: why you should average. In International Conference on Learning Representations, 2019. URL http://arxiv.org/abs/1806.05594. arXiv: 1806.05594.
  • Blier & Ollivier (2018) Blier, L. and Ollivier, Y. The Description Length of Deep Learning models. In Advances in Neural Information Processing Systems, pp.  11, 2018.
  • Blundell et al. (2015) Blundell, C., Cornebise, J., Kavukcuoglu, K., and Wierstra, D. Weight Uncertainty in Neural Networks. In International Conference on Machine Learning, 2015. URL http://arxiv.org/abs/1505.05424. arXiv: 1505.05424.
  • Chaudhari & Soatto (2018) Chaudhari, P. and Soatto, S. Stochastic gradient descent performs variational inference, converges to limit cycles for deep networks. In International Conference on Learning Representations, 2018. URL http://arxiv.org/abs/1710.11029. arXiv: 1710.11029.
  • Chen et al. (2014) Chen, T., Fox, E. B., and Guestrin, C. Stochastic Gradient Hamiltonian Monte Carlo. In International Conference on Machine Learning, 2014. URL http://arxiv.org/abs/1402.4102. arXiv: 1402.4102.
  • Chen et al. (2016) Chen, X., Lee, J. D., Tong, X. T., and Zhang, Y. Statistical Inference for Model Parameters in Stochastic Gradient Descent. arXiv: 1610.08637, October 2016. URL http://arxiv.org/abs/1610.08637.
  • Coates et al. (2011) Coates, A., Ng, A., and Lee, H. An Analysis of Single-Layer Networks in Unsupervised Feature Learning. In Proceedings of the Fourteenth International Conference on Artificial Intelligence and Statistics, pp. 215–223, June 2011. URL http://proceedings.mlr.press/v15/coates11a.html.
  • Dasgupta & Hsu (2007) Dasgupta, S. and Hsu, D. On-Line Estimation with the Multivariate Gaussian Distribution. In Bshouty, N. H. and Gentile, C. (eds.), Twentieth Annual Conference on Learning Theory., volume 4539, pp. 278–292, Berlin, Heidelberg, 2007. Springer Berlin Heidelberg. ISBN 978-3-540-72925-9. doi: 10.1007/978-3-540-72927-3˙21. URL http://link.springer.com/10.1007/978-3-540-72927-3_21.
  • Draxler et al. (2018) Draxler, F., Veschgini, K., Salmhofer, M., and Hamprecht, F. A. Essentially No Barriers in Neural Network Energy Landscape. In International Conference on Machine Learning, pp.  10, 2018.
  • Gal & Ghahramani (2016) Gal, Y. and Ghahramani, Z. Dropout as a Bayesian Approximation. In International Conference on Machine Learning, 2016. URL http://proceedings.mlr.press/v48/gal16.pdf.
  • Gal et al. (2017) Gal, Y., Hron, J., and Kendall, A. Concrete Dropout. In Advances in Neural Information Processing Systems, 2017. URL http://arxiv.org/abs/1705.07832. arXiv: 1705.07832.
  • Gardner et al. (2018) Gardner, J., Pleiss, G., Weinberger, K. Q., Bindel, D., and Wilson, A. G. Gpytorch: Blackbox matrix-matrix gaussian process inference with gpu acceleration. In Advances in Neural Information Processing Systems, pp. 7587–7597, 2018.
  • Garipov et al. (2018) Garipov, T., Izmailov, P., Podoprikhin, D., Vetrov, D., and Wilson, A. G. Loss Surfaces, Mode Connectivity, and Fast Ensembling of DNNs. In Advances in Neural Information Processing Systems, 2018. URL http://arxiv.org/abs/1802.10026. arXiv: 1802.10026.
  • Graves (2011) Graves, A. Practical variational inference for neural networks. In Advances in neural information processing systems, pp. 2348–2356, 2011.
  • Guo et al. (2017) Guo, C., Pleiss, G., Sun, Y., and Weinberger, K. Q. On Calibration of Modern Neural Networks. In International Conference on Machine Learning, June 2017. URL http://arxiv.org/abs/1706.04599. arXiv: 1706.04599.
  • Gur-Ari et al. (2019) Gur-Ari, G., Roberts, D. A., and Dyer, E. Gradient descent happens in a tiny subspace, 2019. URL https://openreview.net/forum?id=ByeTHsAqtX.
  • Halko et al. (2011) Halko, N., Martinsson, P.-G., and Tropp, J. A. Finding structure with randomness: Probabilistic algorithms for constructing approximate matrix decompositions. SIAM review, 53(2):217–288, 2011.
  • He et al. (2016) He, K., Zhang, X., Ren, S., and Sun, J. Deep Residual Learning for Image Recognition. In CVPR, 2016. URL http://arxiv.org/abs/1512.03385. arXiv: 1512.03385.
  • Huang et al. (2017) Huang, G., Liu, Z., van der Maaten, L., and Weinberger, K. Q. Densely Connected Convolutional Networks. In CVPR, 2017. URL http://arxiv.org/abs/1608.06993. arXiv: 1608.06993.
  • Ioffe & Szegedy (2015) Ioffe, S. and Szegedy, C. Batch normalization: Accelerating deep network training by reducing internal covariate shift. arXiv preprint arXiv:1502.03167, 2015.
  • Izmailov et al. (2018) Izmailov, P., Podoprikhin, D., Garipov, T., Vetrov, D., and Wilson, A. G. Averaging Weights Leads to Wider Optima and Better Generalization. In Uncertainty in Artificial Intelligence, 2018, 2018. URL http://arxiv.org/abs/1803.05407. arXiv: 1803.05407.
  • Kendall & Gal (2017) Kendall, A. and Gal, Y. What Uncertainties Do We Need in Bayesian Deep Learning for Computer Vision? In Advances in Neural Information Processing Systems, Long Beach, 2017. URL https://arxiv.org/pdf/1703.04977.pdf.
  • Keskar et al. (2017) Keskar, N. S., Mudigere, D., Nocedal, J., Smelyanskiy, M., and Tang, P. T. P. On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima. In International Conference on Learning Representations, 2017. URL http://arxiv.org/abs/1609.04836. arXiv: 1609.04836.
  • Khan et al. (2018) Khan, M. E., Nielsen, D., Tangkaratt, V., Lin, W., Gal, Y., and Srivastava, A. Fast and Scalable Bayesian Deep Learning by Weight-Perturbation in Adam. In International Conference on Machine Learning, 2018. URL http://arxiv.org/abs/1806.04854. arXiv: 1806.04854.
  • Kingma & Welling (2013) Kingma, D. P. and Welling, M. Auto-encoding variational bayes. In International Conference on Learning Representations, 2013.
  • Kingma et al. (2015a) Kingma, D. P., Salimans, T., and Welling, M. Variational dropout and the local reparameterization trick. In Advances in Neural Information Processing Systems, pp. 2575–2583, 2015a.
  • Kingma et al. (2015b) Kingma, D. P., Salimans, T., and Welling, M. Variational Dropout and the Local Reparameterization Trick. arXiv:1506.02557 [cs, stat], June 2015b. URL http://arxiv.org/abs/1506.02557. arXiv: 1506.02557.
  • Kirkpatrick et al. (2017) Kirkpatrick, J., Pascanu, R., Rabinowitz, N., Veness, J., Desjardins, G., Rusu, A. A., Milan, K., Quan, J., Ramalho, T., Grabska-Barwinska, A., et al. Overcoming catastrophic forgetting in neural networks. Proceedings of the national academy of sciences, pp. 201611835, 2017.
  • Kuleshov et al. (2018) Kuleshov, V., Fenner, N., and Ermon, S. Accurate Uncertainties for Deep Learning Using Calibrated Regression. In International Conference on Machine Learning, pp.  9, 2018.
  • Lakshminarayanan et al. (2017) Lakshminarayanan, B., Pritzel, A., and Blundell, C. Simple and Scalable Predictive Uncertainty Estimation using Deep Ensembles. In Advances in Neural Information Processing Systems, 2017.
  • Li et al. (2018) Li, H., Xu, Z., Taylor, G., Studer, C., and Goldstein, T. Visualizing the Loss Landscape of Neural Nets. In Advances in Neural Information Processing Systems, 2018. URL http://arxiv.org/abs/1712.09913. arXiv: 1712.09913.
  • Loshchilov & Hutter (2019) Loshchilov, I. and Hutter, F. Decoupled Weight Decay Regularization. In International Conference on Learning Representations, 2019. URL http://arxiv.org/abs/1711.05101. arXiv: 1711.05101.
  • Louizos & Welling (2017) Louizos, C. and Welling, M. Multiplicative normalizing flows for variational bayesian neural networks. In International Conference on Machine Learning, 2017.
  • MacKay (1992a) MacKay, D. J. C. Bayesian Interpolation. Neural Computation, 1992a. URL https://www.mitpressjournals.org/doi/pdf/10.1162/neco.1992.4.3.415.
  • MacKay (1992b) MacKay, D. J. C. A Practical Bayesian Framework for Backpropagation Networks. Neural Computation, 4(3):448–472, May 1992b. ISSN 0899-7667, 1530-888X. doi: 10.1162/neco.1992.4.3.448. URL http://www.mitpressjournals.org/doi/10.1162/neco.1992.4.3.448.
  • MacKay (2003) MacKay, D. J. C. Information theory, inference, and learning algorithms. Cambridge University Press, Cambridge, UK ; New York, 2003. ISBN 978-0-521-64298-9.
  • Maddox et al. (2018) Maddox, W., Garipov, T., Izmailov, P., Vetrov, D., and Wilson, A. G. Fast uncertainty estimates and Bayesian model averaging of DNNs. Uncertainty in Deep Learning Workshop at UAI, 2018.
  • Mandt et al. (2017) Mandt, S., Hoffman, M. D., and Blei, D. M. Stochastic Gradient Descent as Approximate Bayesian Inference. JMLR, 18:1–35, 2017.
  • Molchanov et al. (2017) Molchanov, D., Ashukha, A., and Vetrov, D. Variational dropout sparsifies deep neural networks. arXiv preprint arXiv:1701.05369, 2017.
  • Mukhoti & Gal (2018) Mukhoti, J. and Gal, Y. Evaluating Bayesian Deep Learning Methods for Semantic Segmentation, November 2018. URL https://arxiv.org/abs/1811.12709v1.
  • Naeini et al. (2015) Naeini, M. P., Cooper, G. F., and Hauskrecht, M. Obtaining well calibrated probabilities using bayesian binning. In AAAI, pp. 2901–2907, 2015.
  • Neal (1996) Neal, R. M. Bayesian Learning for Neural Networks, volume 118 of Lecture Notes in Statistics. Springer New York, New York, NY, 1996. ISBN 978-0-387-94724-2 978-1-4612-0745-0. URL http://link.springer.com/10.1007/978-1-4612-0745-0.
  • Niculescu-Mizil & Caruana (2005) Niculescu-Mizil, A. and Caruana, R. Predicting good probabilities with supervised learning. In International Conference on Machine Learning, pp. 625–632, Bonn, Germany, 2005. ACM Press. ISBN 978-1-59593-180-1. doi: 10.1145/1102351.1102430. URL http://portal.acm.org/citation.cfm?doid=1102351.1102430.
  • Nikishin et al. (2018) Nikishin, E., Izmailov, P., Athiwaratkun, B., Podoprikhin, D., Garipov, T., Shvechikov, P., Vetrov, D., and Wilson, A. G. Improving Stability in Deep Reinforcement Learning with Weight Averaging. In Uncertainty in Artificial Intelligence Workshop on Uncertainty in Deep Learning, pp.  5, 2018.
  • Polyak & Juditsky (1992) Polyak, B. T. and Juditsky, A. B. Acceleration of Stochastic Approximation by Averaging. SIAM Journal on Control and Optimization, 30(4):838–855, July 1992. ISSN 0363-0129, 1095-7138. doi: 10.1137/0330046. URL http://epubs.siam.org/doi/10.1137/0330046.
  • Rakhlin et al. (2011) Rakhlin, A., Shamir, O., and Sridharan, K. Making Gradient Descent Optimal for Strongly Convex Stochastic Optimization. In International Conference on Machine Learning, September 2011. URL http://arxiv.org/abs/1109.5647. arXiv: 1109.5647.
  • Ritter et al. (2018a) Ritter, H., Botev, A., and Barber, D. Online Structured Laplace Approximations For Overcoming Catastrophic Forgetting. In Advances in Neural Information Processing Systems, 2018a. URL http://arxiv.org/abs/1805.07810. arXiv: 1805.07810.
  • Ritter et al. (2018b) Ritter, H., Botev, A., and Barber, D. A Scalable Laplace Approximation for Neural Networks. In International Conference on Learning Representations, 2018b.
  • Ruppert (1988) Ruppert, D. Efficient Estimators from a Slowly Convergent Robbins-Munro Process. Technical Report 781, Cornell University, School of Operations Report and Industrial Engineering, 1988. URL https://ecommons.cornell.edu/bitstream/handle/1813/8664/TR000781.pdf?sequence=1&isAllowed=y.
  • Russakovsky et al. (2015) Russakovsky, O., Deng, J., Su, H., Krause, J., Satheesh, S., Ma, S., Huang, Z., Karpathy, A., Khosla, A., Bernstein, M., Berg, A. C., and Fei-Fei, L. ImageNet Large Scale Visual Recognition Challenge. IJCV, 115(3):211–252, 2015. URL http://arxiv.org/abs/1409.0575. arXiv: 1409.0575.
  • Saatci & Wilson (2017) Saatci, Y. and Wilson, A. G. Bayesian gan. In Guyon, I., Luxburg, U. V., Bengio, S., Wallach, H., Fergus, R., Vishwanathan, S., and Garnett, R. (eds.), Advances in Neural Information Processing Systems 30, pp. 3622–3631. Curran Associates, Inc., 2017. URL http://papers.nips.cc/paper/6953-bayesian-gan.pdf.
  • Sagun et al. (2018) Sagun, L., Evci, U., Guney, V. U., Dauphin, Y., and Bottou, L. Empirical Analysis of the Hessian of Over-Parametrized Neural Networks. In International Conference on Learning Representations Workshop Track, 2018. URL http://arxiv.org/abs/1706.04454. arXiv: 1706.04454.
  • Vaart (1998) Vaart, A. W. v. d. Asymptotic Statistics. Cambridge Series in Statistical and Probabilistic Mathematics. Cambridge University Press, Cambridge, 1998. ISBN 978-0-521-78450-4. doi: 10.1017/CBO9780511802256. URL https://www.cambridge.org/core/books/asymptotic-statistics/A3C7DAD3F7E66A1FA60E9C8FE132EE1D.
  • Yazici et al. (2019) Yazici, Y., Foo, C.-S., Winkler, S., Yap, K.-H., Piliouras, G., and Chandrasekhar, V. The Unusual Effectiveness of Averaging in GAN Training. In International Conference on Learning Representations, 2019. URL http://arxiv.org/abs/1806.04498. arXiv: 1806.04498.
  • Zagoruyko & Komodakis (2016) Zagoruyko, S. and Komodakis, N. Wide Residual Networks. In BMVC, 2016. URL http://arxiv.org/abs/1605.07146. arXiv: 1605.07146.
  • Zhang et al. (2017) Zhang, G., Sun, S., Duvenaud, D., and Grosse, R. Noisy Natural Gradient as Variational Inference. arXiv:1712.02390 [cs, stat], December 2017. URL http://arxiv.org/abs/1712.02390. arXiv: 1712.02390.
\@xsect

To estimate tr(C) from the gradient noise we need to divide the estimated variance by the batch size (as V(g^(θ))=BC(θ)), for a correct version of Equation 5. From Assumption 1 and Equation 6 of Mandt et al. (2017), we see that

g^(θ)g(θ)+1Bg(θ),g(θ)N(0,C(θ)),

where B is the batch size. Thus, collecting the variance of g^(θ) (the variance of the stochastic gradients) will give estimates that are upscaled by a factor of B:

η2B2Ndtr(V(g^(θ))).

To include momentum, we can repeat the analysis in Sections 4.1 and 4.3 Mandt et al. (2017) finding that this also involves scaling the optimal learning rate but by a factor of μ, the momentum term. This gives the final optimal learning rate equation as

η2μB2Ndtr(V(g^(θ))). (6)

In Figure 3(b), we computed tr(C) for VGG-16 and PreResNet-164 on CIFAR-100 beginning from the start of training (referred to as from scratch), as well as the start of the SWAG procedure (referred to in the legend as SWA). We see that tr(C) is never quite constant when trained from scratch, while for a period of constant learning rate near the end of training, referred to as the stationary phase, tr(C) is essentially constant throughout. This discrepancy is likely due to large gradients at the very beginning of training, indicating that the stationary distribution has not been reached yet.

Next, in Figure 3(a), we used the computed tr(C) estimate for all four models and Equation 6 to compute the optimal learning rate under the assumptions of Mandt et al. (2017), finding that these learning rates are not constant for the estimates beginning at the start of training and that they are always way too large (3,000 at the minimum compared to a standard learning rate of 0.1).

(a) Optimal learning rate.
(b) tr(C)
Figure 4: Gradient variance norm and computed optimal learning rates for VGG-16 and PreResNet-164. The computed optimal learning rates are always too large by a factor of 104, while the gradient variance stabilizes over the course of training.
\@xsect

In Figure 5 we present plots analogous to those in Section id1 for PreResNet-110 and VGG-16 on CIFAR-10 and CIFAR-100. For all dataset-architecture pairs we see that SWAG is able to capture the geometry of the posterior in the subspace spanned by SGD trajectory.

Figure 5: Left: Posterior-density cross-sections along the rays corresponding to different eigenvectors of SWAG covariance matrix. Middle: Posterior-density surface in the plane spanned by eigenvectors of SWAG covariance matrix corresponding to the first and second largest eigenvalues and (Right:) the third and fourth largest eigenvalues. Each row in the figure corresponds to an architecture-dataset pair indicated in the title of each panel.
\@xsect

In this section, we discuss the hyper-parameters in SWAG, as well as some current theoretical limitations.

\@xsect
(a)
(b)
(c)
(d)
Figure 6: (a) 30 samples of SWAG with a rank 20 covariance matches the SWA result over the course of training for PreResNet56 on CIFAR-100. SWAG with a rank of 140, SWAG with a rank of 20, and SWA all outperform ensembles of SGD iterates from the SWA procedure and from a standard SGD training path. (b) NLL and (c) accuracy by number of samples for WideResNet on CIFAR-100 for SWAG, SWAG-Diag, and SWA. 30 samples is adequate for stable accuracies and NLLs. (d) NLL by number of samples for different scales for WideResNet on CIFAR-100 for SWAG, SWAG-Diag, and SWA. Scales beneath 1 perform better, with 0.5 and 0.25 best.

We now evaluate the effect of the covariance matrix rank on the SWAG approximation. To do so, we trained a PreResNet56 on CIFAR-100 with SWAG beginning from epoch 161, and evaluated 30 sample Bayesian model averages obtained at different epochs; the accuracy plot from this experiment is shown in Figure 6 (a). The rank of each model after epoch 161 is simply min(epoch-161,140), and we can see that the 30 samples from even a low rank approximation reach the same predictive accuracy as the SWA model. Interestingly, both SWAG and SWA outperform ensembles of a SGD run and ensembles of the SGD models in the SWA run.

\@xsect

In most situations where SWAG will be used, no closed form expression for the integral f(y)q(θ|y)𝑑θ, will exist. Thus, Monte Carlo approximations will be used; Monte Carlo integration converges at a rate of 1/K, where K is the number of samples used, but practically good results may be found with very few samples (e.g. Chapter 29 of MacKay (2003)).

To test how many samples are needed for good predictive accuracy in a Bayesian model averaging task, we used a rank 20 approximation for SWAG and then tested the NLL on the test set as a function of the number of samples for WideResNet28x10 (Zagoruyko & Komodakis, 2016) on CIFAR-100.

The results from this experiment are shown in Figure 6 (b, c), where it is possible to see that about 3 samples will match the SWA result for NLL, with about 30 samples necessary for stable accuracy (about the same as SWA for this network). In most of our experiments, we used 30 samples for consistency. In practice, we suggest tuning this number by looking at a validation set as well as the computational resources available and comparing to the free SWA predictions that come with SWAG.

\@xsect

First, we note that the covariance, Σ, estimated using SWAG, is a function of the learning rate (and momentum) for SGD. While the theoretical work of Mandt et al. (2017) suggests that it is possible to optimally set the learning rate, our experiments in Section id1 show that currently the assumptions of the theory do not match the empirical reality in deep learning. In the linear setting as in Mandt et al. (2017), the learning rate controls the scale of the asymptotic covariance matrix. If the optimal learning rate (Equation 5) is used in this setting, the covariance matches the true posterior. To attempt to disassociate the learning rate from the covariance in practice, we rescale the covariance matrix when sampling by a constant factor for a WideResNet on CIFAR-100 shown in Figure 6 (d).

Over several replications, we found that a scale of 0.5 worked best, which is expected because the low rank plus diagonal covariance incorporates the variance twice (once for the diagonal component and once from the low rank component).

\@xsect

One possible slowdown of SWAG at inference time is in the usage of updated batch norm parameters. Following Izmailov et al. (2018), we found that in order for the averaging and sampling to work well, it was necessary to update the batch norm parameters of networks after sampling a new model. This is shown in Figure 7 for a WideResNet on CIFAR-100 for two independently trained models.

Figure 7: NLL by number of samples for SWAG with and without batch norm updates after sampling. Updating the batch norm parameters after sampling results in a significant improvement in NLL.
\@xsect

From our experimental findings, we see that given an equal amount of training time, SWAG typically outperforms other methods for uncertainty calibration. SWAG additionally does not require a validation set like temperature scaling and Platt scaling (e.g. Guo et al. (2017); Kuleshov et al. (2018)). SWAG also appears to have a distinct advantage over temperature scaling, and other popular alternatives, when the target data are from a different distribution than the training data, as shown by our transfer learning experiments.

Deep ensembles (Lakshminarayanan et al., 2017) require several times longer training for equal calibration, but often perform somewhat better due to incorporating several independent training runs. Thus SWAG will be particularly valuable when training time is limited, but inference time may not be. One possible application is thus in medical applications when image sizes (for semantic segmentation) are large, but predictions can be parallelized and may not have to be instantaneous.

\@xsect
Figure 8: Reliability diagrams (see Section id1) for all models and datasets. The dataset and architecture are listed in the title of each panel.
Figure 9: In and out of sample entropy distributions for WideResNet28x10 on CIFAR5 + 5.
\@xsect

In this section we describe all of the architectures and hyper-parameters we use in Sections id1, id1.

On ImageNet we use architecture implementations and pre-trained weights from https://github.com/pytorch/vision/tree/master/torchvision. For the experiments on CIFAR datasets we adapted the following implementations:

For all datasets and architectures we use the same piecewise constant learning rate schedule and weight decay as in Izmailov et al. (2018), except we train Pre-ResNet for 300 epochs and start averaging after epoch 160 in SWAG and SWA. For all of the methods we are using our own implementations in PyTorch. We describe the hyper-parameters for all experiments for each model:

\@xsect

We use the same hyper-parameters as Izmailov et al. (2018) on CIFAR datasets. On ImageNet we used a constant learning rate of 10-3 instead of the cyclical schedule, and averaged 4 models per epoch. We adapt the code from https://github.com/timgaripov/swa for our implementation of SWA.

\@xsect

In all experiments we use rank K=20 and use 30 weight samples for Bayesian model averaging. We re-use all the other hyper-parameters from SWA.

\@xsect

For our implementation we adapt the code for KFAC Fisher approximation from https://github.com/Thrandis/EKFAC-pytorch and implement our own code for sampling. Following (Ritter et al., 2018b) we tune the scale of the approximation on validation set for every model and dataset.

\@xsect

In order to implement MC-dropout we add dropout layers before each weight layer and sample 30 different dropout masks for Bayesian model averaging at inference time. To choose the learning rate, we ran the models with drop out rates in the set {0.1,0.05,0.01} and chose the one that performed best on validation data. For both VGG-16 and WideResNet28x10 we found that dropout rate of 0.05 worked best and used it in all experiments. On PreResNet-164 we couldn’t achieve reasonable performance with any of the three dropout rates, which has been reported from the work of He et al. (2016). We report the results for MC-Dropout in combination with both SWA (SWA-Drop) and SGD (SGD-Drop) training.

\@xsect

For SWA and SGD solutions we picked the optimal temperature by minimizing negative log-likelihood on validation data, adapting the code from https://github.com/gpleiss/temperature_scaling.

On CIFAR datasets for tuning hyper-parameters we used the last 5000 training data points as a validation set. On ImageNet we used 5000 of test data points for validation. On the transfer task for CIFAR10 to STL10, we report accuracy on all 10 STL10 classes even though frogs are not a part of the STL10 test set (and monkeys are not a part of the CIFAR10 training set).

\@xsect

We provide test accuracies and negative log-likelihoods (NLL) for all methods and datasets in Tables 3 and 4 respectively. We observe that SWAG is competitive with SWA, SWA with temperature scaling and SWA-Dropout in terms of test accuracy, and typically outperforms all the baselines in terms of NLL. SWAG-Diagonal is generally inferior to SWAG for log-likelihood, but outperforms SWA.

In Table 2 we additionally report expected calibration error (ECE, Naeini et al., 2015), a metric of calibration of the predictive uncertainties. To compute ECE for a given model we split the test points into 20 bins based on the confidence of the model, and we compute the absolute value of the difference of the average confidence and accuracy within each bin, and average the obtained values over all bins. Please refer to (Naeini et al., 2015; Guo et al., 2017) for more details. We observe that SWAG is competitive with temperature scaling for ECE. Again, SWAG-Diagonal achieves better calibration than SWA, but using the low-rank plus diagonal covariance approximation in SWAG leads to substantially improved performance.

\@xsect

We provide the additional reliability diagrams for all methods and datasets in Figure 8. SWAG consistently improves calibration over SWA, and performs on par or better than temperature scaling. In transfer learning temperature scaling fails to achieve good calibration, while SWAG still provides a significant improvement over SWA.

\@xsect

Table 1 shows the computed symmetrized, discretized KL distance between in and out of sample distributions for the CIFAR5 out of sample image detection class. We used the same bins as in Figure 9 to discretize the entropy distributions, then smoothed these bins by a factor of 1e-7 before calculating KL(IN||OUT)+KL(OUT||IN) using the scipy.stats.entropy function. We can see even qualitatively that the distributions are more distinct for SWAG and SWAG-Diagonal than for the other methods, particularly temperature scaling.

Table 2: ECE for various versions of SWAG, temperature scaling, and dropout.
Dataset Model SGD SWA SWAG-Diag SWAG KFAC-Laplace SWA-Dropout SWA-Temp
CIFAR-10 VGG-16 0.0483±0.0022 0.0408±0.0019 0.0267±0.0025 0.0158±0.0030 0.0094±0.0005 0.0284±0.0036 0.0366±0.0063
CIFAR-10 PreResNet-164 0.0255±0.0009 0.0203±0.0010 0.0082±0.0008 0.0053±0.0004 0.0092±0.0018 0.0162±0.0000 0.0172±0.0010
CIFAR-10 WideResNet28x10 0.0166±0.0007 0.0087±0.0002 0.0047±0.0013 0.0088±0.0006 0.0060±0.0003 0.0094±0.0014 0.0080±0.0007
CIFAR-100 VGG-16 0.1870±0.0014 0.1514±0.0032 0.0819±0.0021 0.0395±0.0061 0.0778±0.0054 0.1108±0.0181 0.0291±0.0097
CIFAR-100 PreResNet-164 0.1012±0.0009 0.0700±0.0056 0.0239±0.0047 0.0587±0.0048 0.0158±0.0014 0.0175±0.0037
CIFAR-100 WideResNet28x10 0.0479±0.0010 0.0684±0.0022 0.0322±0.0018 0.0113±0.0020 0.0379±0.0047 0.0574±0.0028 0.0220±0.0007
ImageNet DenseNet-161 0.0545±0.0000 0.0509±0.0000 0.0459±0.0000 0.0204±0.0000 0.0190±0.0000
ImageNet ResNet-152 0.0478±0.0000 0.0605±0.0000 0.0566±0.0000 0.0279±0.0000 0.0183±0.0000
CIFAR10 STL10 VGG-16 0.2149±0.0027 0.2082±0.0056 0.1719±0.0075 0.1463±0.0075 0.1803±0.0024 0.2089±0.0055
CIFAR10 STL10 PreResNet-164 0.1758±0.0000 0.1739±0.0000 0.1312±0.0000 0.1110±0.0000 0.1646±0.0000
CIFAR10 STL10 WideResNet28x10 0.1561±0.0000 0.1413±0.0000 0.1241±0.0000 0.1017±0.0000 0.1421±0.0000 0.1371±0.0000
Table 3: NLL for various versions of SWAG, temperature scaling, and dropout.
Dataset Model SGD SWA SWAG-Diag SWAG KFAC-Laplace SWA-Dropout SWA-Temp
CIFAR-10 VGG-16 0.3285±0.0139 0.2621±0.0104 0.2200±0.0078 0.2016±0.0031 0.2252±0.0032 0.2328±0.0049 0.2481±0.0245
CIFAR-10 PreResNet-164 0.1814±0.0025 0.1450±0.0042 0.1251±0.0029 0.1232±0.0022 0.1471±0.0012 0.1270±0.0000 0.1347±0.0038
CIFAR-10 WideResNet28x10 0.1294±0.0022 0.1075±0.0004 0.1077±0.0009 0.1122±0.0009 0.1210±0.0020 0.1094±0.0021 0.1064±0.0004
CIFAR-100 VGG-16 1.7308±0.0137 1.2780±0.0051 1.0163±0.0032 0.9480±0.0038 1.1915±0.0199 1.1872±0.0524 1.0386±0.0126
CIFAR-100 PreResNet-164 0.9465±0.0191 0.7370±0.0265 0.6837±0.0186 0.7081±0.0162 0.7881±0.0025 0.6770±0.0191
CIFAR-100 WideResNet28x10 0.7958±0.0089 0.6684±0.0034 0.6150±0.0029 0.6078±0.0006 0.7692±0.0092 0.6500±0.0049 0.6134±0.0023
ImageNet DenseNet-161 0.9094±0.0000 0.8655±0.0000 0.8559±0.0000 0.8303±0.0000 0.8359±0.0000
ImageNet ResNet-152 0.8716±0.0000 0.8682±0.0000 0.8584±0.0000 0.8205±0.0000 0.8226±0.0000
CIFAR10 STL10 VGG-16 1.6528±0.0390 1.3993±0.0502 1.2258±0.0446 1.1402±0.0342 1.3133±0.0000 1.4082±0.0506
CIFAR10 STL10 PreResNet-164 1.4790±0.0000 1.3552±0.0000 1.0700±0.0000 0.9706±0.0000 1.2228±0.0000
CIFAR10 STL10 WideResNet28x10 1.1308±0.0000 1.0047±0.0000 0.9340±0.0000 0.8710±0.0000 0.9914±0.0000 0.9706±0.0000
Table 4: Accuracy for various versions of SWAG, temperature scaling, and dropout.
Dataset Model SGD SWA SWAG-Diag SWAG KFAC-Laplace SWA-Dropout SWA-Temp
CIFAR-10 VGG-16 93.17±0.14 93.61±0.11 93.66±0.15 93.60±0.10 92.65±0.20 93.23±0.36 93.61±0.11
CIFAR-10 PreResNet-164 95.49±0.06 96.09±0.08 96.03±0.10 96.03±0.02 95.49±0.06 96.18±0.00 96.09±0.08
CIFAR-10 WideResNet28x10 96.41±0.10 96.46±0.04 96.41±0.05 96.32±0.08 96.17±0.00 96.39±0.09 96.46±0.04
CIFAR-100 VGG-16 73.15±0.11 74.30±0.22 74.68±0.22 74.77±0.09 72.38±0.23 72.50±0.54 74.30±0.22
CIFAR-100 PreResNet-164 78.50±0.32 80.19±0.52 80.18±0.50 79.90±0.50 78.51±0.05 80.19±0.52
CIFAR-100 WideResNet28x10 80.76±0.29 82.40±0.16 82.40±0.09 82.23±0.19 80.94±0.41 82.30±0.19 82.40±0.16
ImageNet DenseNet-161 77.79±0.00 78.60±0.00 78.59±0.00 78.59±0.00 78.60±0.00
ImageNet ResNet-152 78.39±0.00 78.92±0.00 78.96±0.00 79.08±0.00 78.92±0.00
CIFAR10 STL10 VGG-16 72.42±0.07 71.92±0.01 72.09±0.04 72.19±0.06 71.45±0.11 71.92±0.01
CIFAR10 STL10 PreResNet-164 75.56±0.00 76.02±0.00 75.95±0.00 75.88±0.00 76.02±0.00
CIFAR10 STL10 WideResNet28x10 76.75±0.00 77.50±0.00 77.26±0.00 77.09±0.00 76.91±0.00 77.50±0.00