Facilitating Bayesian Continual Learning by Natural Gradients and Stein Gradients
FFacilitating Bayesian Continual Learning by NaturalGradients and Stein Gradients
Yu Chen ∗ University of Bristol [email protected]
Tom Diethe
Amazon [email protected]
Neil Lawrence
Amazon [email protected]
Abstract
Continual learning aims to enable machine learning models to learn a generalsolution space for past and future tasks in a sequential manner. Conventionalmodels tend to forget the knowledge of previous tasks while learning a new task,a phenomenon known as catastrophic forgetting . When using Bayesian models incontinual learning, knowledge from previous tasks can be retained in two ways:(i) posterior distributions over the parameters, containing the knowledge gainedfrom inference in previous tasks, which then serve as the priors for the followingtask; (ii) coresets , containing knowledge of data distributions of previous tasks.Here, we show that Bayesian continual learning can be facilitated in terms of thesetwo means through the use of natural gradients and Stein gradients respectively.
There are several existing approaches for preventing catastrophic forgetting of regular (non-Bayesian) Neural Networks (NNs) by constructing a regularization term from parameters of pre-vious tasks, such as Elastic Weight Consolidation (EWC) [1] and Synaptic Intelligence (SI) [2]. Inthe Bayesian setting, Variational Continual Learning (VCL) [3] proposes a framework that makesuse of Variational Inference (VI). L VCL ( θ ) = E q t ( θ ) [log p ( D t | θ )] − KL ( q t ( θ ) (cid:107) q t − ( θ )) . (1)The objective function is as in Equation (1), where t is the index of tasks, q t ( θ ) represents theapproximated posterior of parameters θ of task t and D t is the data of task t . This is the sameas the objective of conventional VI except the prior is from the previous task, which produces theregularization by Kullback-Leibler (KL)-divergence between parameters of current and previoustasks. In addition, VCL [3] proposes a predictive model trained by coresets of seen tasks to performsprediction for those tasks, where the coresets consist of data samples from the dataset of each seentask except the training data D t of each task. ˆ q t = arg max q t E q t ( θ ) [log p ( C t | θ )] − KL ( q t ( θ ) (cid:107) q ∗ t ( θ )) . (2)As shown in Equation (2), C t = { c , c , . . . , c t } represents the collection of coresets at task t and q ∗ t ( θ ) is the optimal posterior obtained by Equation (1). VCL shows promising performancecomparing to EWC [1] and SI [2], which demonstrates that effectiveness of Bayesian approaches tocontinual learning. In order to prevent catastrophic forgetting in Bayesian continual learning, we would prefer the pos-teriors of a new task stay as close as possible to the posteriors of the previous task. Conventional gra-dient methods give the direction of steepest descent of parameters in Euclidean space, which might ∗ This work was conducted during Yu Chen’s internship at Amazon.32nd Conference on Neural Information Processing Systems (NeurIPS 2018), Montr´eal, Canada. a r X i v : . [ c s . L G ] A p r ause a large difference in terms of distributions following a small change in terms of parameters.We posit that natural gradient methods may be a better choice than the conventional gradient de-scent. The definition of the natural gradient is the direction of steepest descent in Riemannian spacerather than Euclidean space, which means the natural gradient would prefer the smallest change interms of distribution while optimizing some objective function [4]. The formulation is as below: ˆ ∇L ( β ) = ∇L ( β ) E θ (cid:104) ( ∇ log p ( θ | β )) T ( ∇ log p ( θ | β )) (cid:105) − def = ∇L ( β ) F − β , (3)where F β is the Fisher information of β . Specifically, when the posterior of a parameter θ is from the exponential family, we can write it in theform of p ( θ | β ) = h ( θ ) exp { η ( β ) T u ( θ ) − a ( η ( β )) } , where h ( · ) is the base measure and a ( · ) is log-normalizer, η ( · ) is natural parameter and u ( · ) are sufficient statistics. Then the Fisher informationof β is the covariance of the sufficient statistics which is the second derivative of a ( · ) [5]: F β = E q [( u ( θ ) − E q [ u ( θ )]) T ( u ( θ ) − E q [ u ( θ )])] = ∇ β a ( η ( β )) . (4)In this case, the natural gradient of β is the transformation of the Euclidean gradient by the precisionmatrix of the sufficient statistics u ( θ ) . In the simplest (and most common) formulation of Bayesian NNs (BNNs), the weights are drawnfrom Gaussian distributions, with a mean-field factorization which assumes that the weights areindependent. Hence, we have an approximate posterior for each weight q ( θ i | µ i , σ i ) = N ( µ i , σ i ) ,where µ i , σ i are the parameters to be optimized, and their Fisher information has an analytic form: F µ i = 1 /σ i , F v i = 2 , where v i = log σ i . (5)Consequently, the natural gradient of the mean of posterior can be computed as follows, where L represents the objective (loss) function: ˆ g µ i = σ i g µ i , g µ i = ∇ µ i L . (6)Equation (6) indicates that small σ i can cause the magnitude of natural gradient to be much re-duced. In addition, BNNs usually need very small variances in initialization to obtain a good perfor-mance at prediction time, which brings difficulties when tuning learning rates when applying vanillaStochastic Gradient Descent (SGD) to this Gaussian Natural Gradient (GNG). As shown in Figure 1and 2 in the supplementary materials we can see how the scale of variance in initialization changesthe magnitude of GNG.Meanwhile, Adam optimization [6] provides a method to ignore the scale of gradients in updatingsteps, which could compensate this drawback of GNG. More precisely, Adam optimization uses thesecond moment of gradients to reduce the variance of updating steps: θ k ← θ k − − α k ∗ E k [ g θ,k ] (cid:14) (cid:113) E k [ g Tθ,k g θ,k ] , g θ = ∇ θ L , (7)where k is index of updating steps, E k means averaging over updating steps, α k is the adaptivelearning rate at step k . Considering the first and second moments of ˆ g µ i ,k , E k [ˆ g µ i ,k ] = E k [ σ i,k ] E k [ g µ i ,k ] + cov ( σ i,k , g µ i ,k ) E k [ˆ g µ i ,k ] = ( E k [ σ i,k ] + var ( σ i,k )) E k [ g µ i ,k ] + cov ( σ i,k , g µ i ,k ) (8)We can see that only when var ( σ i,k ) = 0 and g µ i ,k are independent from σ i,k , the updates of GNGare equal to the updates by Euclidean gradients in the Adam optimizer. It also that shows largervar ( σ i,k ) will result in smaller updates when applying Adam optimization to GNG.We show comparison between different gradient descent algorithms in the supplementary materials.More experimental results are shown in Section 4.2n non-Bayesian models, natural gradients may have problems with the Adam optimizer becausethere is no posterior p ( θ | β ) defined. The distribution measured in natural gradient is the conditionalprobability p ( x | θ ) [4] and the loss function is usually L θ = E x [log p ( x | θ )] . In this case the naturalgradient of θ becomes: ˆ g θ = E x [ g θ ] E x [ g Tθ g θ ] , g θ = ∇ θ log p ( x | θ ) , ∇ θ L = E x [ g θ ] . (9)If we apply this to the Adam optimizer, which means replacing g θ in Equation (7) by ˆ g θ , the for-mulation is duplicated and involves the fourth moment of the gradient, which is undesirable for bothAdam optimization and natural gradients. One example is EWC [1] which uses Fisher informationto construct the penalty of changing previous parameters, hence, it has a similar form with Adamand it works worse with Adam than with vanilla SGD in our experience. However, this is not thecase for Bayesian models, where Equation (9) does not hold because the parameter θ has its poste-rior p ( θ | β ) and then the loss function L is optimized w.r.t. β , meanwhile ∇ β L (cid:54) = E θ [ ∇ β log p ( θ | β )] in common cases. In the context of continual learning, “coresets” are small collections of data samples of every learnedtask, used for task revisiting when learning a new task [3]. The motivation is to retain summarizedinformation of the data distribution of learned tasks so that we can use this information to constructan optimization objective for preventing parameters from drifting too far away from the solutionspace of old tasks while learning a new task. Typically, the memory cost of coresets will increasewith the number of tasks, hence, we would prefer the size of a coreset as small as possible. Thereare some existing approaches to Bayesian coreset construction for scalable machine learning [7, 8],the idea is to find a sparse weighted subset of data to approximate the likelihood over the wholedataset. In their problem setting the coreset construction is also crucial in posterior approximation,and the computational cost is at least O ( M N ) [8], where M is the coreset size and N is the datasetsize. In Bayesian continual learning, the coreset construction does not play a role in the posteriorapproximation of a task. For example, we can construct coresets without knowing the posterior, i.e.random coresets, K -centre coresets [3]. However, the information of a learned task is not only inits data samples but also in its trained parameters, so we consider constructing coresets using ourapproximated posteriors, yet without intervening the usual Bayesian inference procedure. Stein gradients [9] can be used to generate samples of a known distribution. Suppose we have aseries of samples x ( l ) from the empirical distribution p ( x ) , and we update them iteratively to movecloser to samples from the conditional distribution p ( x | θ ) by x ( l ) k +1 = x ( l ) k + (cid:15) φ ∗ ( x ( l ) k ) , where φ ∗ = arg max φ ∈F −∇ (cid:15) D KL ( p ( x ) || p ( x | θ )) | (cid:15) =0 = arg max φ ∈F E q [ A p φ ( x )] . (10) φ ∗ ( · ) is chosen to decrease the KL-divergence between p ( x ) and p ( x | θ ) in the steepest direction; F is chosen to be the unit ball of the Reproducing Kernel Hilbert Space to give a closed form updateof samples; and A p is Stein operator.Thus, the Stein gradient can be computed by: φ ∗ ( x ( l ) k ) = 1 M M (cid:88) j =1 (cid:104) k ( x ( j ) k , x ( l ) k ) ∇ x ( j ) k log p ( x ( j ) k | θ ) + ∇ x ( j ) k k ( x ( j ) k , x ( l ) k ) (cid:105) . (11)In the mean-field BNN model introduced in Section 2.2, we can just replace θ by µ, σ in Equa-tion (11). The computational complexity of the Stein gradient method is O ( M ) , which is signifi-cantly cheaper than O ( M N ) when M << N . We tested GNG with Adam in the framework of VCL on permuted MNIST [1], split MNIST [2],and split fashion MNIST [10] tasks. We applied a BNN with two hidden layers, each layer with300 hidden units, all split tasks tested using multi-head models [2]. The results are displayed inFigure 1 (left column) and the error bars are from 5 runs by different random seeds. In the permutedMNIST task, GNG with Adam outperforms standalone Adam. There is no significant difference insplit tasks. More details and further analysis can be found in the supplementary material. P er m u t e d M N I S T A v e r a g e a cc u r a c y Number of tasks A v e r age a cc u r a cy Adam + random coresetAdam + kcenter coresetAdam + Stein coresetAdam + GNG + random coresetAdam + GNG + kcenter coresetAdam + GNG + Stein coreset regretpredictive Sp li t M N I S T A v e r a g e a cc u r a c y AdamAdam + GNG
Number of tasks A v e r age a cc u r a cy Adam + random coresetAdam + kcenter coresetAdam + Stein coresetAdam + GNG + random coresetAdam + GNG + kcenter coresetAdam + GNG + Stein coresetregretpredictive Sp li t F a s h i o n M N I S T A v e r a g e a cc u r a c y Number of tasks A v e r age a cc u r a cy Adam + random coresetAdam + kcenter coresetAdam + Stein coresetAdam + GNG + random coresetAdam + GNG + kcenter coresetAdam + GNG + Stein coresetregretpredictive
Figure 1: Average accuracy on permuted and split tasks without (left) and with (right) coresetsFor experiments with coresets, we tested two different usages of coresets. In the first we use coresetsto train a predictive model as introduced in [3] (Equation (2)); in the second we add a regret lossfrom the coresets to the objective function, which does not require a separate predictive model: L t = E q t ( θ ) [log p ( D t | θ )] + E q t ( θ ) [log p ( C t − | θ )] − KL ( q t ( θ ) (cid:107) q t − ( θ )) , (12)where the second term in Equation (12) is regret loss constructed by coresets of previous tasks C t − .We applied a RBF kernel in the same manner as described in [9] to the Stein gradients and testedthe Stein coresets in both permuted and split tasks, comparing with random and K -center coresets.The coreset size is per task in permuted MNIST and in split tasks, which is the same as usedin [3]. The results are shown in Figure 1 (right column). The regret usage of coresets gives betterperformance in general, and Stein coresets also outperform other two types of coresets in most cases.4 eferences [1] James Kirkpatrick, Razvan Pascanu, Neil Rabinowitz, Joel Veness, Guillaume Desjardins, Andrei A Rusu,Kieran Milan, John Quan, Tiago Ramalho, Agnieszka Grabska-Barwinska, et al. Overcoming catastrophicforgetting in neural networks. Proceedings of the national academy of sciences , page 201611835, 2017.[2] Friedemann Zenke, Ben Poole, and Surya Ganguli. Continual learning through synaptic intelligence. In
International Conference on Machine Learning , pages 3987–3995, 2017.[3] Cuong V Nguyen, Yingzhen Li, Thang D Bui, and Richard E Turner. Variational continual learning. In
International Conference on Learning Representations , 2018.[4] Razvan Pascanu and Yoshua Bengio. Revisiting natural gradient for deep networks. In
InternationalConference on Learning Representations 2014 , 2014.[5] Matthew D Hoffman, David M Blei, Chong Wang, and John Paisley. Stochastic variational inference.
TheJournal of Machine Learning Research , 14(1):1303–1347, 2013.[6] Diederik P Kingma and Jimmy Lei Ba. Adam: A method for stochastic optimization. In
Proceeding 3rdInternational Conference on Learning Representations , 2014.[7] Jonathan Huggins, Trevor Campbell, and Tamara Broderick. Coresets for scalable Bayesian logisticregression. In
Advances in Neural Information Processing Systems , pages 4080–4088, 2016.[8] Trevor Campbell and Tamara Broderick. Bayesian coreset construction via greedy iterative geodesicascent. arXiv preprint arXiv:1802.01737 , 2018.[9] Qiang Liu and Dilin Wang. Stein variational gradient descent: A general purpose Bayesian inferencealgorithm. In
Advances In Neural Information Processing Systems , pages 2378–2386, 2016.[10] Han Xiao, Kashif Rasul, and Roland Vollgraf. Fashion-MNIST: a novel image dataset for benchmarkingmachine learning algorithms. arXiv preprint arXiv:1708.07747 , 2017.[11] Christos Louizos, Karen Ullrich, and Max Welling. Bayesian compression for deep learning. In
Advancesin Neural Information Processing Systems , pages 3288–3298, 2017. ppendices A Comparing different gradient descent algorithms (a) Vanilla SGD (b) Adam (c) SGD+GNG (d) Adam+GNG
Figure 2: Updating trajectory of parameters of 1-dimensional Bayesian linear regression in continuallearning. The model is defined as y ∼ N ( wx + b, . , w ∼ N ( µ w , σ w ) , b ∼ N ( µ b , σ b ) . The x -axis is µ w and y -axis is µ b . The contour depicts the average MSE over seen tasks, the cross-markindicates the position of true parameters of each task, different colours represent different tasks. Thelearning rate is set to . for vanilla SGD and . for all other methods. The initialization of σ w and σ b is set to σ = e − . Figures 2 and 3 demonstrate how the optimization methods and scale of variance affect parameter updates inan 1-dimensional Bayesian linear regression model of continual learning. In Figure 2d the updating steps aresmaller than in Figure 3d, even when the scale of variance is larger, which is because larger value of initializa-tion σ results in a larger variance of gradients (see the difference between Figure 2a and Figure 3a, Figure 2band Figure 3b), and consequently larger var ( σ i,t ) as well, meaning that the step size of GNG decreases accord-ing to Equation 7 and 8 in the main content. In general, GNG shows lower variance in parameter updates, andit works better with Adam than with SGD. B Further analysis of Gaussian Natural Gradients and Adam experiments
As one model has a limited capacity, and each different task contains some different information, the idealcase for continual learning is that each new task shares as much information as possible with previous tasks,and occupying as little extra capacity within the Neural Network as possible. This is analogous to modelcompression [11], but one key difference is we want more free parameters instead of parameters that are setto zero. For example, there are k independent parameters in a model and the log-likelihood of current task isfactorized as: log p ( D t | θ , θ , . . . , θ k ) = k (cid:88) i =1 log p ( D t | θ i ) . (13)If θ is absolutely free for this task, it indicates the following conditional probability is a constant w.r.t. θ : log p ( D t , θ , . . . , θ k | θ ) = log p ( D t | θ ) + k (cid:88) j =2 log p ( D t , θ j ) = const, ∀ θ . (14) .2 0.0 0.2 0.4 0.6 0.80.00.20.40.60.81.0 0.20.00.20.40.60.8task 1task 2task 3task 4task 5 (a) Vanilla SGD (b) Adam (c) SGD+GNG (d) Adam+GNG Figure 3: Parameter trajectory of 1-dimensional Bayesian linear regression in continual learning.All configurations are the same as in Figure 2 except σ = e − . This would require ∇ θ log p ( D t | θ ) = 0 , ∀ θ . (15)Therefore, θ is free to move, then no matter what value of θ is set to in future tasks, it will not affect theloss of previously learned tasks. In realistic situations, θ is very unlikely to be absolutely free. However, it isfeasible to maximize the entropy of θ , larger entropy indicating more freedom of θ . For instance, minimizingKL divergence includes maximizing the entropy of parameters: KL ( q t ( θ ) || q t − ( θ )) = E q t [ q t − ( θ )] − H q t ( θ ) . (16)On the other hand, it is undesirable to change parameters with lower entropy instead of those with higherentropy while learning a new task, since it could cause a larger loss on previous tasks.The entropy of a Gaussian distribution is defined by its variance alone. In this sense, a larger decrease ofvariance indicates larger decrease of entropy. To understand why GNG works better on permuted MNISTtasks, we visualized how the variances of the weights change in Figure 4 where we normalized all values asbelow: ∆ σ i,t = σ i,t − max i σ i, max i σ i, , (17)where max i σ i, is the maximal variance of the first task. When the variance of parameters is decreased bylearning a new task, the entropy of the model is decreased as well. We can think of it as new information writteninto the model, so when the model has learned more tasks, the variances of more parameters will have shrunkas shown in Figure 4.In an ideal case, a parameter with larger variance should be chosen to write new information preferentially toavoid erasing information of previous tasks. Therefore, it would be preferred if the dark colour spread moreevenly in latter tasks in Figure 4, and Adam + GNG appears to have this property for the permuted MNIST task(Figure 4a). However, there is no notable difference caused by GNG for split MNIST tasks (Figure 4b), whichconsistent with their performance in terms of average accuracy over tasks. The underlying reason needs furtherinvestigation. ayer 1layer 2layer 3 t a s k t a s k t a s k t a s k t a s k t a s k t a s k t a s k t a s k t a s k layer 1layer 2layer 3 1.51.20.90.60.30.0 (a) permuted MNIST layer 1layer 2 t a s k t a s k t a s k t a s k t a s k layer 1layer 2 0.100.080.060.040.02 (b) split MNIST Figure 4: Variance changes w.r.t. first task, top row is from models trained by Adam, bottom row isfrom models trained by Adam + GNG, tested on permuted and split MNIST without coresets. The x -axis is concatenated by tasks, the yy