A Complete Recipe for Stochastic Gradient MCMC

Yi-An Ma, Tianqi Chen, Emily B. Fox

Introduction

Markov chain Monte Carlo (MCMC) has become a defacto tool for Bayesian posterior inference. However, these methods notoriously mix slowly in complex, high-dimensional models and scale poorly to large datasets. The past decades have seen a rise in MCMC methods that provide more efficient exploration of the posterior, such as Hamiltonian Monte Carlo (HMC) and its Reimann manifold variant . This class of samplers is based on defining a potential energy function in terms of the target posterior distribution and then devising various continuous dynamics to explore the energy landscape, enabling proposals of distant states. The gain in efficiency of exploration often comes at the cost of a significant computational burden in large datasets.

Recently, stochastic gradient variants of such continuous-dynamic samplers have proven quite useful in scaling the methods to large datasets . At each iteration, these samplers use data subsamples—or minibatches—rather than the full dataset. Stochastic gradient Langevin dynamics (SGLD) innovated in this area by connecting stochastic optimization with a first-order Langevin dynamic MCMC technique, showing that adding the “right amount” of noise to stochastic gradient ascent iterates leads to samples from the target posterior as the step size is annealed. Stochastic gradient Hamiltonian Monte Carlo (SGHMC) builds on this idea, but importantly incorporates the efficient exploration provided by the HMC momentum term. A key insight in that paper was that the naïve stochastic gradient variant of HMC actually leads to an incorrect stationary distribution (also see ); instead a modification to the dynamics underlying HMC is needed to account for the stochastic gradient noise. Variants of both SGLD and SGHMC with further modifications to improve efficiency have also recently been proposed .

In the plethora of past MCMC methods that explicitly leverage continuous dynamics—including HMC, Riemann manifold HMC, and the stochastic gradient methods—the focus has been on showing that the intricate dynamics leave the target posterior distribution invariant. Innovating in this arena requires constructing novel dynamics and simultaneously ensuring that the target distribution is the stationary distribution. This can be quite challenging, and often requires significant physical and geometrical intuition . A natural question, then, is whether there exists a general recipe for devising such continuous-dynamic MCMC methods that naturally lead to invariance of the target distribution. In this paper, we answer this question to the affirmative. Furthermore, and quite importantly, our proposed recipe is complete. That is, any continuous Markov process (with no jumps) with the desired invariant distribution can be cast within our framework, including HMC, Riemann manifold HMC, SGLD, SGHMC, their recent variants, and any future developments in this area. That is, our method provides a unifying framework of past algorithms, as well as a practical tool for devising new samplers and testing the correctness of proposed samplers.

The recipe involves defining a (stochastic) system parameterized by two matrices: a positive semidefinite diffusion matrix, D(z)\mathbf{D}(\mathbf{z}), and a skew-symmetric curl matrix, Q(z)\mathbf{Q}(\mathbf{z}), where z=(θ,r)\mathbf{z}=(\theta,r) with θ\theta our model parameters of interest and rr a set of auxiliary variables. The dynamics are then written explicitly in terms of the target stationary distribution and these two matrices. By varying the choices of D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}), we explore the space of MCMC methods that maintain the correct invariant distribution. We constructively prove the completeness of this framework by converting a general continuous Markov process into the proposed dynamic structure.

For any given D(z)\mathbf{D}(\mathbf{z}), Q(z)\mathbf{Q}(\mathbf{z}), and target distribution, we provide practical algorithms for implementing either full-data or minibatch-based variants of the sampler. In Sec. 3.1, we cast many previous continuous-dynamic samplers in our framework, finding their D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}). We then show how these existing D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}) building blocks can be used to devise new samplers; we leave the question of exploring the space of D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}) well-suited to the structure of the target distribution as an interesting direction for future research. In Sec. 3.2 we demonstrate our ability to construct new and relevant samplers by proposing stochastic gradient Riemann Hamiltonian Monte Carlo, the existence of which was previously only speculated. We demonstrate the utility of this sampler on synthetic data and in a streaming Wikipedia analysis using latent Dirichlet allocation .

A Complete Stochastic Gradient MCMC Framework

Marginalizing the auxiliary variables gives us the desired distribution on θ\theta. In this paper, we generically consider z\mathbf{z} as the samples we seek to draw; z\mathbf{z} could represent θ\theta itself, or an augmented state space in which case we simply discard the auxiliary variables to perform the desired marginalization.

As in HMC, the idea is to translate the task of sampling from the posterior distribution to simulating from a continuous dynamical system which is used to define a Markov transition kernel. That is, over any interval hh, the differential equation defines a mapping from the state at time tt to the state at time t+ht+h. One can then discuss the evolution of the distribution p(z,t)p(\mathbf{z},t) under the dynamics, as characterized by the Fokker-Planck equation for stochastic dynamics or the Liouville equation for deterministic dynamics . This evolution can be used to analyze the invariant distribution of the dynamics, ps(z)p^{s}(\mathbf{z}). When considering deterministic dynamics, as in HMC, a jump process must be added to ensure ergodicity. If the resulting stationary distribution is equal to the target posterior, then simulating from the process can be equated with drawing samples from the posterior.

If the stationary distribution is not the target distribution, a Metropolis-Hastings (MH) correction can often be applied. Unfortunately, such correction steps require a costly computation on the entire dataset. Even if one can compute the MH correction, if the dynamics do not nearly lead to the correct stationary distribution, then the rejection rate can be high even for short simulation periods hh. Furthermore, for many stochastic gradient MCMC samplers, computing the probability of the reverse path is infeasible, obviating the use of MH. As such, a focus in the literature is on defining dynamics with the right target distribution, especially in large-data scenarios where MH corrections are computationally burdensome or infeasible.

Generically, all continuous Markov processes that one might consider for sampling can be written as a stochastic differential equation (SDE) of the form:

where f(z)\mathbf{f}(\mathbf{z}) denotes the deterministic drift and often relates to the gradient of H(z)H(\mathbf{z}), W(t){\mathbf{W}(t)} is a dd-dimensional Wiener process, and D(z)\mathbf{D}(\mathbf{z}) is a positive semidefinite diffusion matrix. Clearly, however, not all choices of f(z)\mathbf{f}(\mathbf{z}) and D(z)\mathbf{D}(\mathbf{z}) yield the stationary distribution ps(z)∝exp⁡(−H(z))p^{s}(\mathbf{z})\propto\exp(-H(\mathbf{z})).

When D(z)=0\mathbf{D}(\mathbf{z})=0, as in HMC, the dynamics of Eq. (2) become deterministic. Our exposition focuses on SDEs, but our analysis applies to deterministic dynamics as well. In this case, our framework—using the Liouville equation in place of Fokker-Planck—ensures that the deterministic dynamics leave the target distribution invariant. For ergodicity, a jump process must be added, which is not considered in our recipe, but tends to be straightforward (e.g., momentum resampling in HMC).

To devise a recipe for constructing SDEs with the correct stationary distribution, we propose writing f(z)\mathbf{f}(\mathbf{z}) directly in terms of the target distribution:

Here, Q(z)\mathbf{Q}(\mathbf{z}) is a skew-symmetric curl matrix representing the deterministic traversing effects seen in HMC procedures. In contrast, the diffusion matrix D(z)\mathbf{D}(\mathbf{z}) determines the strength of the Wiener-process-driven diffusion. Matrices D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}) can be adjusted to attain faster convergence to the posterior distribution. A more detailed discussion on the interpretation of D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}) and the influence of specific choices of these matrices is provided in the Supplement.

Importantly, as we show in Theorem 1, sampling the stochastic dynamics of Eq. (2) (according to Itô integral) with f(z)\mathbf{f}(\mathbf{z}) as in Eq. (3) leads to the desired posterior distribution as the stationary distribution: ps(z)∝exp⁡(−H(z))p^{s}(\mathbf{z})\propto\exp(-H(\mathbf{z})). That is, for any choice of positive semidefinite D(z)\mathbf{D}(\mathbf{z}) and skew-symmetric Q(z)\mathbf{Q}(\mathbf{z}) parameterizing f(z)\mathbf{f}(\mathbf{z}), we know that simulating from Eq. (2) will provide samples from p(θ∣S)p(\theta\mid\mathcal{S}) (discarding any sampled auxiliary variables rr) assuming the process is ergodic.

ps(z)∝exp⁡(−H(z))p^{s}(\mathbf{z})\propto\exp(-H(\mathbf{z})) is a stationary distribution of the dynamics of Eq. (2) if f(z)\mathbf{f}(\mathbf{z}) is restricted to the form of Eq. (3), with D(z)\mathbf{D}(\mathbf{z}) positive semidefinite and Q(z)\mathbf{Q}(\mathbf{z}) skew-symmetric. If D(z)\mathbf{D}(\mathbf{z}) is positive definite, or if ergodicity can be shown, then the stationary distribution is unique.

The equivalence of ps(z)p^{s}(\mathbf{z}) and the target p(z∣S)∝exp⁡(−H(z))p(\mathbf{z}\mid\mathcal{S})\propto\exp(-H(\mathbf{z})) can be shown using the Fokker-Planck description of the probability density evolution under the dynamics of Eq. (2) :

Eq. (4) can be further transformed into a more compact form :

We can verify that p(z∣S)p(\mathbf{z}\mid\mathcal{S}) is invariant under Eq. (5) by calculating [e−H(z)∇H(z)+∇e−H(z)]=0\left[e^{-H(\mathbf{z})}\nabla H(\mathbf{z})+\nabla e^{-H(\mathbf{z})}\right]=0. If the process is ergodic, this invariant distribution is unique. The equivalence of the compact form was originally proved in ; we include a detailed proof in the Supplement for completeness. ∎

2 Completeness of the Framework

An important question is what portion of samplers defined by continuous Markov processes with the target invariant distribution can we define by iterating over all possible D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z})? In Theorem 2, we show that for any continuous Markov process with the desired stationary distribution, ps(z)p^{s}(\mathbf{z}), there exists an SDE as in Eq. (2) with f(z)\mathbf{f}(\mathbf{z}) defined as in Eq. (3). We know from the Chapman-Kolmogorov equation that any continuous Markov process with stationary distribution ps(z)p^{s}(\mathbf{z}) can be written as in Eq. (2), which gives us the diffusion matrix D(z)\mathbf{D}(\mathbf{z}). Theorem 2 then constructively defines the curl matrix Q(z)\mathbf{Q}(\mathbf{z}). This result implies that our recipe is complete. That is, we cover all possible continuous Markov process samplers in our framework. See Fig. 1.

For the SDE of Eq. (2), suppose its stationary probability density function ps(z)p^{s}(\mathbf{z}) uniquely exists, and that \left[\mathbf{f}_{i}(\mathbf{z})p^{s}(\mathbf{z})-\sum_{j=1}^{d}\dfrac{\partial}{\partial\theta_{j}}\Big{(}{\mathbf{D}}_{ij}(\mathbf{z})p^{s}(\mathbf{z})\Big{)}\right] is integrable with respect to the Lebesgue measure, then there exists a skew-symmetric Q(z)\mathbf{Q}(\mathbf{z}) such that Eq. (3) holds.

The integrability condition is usually satisfied when the probability density function uniquely exists. A constructive proof for the existence of Q(z){\mathbf{Q}}(\mathbf{z}) is provided in the Supplement.

3 A Practical Algorithm

In practice, simulation relies on an ϵ\epsilon-discretization of the SDE, leading to a full-data update rule

Calculating the gradient of H(z)H(\mathbf{z}) involves evaluating the gradient of U(θ)U(\theta). For a stochastic gradient method, the assumption is that U(θ)U(\theta) is too computationally intensive to compute as it relies on a sum over all data points (see Sec. 2). Instead, such stochastic gradient algorithms examine independently sampled data subsets S~⊂S\widetilde{\mathcal{S}}\subset\mathcal{S} and the corresponding potential for these data:

The specific form of Eq. (7) implies that U~(θ)\widetilde{U}(\theta) is an unbiased estimator of U(θ)U(\theta). As such, a gradient computed based on U~(θ)\widetilde{U}(\theta)—called a stochastic gradient —is a noisy, but unbiased estimator of the full-data gradient. The key question in many of the existing stochastic gradient MCMC algorithms is whether the noise injected by the stochastic gradient adversely affects the stationary distribution of the modified dynamics (using ∇U~(θ)\nabla\widetilde{U}(\theta) in place of ∇U(θ)\nabla U(\theta)). One way to analyze the impact of the stochastic gradient is to make use of the central limit theorem and assume

resulting in a noisy Hamiltonian gradient ∇H~(z)=∇H(z)+[N(0,V(θ)),0]T\nabla\widetilde{H}(\mathbf{z})=\nabla H(\mathbf{z})+[\mathcal{N}(0,\mathbf{V}(\theta)),\mathbf{0}]^{T}. Simply plugging in ∇H~(z)\nabla\widetilde{H}(\mathbf{z}) in place of ∇H(z)\nabla H(\mathbf{z}) in Eq. (6) results in dynamics with an additional noise term ({\mathbf{D}}(\mathbf{z}_{t})+{\mathbf{Q}}(\mathbf{z}_{t})\big{)}[\mathcal{N}(0,\mathbf{V}(\theta)),\mathbf{0}]^{T}. To counteract this, assume we have an estimate B^t\hat{\mathbf{B}}_{t} of the variance of this additional noise satisfying 2D(zt)−ϵtB^t⪰02\mathbf{D}(\mathbf{z}_{t})-\epsilon_{t}\hat{\mathbf{B}}_{t}\succeq 0 (i.e., positive semidefinite). With small ϵ\epsilon, this is always true since the stochastic gradient noise scales down faster than the added noise. Then, we can attempt to account for the stochastic gradient noise by simulating

This provides our stochastic gradient—or minibatch— variant of the sampler. In Eq. (9), the noise introduced by the stochastic gradient is multiplied by ϵt\epsilon_{t} (and the compensation by ϵt2\epsilon_{t}^{2}), implying that the discrepancy between these dynamics and those of Eq. (6) approaches zero as ϵt\epsilon_{t} goes to zero. As such, in this infinitesimal step size limit, since Eq. (6) yields the correct invariant distribution, so does Eq. (9). This avoids the need for a costly or potentially intractable MH correction. However, having to decrease ϵt\epsilon_{t} to zero comes at the cost of increasingly small updates. We can also use a finite, small step size in practice, resulting in a biased (but faster) sampler. A similar bias-speed tradeoff was used in to construct MH samplers, in addition to being used in SGLD and SGHMC.

Applying the Theory to Construct Samplers

We explicitly state how some recently developed MCMC methods fall within the proposed framework based on specific choices of D(z)\mathbf{D}(\mathbf{z}), Q(z)\mathbf{Q}(\mathbf{z}) and H(z)H(\mathbf{z}) in Eq. (2) and (3). For the stochastic gradient methods, we show how our framework can be used to “reinvent” the samplers by guiding their construction and avoiding potential mistakes or inefficiencies caused by naïve implementations.

The key ingredient in HMC is Hamiltonian dynamics, which simulate the physical motion of an object with position θ\theta, momentum rr, and mass M\mathbf{M} on an frictionless surface as follows (typically, a leapfrog simulation is used instead):

Eq. (12) is a special case of the proposed framework with z=(θ,r)\mathbf{z}=(\theta,r), H(θ,r)=U(θ)+12rTM−1rH(\theta,r)=U(\theta)+\frac{1}{2}r^{T}M^{-1}r, {\mathbf{Q}}(\theta,r)=\left(\begin{array}[]{ll}0&-\mathbf{I}\\ \mathbf{I}&0\end{array}\right) and D(θ,r)=0\mathbf{D}(\theta,r)=\mathbf{0}.

As discussed in , simply replacing ∇U(θ)\nabla U(\theta) by the stochastic gradient ∇U~(θ)\nabla\widetilde{U}(\theta) in Eq. (12) results in the following updates:

where the ≈\approx arises from the approximation of Eq. (8). Careful study shows that Eq. (15) cannot be rewritten into our proposed framework, which hints that such a naïve stochastic gradient version of HMC is not correct. Interestingly, the authors of proved that this naïve version indeed does not have the correct stationary distribution. In our framework, we see that the noise term N(0,2ϵtD(z))\mathcal{N}(0,2\epsilon_{t}\mathbf{D}(\mathbf{z})) is paired with a D(z)∇H(z)\mathbf{D}(\mathbf{z})\nabla H(\mathbf{z}) term, hinting that such a term should be added to Eq. (15). Here, {\mathbf{D}}(\theta,r)=\left(\begin{array}[]{ll}0&0\\ 0&\epsilon\mathbf{V}(\theta)\end{array}\right), which means we need to add D(z)∇H(z)=ϵV(θ)∇rH(θ,r)=ϵV(θ)M−1r\mathbf{D}(\mathbf{z})\nabla H(\mathbf{z})=\epsilon\mathbf{V}(\theta)\nabla_{r}H(\theta,r)=\epsilon\mathbf{V}(\theta)\mathbf{M}^{-1}r. Interestingly, this is the correction strategy proposed in , but through a physical interpretation of the dynamics. In particular, the term ϵV(θ)M−1r\epsilon\mathbf{V}(\theta)\mathbf{M}^{-1}r (or, generically, CM−1r\mathbf{C}\mathbf{M}^{-1}r where C⪰ϵV(θ)\mathbf{C}\succeq\epsilon\mathbf{V}(\theta)) has an interpretation as friction and leads to second order Langevin dynamics:

Here, B^t\hat{\mathbf{B}}_{t} is an estimate of V(θt)\mathbf{V}(\theta_{t}). This method now fits into our framework with H(θ,r)H(\theta,r) and Q(θ,r)\mathbf{Q}(\theta,r) as in HMC, but with {\mathbf{D}}(\theta,r)=\left(\begin{array}[]{ll}0&0\\ 0&\mathbf{C}\end{array}\right). This example shows how our theory can be used to identify invalid samplers and provide guidance on how to effortlessly correct the mistakes; this is crucial when physical intuition is not available. Once the proposed sampler is cast in our framework with a specific D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}), there is no need for sampler-specific proofs, such as those of .

SGLD proposes to use the following first order (no momentum) Langevin dynamics to generate samples

This algorithm corresponds to taking z=θ\mathbf{z}=\theta with H(θ)=U(θ)H(\theta)=U(\theta), D(θ)=D{\mathbf{D}}(\theta)=\mathbf{D}, Q(θ)=0{\mathbf{Q}}(\theta)=\mathbf{0}, and B^t=0\hat{\mathbf{B}}_{t}=\mathbf{0}. As motivated by Eq. (9) of our framework, the variance of the stochastic gradient can be subtracted from the sampler injected noise to make the finite stepsize simulation more accurate. This variant of SGLD leads to the stochastic gradient Fisher scoring algorithm .

SGLD can be generalized to use an adaptive diffusion matrix D(θ){\mathbf{D}}(\theta). Specifically, it is interesting to take D(θ)=G−1(θ){\mathbf{D}}(\theta)=\mathbf{G}^{-1}(\theta), where G(θ)\mathbf{G}(\theta) is the Fisher information metric. The sampler dynamics are given by

Taking D(θ)=G(θ)−1\mathbf{D}(\theta)={\mathbf{G}}(\theta)^{-1}, Q(θ)=0{\mathbf{Q}}(\theta)=\mathbf{0}, and B^t=0\hat{\mathbf{B}}_{t}=\mathbf{0}, this SGRLD method falls into our framework with correction term Γi(θ)=∑j∂Dij(θ)∂θj\Gamma_{i}(\theta)=\sum\limits_{j}\dfrac{\partial{\mathbf{D}}_{ij}(\theta)}{\partial\theta_{j}}. It is interesting to note that in earlier literature , Γi(θ)\Gamma_{i}(\theta) was taken to be 2 ∣G(θ)∣−1/2∑j∂∂θj(Gij−1(θ)∣G(θ)∣1/2)2\ |\mathbf{G}(\theta)|^{-1/2}\sum\limits_{j}\dfrac{\partial}{\partial\theta_{j}}\left(\mathbf{G}_{ij}^{-1}(\theta)|\mathbf{G}(\theta)|^{1/2}\right). More recently, it was found that this correction term corresponds to the distribution function with respect to a non-Lebesgue measure ; for the Lebesgue measure, the revised Γi(θ)\Gamma_{i}(\theta) was as determined by our framework . Again, we have an example of our theory providing guidance in devising correct samplers.

Finally, the SGNHT method incorporates ideas from thermodynamics to further increase adaptivity by augmenting the SGHMC system with an additional scalar auxiliary variable, ξ\xi. The algorithm uses the following dynamics:

We can take z=(θ,r,ξ)\mathbf{z}=(\theta,r,\xi), H(θ,r,ξ)=U(θ)+12rTr+12d(ξ−A)2H(\theta,r,\xi)=U(\theta)+\dfrac{1}{2}r^{T}r+\dfrac{1}{2d}(\xi-A)^{2}, {\mathbf{D}}(\theta,r,\xi)=\left(\begin{array}[]{ccc}0&0&0\\ 0&A\cdot\mathbf{I}&0\\ 0&0&0\end{array}\right), and {\mathbf{Q}}(\theta,r,\xi)=\left(\begin{array}[]{ccc}0&-\mathbf{I}&0\\ \mathbf{I}&0&r/d\\ 0&-r^{T}/d&0\end{array}\right) to place these dynamics within our framework.

In our framework, SGLD and SGRLD take Q(z)=0\mathbf{Q}(\mathbf{z})=0 and instead stress the design of the diffusion matrix D(z)\mathbf{D}(\mathbf{z}), with SGLD using a constant D(z)\mathbf{D}(\mathbf{z}) and SGRLD an adaptive, θ\theta-dependent diffusion matrix to better account for the geometry of the space being explored. On the other hand, HMC takes D(z)=0\mathbf{D}(\mathbf{z})=0 and focuses on the curl matrix Q(z)\mathbf{Q}(\mathbf{z}). SGHMC combines SGLD with HMC through non-zero D(θ)\mathbf{D}(\theta) and Q(θ)\mathbf{Q}(\theta) matrices. SGNHT then extends SGHMC by taking Q(z)\mathbf{Q}(\mathbf{z}) to be state dependent. The relationships between these methods are depicted in the Supplement, which likewise contains a discussion of the tradeoffs between these two matrices. In short, D(z)\mathbf{D}(\mathbf{z}) can guide escaping from local modes while Q(z)\mathbf{Q}(\mathbf{z}) can enable rapid traversing of low-probability regions, especially when state adaptation is incorporated. We readily see that most of the product space D(z)×Q(z)\mathbf{D}(\mathbf{z})\times\mathbf{Q}(\mathbf{z}), defining the space of all possible samplers, has yet to be filled.

2 Stochastic Gradient Riemann Hamiltonian Monte Carlo

In Sec. 3.1, we have shown how our framework unifies existing samplers. In this section, we now use our framework to guide the development of a new sampler. While SGHMC inherits the momentum term of HMC, making it easier to traverse the space of parameters, the underlying geometry of the target distribution is still not utilized. Such information can usually be represented by the Fisher information metric , denoted as G(θ)\mathbf{G}(\theta), which can be used to precondition the dynamics. For our proposed system, we consider H(θ,r)=U(θ)+12rTrH(\theta,r)=U(\theta)+\frac{1}{2}r^{T}r, as in HMC/SGHMC methods, and modify the D(θ,r)\mathbf{D}(\theta,r) and Q(θ,r)\mathbf{Q}(\theta,r) of SGHMC to account for the geometry as follows:

We refer to this algorithm as stochastic gradient Riemann Hamiltonian Monte Carlo (SGRHMC). Our theory holds for any positive definite G(θ)\mathbf{G}(\theta), yielding a generalized SGRHMC (gSGRHMC) algorithm, which can be helpful when the Fisher information metric is hard to compute.

A naïve implementation of a state-dependent SGHMC algorithm might simply (i) precondition the HMC update, (ii) replace ∇U(θ)\nabla U(\theta) by ∇U~(θ)\nabla\widetilde{U}(\theta), and (iii) add a state-dependent friction term on the order of the diffusion matrix to counterbalance the noise as in SGHMC, resulting in:

However, as we show in Sec. 4.1, samples from these dynamics do not converge to the desired distribution. Indeed, this system cannot be written within our framework. Instead, we can simply follow our framework and, as indicated by Eq. (9), consider the following update rule:

which includes a correction term ∇θ(G(θ)−1/2)\nabla_{\theta}\left({\mathbf{G}}(\theta)^{-1/2}\right), with ii-th component ∑j∂∂θj(G(θ)−1/2)ij\sum\limits_{j}\dfrac{\partial}{\partial\theta_{j}}\left({\mathbf{G}}(\theta)^{-1/2}\right)_{ij}. The practical implementation of gSGRHMC is outlined in Algorithm 1.

Experiments

In Sec. 4.1, we show that gSGRHMC can excel at rapidly exploring distributions with complex landscapes. We then apply SGRHMC to sampling in a latent Dirichlet allocation (LDA) model on a large Wikipedia dataset in Sec. 4.2. The Supplement contains details on the specific samplers considered and the parameter settings used in these experiments.

In this section we aim to empirically (i) validate the correctness of our recipe and (ii) assess the effectiveness of gSGRHMC. In Fig. 2(left), we consider two univariate distributions (shown in the Supplement) and compare SGLD, SGHMC, the naïve state-adaptive SGHMC of Eq. (27), and our proposed gSGRHMC of Eq. (30). See the Supplement for the form of G(θ)\mathbf{G}(\theta). As expected, the naïve implementation does not converge to the target distribution. In contrast, the gSGRHMC algorithm obtained via our recipe indeed has the correct invariant distribution and efficiently explores the distributions. In the second experiment, we sample a bivariate distribution with strong correlation. The results are shown in Fig. 2(right). The comparison between SGLD, SGHMC, and our gSGRHMC method shows that both a state-dependent preconditioner and Hamiltonian dynamics help to make the sampler more efficient than either element on its own.

2 Online Latent Dirichlet Allocation

We also applied SGRHMC (with G(θ)=diag(θ)−1{\mathbf{G}}(\theta)={\rm diag}(\theta)^{-1}, the Fisher information metric) to an online latent Dirichlet allocation (LDA) analysis of topics present in Wikipedia entries. In LDA, each topic is associated with a distribution over words, with βkw\beta_{kw} the probability of word ww under topic kk. Each document is comprised of a mixture of topics, with πk(d)\pi^{(d)}_{k} the probability of topic kk in document dd. Documents are generated by first selecting a topic zj(d)∼π(d)z_{j}^{(d)}\sim\pi^{(d)} for the jjth word and then drawing the specific word from the topic as xj(d)∼βzj(d)x_{j}^{(d)}\sim\beta_{z_{j}^{(d)}}. Typically, π(d)\pi^{(d)} and βk\beta_{k} are given Dirichlet priors.

The goal of our analysis here is inference of the corpus-wide topic distributions βk\beta_{k}. Since the Wikipedia dataset is large and continually growing with new articles, it is not practical to carry out this task over the whole dataset. Instead, we scrape the corpus from Wikipedia in a streaming manner and sample parameters based on minibatches of data. Following the approach in , we first analytically marginalize the document distributions π(d)\pi^{(d)} and, to resolve the boundary issue posed by the Dirichlet posterior of βk\beta_{k} defined on the probability simplex, use an expanded mean parameterization shown in Figure 3(upper left). Under this parameterization, we then compute ∇log⁡p(θ∣x)\nabla\log p(\theta|\mathbf{x}) and, in our implementation, use boundary reflection to ensure the positivity of parameters θkw\theta_{kw}. The necessary expectation over word-specific topic indicators zj(d)z_{j}^{(d)} is approximated using Gibbs sampling separately on each document, as in . The Supplement contains further details.

For all the methods, we report results of three random runs. When sampling distributions with mass concentrated over small regions, as in this application, it is important to incorporate geometric information via a Riemannian sampler . The results in Fig. 3(right) indeed demonstrate the importance of Riemannian variants of the stochastic gradient samplers. However, there also appears to be some benefits gained from the incorporation of the HMC term for both the Riemmannian and non-Reimannian samplers. The average runtime for the different methods are similar (see Fig. 3(lower left)) since the main computational bottleneck is the gradient evaluation. Overall, this application serves as an important example of where our newly proposed sampler can have impact.

Conclusion

We presented a general recipe for devising MCMC samplers based on continuous Markov processes. Our framework constructs an SDE specified by two matrices, a positive semidefinite D(z)\mathbf{D}(\mathbf{z}) and a skew-symmetric Q(z)\mathbf{Q}(\mathbf{z}). We prove that for any D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}), we can devise a continuous Markov process with a specified stationary distribution. We also prove that for any continuous Markov process with the target stationary distribution, there exists a D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}) that cast the process in our framework. Our recipe is particularly useful in the more challenging case of devising stochastic gradient MCMC samplers. We demonstrate the utility of our recipe in “reinventing” previous stochastic gradient MCMC samplers, and in proposing our SGRHMC method. The efficiency and scalability of the SGRHMC method was shown on simulated data and a streaming Wikipedia analysis.

This work was supported in part by ONR Grant N00014-10-1-0746, NSF CAREER Award IIS-1350133, and the TerraSwarm Research Center sponsored by MARCO and DARPA. We also thank Mr. Lei Wu for helping with the proof of Theorem 2 and Professors Ping Ao and Hong Qian for many discussions.

References

Supplementary Material: A Complete Recipe for Stochastic Gradient MCMC

Appendix A Proof of Stationary Distribution

In this section, we provide a proof for Theorem 1. To prove the theorem, we first show when f(z){\mathbf{f}}(\mathbf{z}) satisfies Eq. (3) in the main paper, then the following Fokker-Planck equation of the dynamics:

is equivalent to the following compact form :

We can further decompose the second term as follows

The second equality follows because ∑ij∂∂zi∂∂zj[Qij(z)p(z,t)]=0\sum_{ij}\dfrac{\partial}{\partial\mathbf{z}_{i}}\dfrac{\partial}{\partial\mathbf{z}_{j}}[\mathbf{Q}_{ij}(\mathbf{z})p(\mathbf{z},t)]=0 due to anti-symmetry of Q\mathbf{Q}. Putting these back into the formula, we get

We can then verify that p(z∣S)∝e−H(z)p(\mathbf{z}\mid\mathcal{S})\propto e^{-H(\mathbf{z})} is invariant under the compact form by calculating

The above proof follows directly from and is provided here for readers’ convenience.

Appendix B Proof of Completeness

In this section, we provide a constructive proof for Theorem 2, the existence of Q(z){\mathbf{Q}}(\mathbf{z}).

We first rewrite Eq. (3) in the main paper and notice that finding matrix Q(z)\mathbf{Q}(\mathbf{z}) is equivalent to finding the matrix Q(z)ps(z)\mathbf{Q}(\mathbf{z})p^{s}(\mathbf{z}) such that

where the right hand side is a divergence-free vector.

We transform the above equation and its constraint into the frequency domain and obtain a set of linear equations.

Then we construct a solution to the linear equations and use inverse Fourier transform to obtain Q(z)\mathbf{Q}(\mathbf{z}).

Multiplying ps(z)p^{s}(\mathbf{z}) on both sides of Eq. (3) in the main paper, and noting that:

The equation for Qij(z){\mathbf{Q}}_{ij}(\mathbf{z}) can now be written as:

Recall that the Fokker-Planck equation for the stochastic process, Eq. (2), is:

We can immediately observe that the right hand side of Eq. (S.4) has a divergenceless property by substituting the stationary probability density function ps(z)p^{s}(\mathbf{z}) into Eq. (S.5):

The nice forms of Eqs. (S.4) and (S.6) imply that the questions can be transformed into a linear algebra problem once we apply a Fourier transform to them. Denote the Fourier transform of Q(z)ps(z){\mathbf{Q}}(\mathbf{z})p^{s}(\mathbf{z}) as Q^(k)\hat{{\mathbf{Q}}}({\mathbf{k}}); and Fourier transform of {\mathbf{f}}_{i}(\mathbf{z})p^{s}(\mathbf{z})-\sum\limits_{j}\dfrac{\partial}{\partial\mathbf{z}_{j}}\Big{(}{\mathbf{D}}_{ij}(\mathbf{z})p^{s}(\mathbf{z})\Big{)} as F^i(k)\hat{{\mathbf{F}}}_{i}({\mathbf{k}}), where k=(k1,⋯ ,kn)T{\mathbf{k}}=({\mathbf{k}}_{1},\cdots,{\mathbf{k}}_{n})^{T} is the set of the spectral variables. That is:

Then, \dfrac{\partial}{\partial\mathbf{z}_{j}}\Big{(}{\mathbf{Q}}_{ij}(\mathbf{z})p^{s}(\mathbf{z})\Big{)} is transformed to 2πi Q^ijkj2\pi{\rm i}\ \hat{{\mathbf{Q}}}_{ij}{\mathbf{k}}_{j}, and Eq. (S.4) becomes the following equivalent form in Fourier space:

Hence, it is clear that matrix Q^\hat{{\mathbf{Q}}} must be a skew-symmetric projection matrix from the span of k{\mathbf{k}} to the span of F^\hat{{\mathbf{F}}}, where k{\mathbf{k}} and F^\hat{{\mathbf{F}}} are always orthogonal to each other. We thereby construct Q^\hat{{\mathbf{Q}}} as combination of two rank 11 projection matrices:

We arrive at the final result that matrix Q(z){\mathbf{Q}}(\mathbf{z}) is equal to ps(z)−1p^{s}(\mathbf{z})^{-1} times the inverse Fourier transform of Q^(k)\hat{{\mathbf{Q}}}({\mathbf{k}}):

Thus, if \left({\mathbf{f}}_{i}(\mathbf{z})p^{s}(\mathbf{z})-\sum\limits_{j}\dfrac{\partial}{\partial\mathbf{z}_{j}}\Big{(}{\mathbf{D}}_{ij}(\mathbf{z})p^{s}(\mathbf{z})\Big{)}\right) belongs to the space of L1L^{1}, then any continuous time Markov process, Eq. (2), can be turned into this new formulation. ∎

Entries in the skew-symmetric projector Qij(z){\mathbf{Q}}_{ij}(\mathbf{z}) constructed here are real.

Denote ai2=∑l≠ikl2{\mathbf{a}}_{i}^{2}=\sum\limits_{l\neq i}{\mathbf{k}}_{l}^{2}, then the inverse Fourier transform of ki(2πi)⋅∑lkl2\dfrac{{\mathbf{k}}_{i}}{(2\pi{\rm i})\cdot\sum\limits_{l}{\mathbf{k}}_{l}^{2}} along the partial variable ki{\mathbf{k}}_{i} is equal to:

where H[x]H[x] is the Heaviside function. Because gi(z){\mathbf{g}}_{i}(\mathbf{z}) is an even function in klk_{l}, l≠il\neq i, its total inverse Fourier transform is real.

Therefore, the inverse Fourier transform of kiF^j(k)(2πi)⋅∑lkl2\dfrac{{\mathbf{k}}_{i}\hat{{\mathbf{F}}}_{j}({\mathbf{k}})}{(2\pi{\rm i})\cdot\sum\limits_{l}{\mathbf{k}}_{l}^{2}} is the convolution of two real functions.

Appendix C 2-D Case as a Simple Intuitive Example of the Construction

Appendix D Previous MCMC Algorithms in the Form of Continuous Markov Processes as Elements in the Current Recipe

This section parallels that of Sec. 3.1 of the main paper, but in terms of the continuous dynamics underlying the samplers. This allows us to rapidly draw connections with our SDE framework of Sec. 2.1. Fig. S.4 provides a cartoon visualization of the portion of the product space D(z)×Q(z)\mathbf{D}(\mathbf{z})\times\mathbf{Q}(\mathbf{z}) already covered by past methods, after casting these methods in our framework below. Our proposed gSGRHMC method covers a portion of this space previously not explored.

The continuous dynamics underlying Eq. (12) in the main paper are

Again, we see Eq. (S.16) is a special case of our proposed framework with z=(θ,r)\mathbf{z}=(\theta,r), H(θ,r)=U(θ)+12rTM−1rH(\theta,r)=U(\theta)+\frac{1}{2}r^{T}M^{-1}r, {\mathbf{Q}}(\theta,r)=\left(\begin{array}[]{ll}0&-I\\ I&0\end{array}\right) and D(θ,r)=0\mathbf{D}(\theta,r)=\mathbf{0}.

As described in , replacing ∇U(θ)\nabla U(\theta) by the stochastic gradient ∇U~(θ)\nabla\widetilde{U}(\theta) in the ϵ\epsilon-discretized HMC system of Eq. (12) (resulting in Eq. (15)) has a continuous-time representation as:

Analogously to Sec. 3.1, these dynamics do not fit into our framework. Instead, in our framework we see that the noise term 2D(z)dW(t)\sqrt{2\mathbf{D}(\mathbf{z})}\textrm{d}\mathbf{W}(t) is paired with a D(z)∇H(z)\mathbf{D}(\mathbf{z})\nabla H(\mathbf{z}) term, hinting that such a term must be added to the dynamics of Eq. (S.19). Here, {\mathbf{D}}(\theta,r)=\left(\begin{array}[]{ll}0&0\\ 0&\epsilon\mathbf{V}\end{array}\right), which means we need to add a term of the form D(z)∇H(z)=ϵV∇rH(θ,r)=ϵVM−1r\mathbf{D}(\mathbf{z})\nabla H(\mathbf{z})=\epsilon\mathbf{V}\nabla_{r}H(\theta,r)=\epsilon\mathbf{V}\mathbf{M}^{-1}r. Interestingly, this is the correction strategy proposed in , but through a physical interpretation of the dynamics. In particular, the term ϵVM−1r\epsilon\mathbf{V}\mathbf{M}^{-1}r (or, generically, CM−1r\mathbf{C}\mathbf{M}^{-1}r) has an interpretation as a friction term and leads to second order Langevin dynamics:

This method now fits into our framework with H(θ,r)H(\theta,r) and Q(θ,r)\mathbf{Q}(\theta,r) as in HMC, but here with {\mathbf{D}}(\theta,r)=\left(\begin{array}[]{ll}0&0\\ 0&C\end{array}\right).

SGLD proposes to use the following first order (no momentum) Langevin dynamics to generate samples

This algorithm corresponds to taking z=θ\mathbf{z}=\theta with H(θ)=U(θ)H(\theta)=U(\theta), D(θ)=D{\mathbf{D}}(\theta)=\mathbf{D}, Q(θ)=0{\mathbf{Q}}(\theta)=0. As in the case of SGHMC, the variance of the stochastic gradient can be subtracted from the sampler injected noise 2DW(t)\sqrt{2\mathbf{D}}\mathbf{W}(t) to make the finite stepsize simulation more accurate. This variant of SGLD leads to the stochastic gradient Fisher scoring algorithm .

SGLD can be generalized to use an adaptive diffusion matrix D(θ){\mathbf{D}}(\theta). Specifically, it is interesting to take D(θ)=G−1(θ){\mathbf{D}}(\theta)=\mathbf{G}^{-1}(\theta), where G(θ)\mathbf{G}(\theta) is the Fisher information metric. The sampler dynamics is given by

Taking D(θ)=G−1(θ)\mathbf{D}(\theta)={\mathbf{G}}^{-1}(\theta) and Q(θ)=0{\mathbf{Q}}(\theta)=\mathbf{0}, the SGRLD method falls into our current framework with the correction term Γi(θ)=∑j∂Dij(θ)∂θj\Gamma_{i}(\theta)=\sum\limits_{j}\dfrac{\partial{\mathbf{D}}_{ij}(\theta)}{\partial\theta_{j}}.

Finally, the continuous dynamics underlying the SGNHT algorithm in Sec. 3.1 are

Again, we see we can take z=(θ,r,ξ)\mathbf{z}=(\theta,r,\xi), H(θ,r,ξ)=U(θ)+12rTr+12d(ξ−A)2H(\theta,r,\xi)=U(\theta)+\dfrac{1}{2}r^{T}r+\dfrac{1}{2d}(\xi-A)^{2}, {\mathbf{D}}(\theta,r,\xi)=\left(\begin{array}[]{ccc}0&0&0\\ 0&A\cdot\mathbf{I}&0\\ 0&0&0\end{array}\right), and {\mathbf{Q}}(\theta,r,\xi)=\left(\begin{array}[]{ccc}0&-\mathbf{I}&0\\ \mathbf{I}&0&r/d\\ 0&-r^{T}/d&0\end{array}\right) to place these dynamics within our framework.

Appendix E Discussion of Choice of D and Q

A lot of choices of D(z)\mathbf{D}(\mathbf{z}) and Q(z)\mathbf{Q}(\mathbf{z}) could potentially result in faster convergence of the samplers than those previously explored. For example, D(z)\mathbf{D}(\mathbf{z}) determines how much noise is introduced. Hence, an adaptive diffusion matrix D(z)\mathbf{D}(\mathbf{z}) can facilitate a faster escape from a local mode if ∣∣D(z)∣∣||\mathbf{D}(\mathbf{z})|| is larger in regions of low probability, and can increase accuracy near the global mode if ∣∣D(z)∣∣||\mathbf{D}(\mathbf{z})|| is smaller in regions of high probability. Motivated by the fact that a majority of the parameter space is covered by low probability mass regions where less accuracy is often needed, one might want to traverse these regions quickly. As such, an adaptive curl matrix Q(z)\mathbf{Q}(\mathbf{z}) with 22-norm growing with the level set of the distribution can facilitate a more efficient sampler. We explore an example of this in the gSGRHMC algorithm of the synthetic experiments (see Supp. F.1).

Appendix F Parameter Settings in Synthetic and Online Latent Dirichlet Allocation Experiments

In the synthetic experiment using gSGRHMC, we specifically consider G(θ)−1=D∣U~(θ)+C∣\mathbf{G}(\theta)^{-1}=D\sqrt{|\widetilde{U}(\theta)+C|}. The constant CC ensures that U~(θ)+C{\widetilde{U}(\theta)+C} is positive in most cases so that the fluctuation is indeed smaller when the probability density function is higher. Note that we define G(θ)\mathbf{G}(\theta) in terms of U~(θ)\widetilde{U}(\theta) to avoid a costly full-data computation. We choose D=1.5D=1.5 and C=0.5C=0.5 in the experiments. The design of G\mathbf{G} is motivated by the discussion in Supp. E, taking Q(θ)\mathbf{Q}(\theta) to have 2-norm growing with the level sets of the potential function can lead to faster exploration of the posterior.

Comparison of SGLD, SGHMC, the naïve implementation of SGRHMC (Eq. (27)), and the gSGRHMC methods is shown in Fig. S.5, indicating the incorrectness of the naïve SGRHMC.

F.2 Online Latent Dirichlet Allocation Experiment

In the online latent Dirichlet allocation (LDA) experiment, we used minibatches of 5050 documents and K=50K=50 topics. Similar to , the stochastic gradient of the log posterior of the parameter θ\theta on a minibatch S~\widetilde{\mathcal{S}} is calculated as

where α\alpha is the hyper-parameter for the Gamma prior of per-topic word distributions, and γ\gamma for the per-document topic distributions. Here, ndkwn_{dkw} is the count of how many times word ww is assigned to topic kk in document dd (via zj(d)=kz_{j}^{(d)}=k for xj=wx_{j}=w). The ⋅\cdot notation indicates ndk⋅=∑wndkwn_{dk\cdot}=\sum_{w}n_{dkw}. To calculate the expectation of the latent topic assignment counts ndkwn_{dkw}, Gibbs sampling is used on the topic assignments in each document separately, using the conditional distributions

where \j\backslash j represents a count excluding the topic assignment variable zj(d)z_{j}^{(d)} being updated. See for further details.

We follow the experimental settings in for Riemmanian samplers (SGRLD and SGRHMC), taking the hyper-parameters of Dirichlet priors to be γ=0.01\gamma=0.01 and α=0.0001\alpha=0.0001. Since the non-Riemmanian samplers (SGLD and SGHMC) do not handle distributions with mass concentrated over small regions as well as the Riemmanian samplers, we found γ=0.1\gamma=0.1 and α=0.01\alpha=0.01 to be optimal hyper-parameters for them and use these instead for SGLD and SGHMC. In doing so, we are modifying the posterior being sampled, but wished to provide as good of performance as possible for these baseline methods for a fair comparison. For the SGRLD method, we keep the stepsize schedule of \epsilon_{t}=\left(a\cdot\bigg{(}1+\dfrac{t}{b}\bigg{)}\right)^{-c} and corresponding optimal parameters a,b,ca,b,c used in the experiment of . For the other methods, we use a constant stepsize because it was easier to tune. (A constant stepsize for SGRLD performed worse than the schedule described above, so again we are trying to be as fair to baseline methods as possible when using non-constant stepsize for SGRLD.) A grid search is performed to find ϵt=0.02\epsilon_{t}=0.02 for the SGRHMC method; ϵt=0.01\epsilon_{t}=0.01, D=I\mathbf{D}=I (corresponding to Eq. (19) in the main paper) for the SGLD method; and ϵt=0.1\epsilon_{t}=0.1, C=M=I\mathbf{C}=\mathbf{M}=I (corresponding to Eq. (18) in the main paper) for the SGHMC method.

For a randomly selected subset of topics, in Table S.1 we show the top seven most heavily weighted words in the topic learned with the SGRHMC sampler.