Search

Search papers and lessons

Stochastic Variational Inference

Matt Hoffman, David M. Blei, Chong Wang et al. · 2012

Hoffman, Blei, Wang and Paisley's 2012 paper turns variational inference into a stream of small updates, each from one random document, so Bayesian topic models can learn from millions of documents.

On this page

  • Colour = topic mix
  • Probability
  • Inference
  • Calculus
  • Discrete math
  • Optimizers
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

Standard variational inference approximates a hard posterior distribution by solving an optimization problem, but each update has to look at every data point in the set. Hoffman, Blei, Wang and Paisley showed how to replace that full pass with a stream of cheap updates. Each step samples one document, computes a local guess about it, and blends that guess into the global answer with a shrinking step size. The method fits Bayesian topic models to collections of 1.8 million New York Times articles and 3.8 million Wikipedia articles. The full-pass method was run only on subsets of these collections, and did worse.

Builds on: Variational Lower Bound (ELBO), Mean-Field Approximation, Natural Gradient Descent, Stochastic Gradient Descent, Posterior Distribution · Leads to: scalable Bayesian inference for large models with hidden structure

Why it mattered

The paper made large-scale Bayesian modelling practical for text. Topic models built on it can organize large archives without labels: the authors ran the method on 350,000 articles from the journal Nature, 1.8 million New York Times articles and 3.8 million Wikipedia articles, and reported that the older full-pass variational method, run on subsets of each collection, predicted held-out text less well. The same recipe (take a noisy natural gradient from a random subset, then step with a decreasing size) is the basis of many later ways to scale variational methods to data that does not fit in memory.

History

Variational methods for probabilistic models were pioneered in the mid-1990s and developed through the 2000s, in work surveyed by Jordan and colleagues (1999) and by Wainwright and Jordan (2008). These methods needed a full pass over the data for each coordinate update, so they were slow on large collections.

The stochastic optimization ideas came from much earlier work. Robbins and Monro showed in 1951 that noisy gradient steps with shrinking sizes converge under mild conditions. Applying that idea to the natural gradient of the variational objective was the step the paper took. Its arXiv version appeared in 2012, and the paper was published in the Journal of Machine Learning Research in 2013. It caught on because it reused the existing coordinate updates almost unchanged, so any implementation of classical variational inference could be adapted to it.

The problem

A Bayesian model has hidden variables, things we cannot see such as the topics in a collection or the cluster of each item. Given the observed data, we want the posterior distribution over those hidden variables. For most realistic models the exact posterior has an integral with no closed form, so we need an approximation.

Variational inference (see Variational Lower Bound (ELBO)) turns the approximation into optimization. We choose a family of simple distributions indexed by parameters, and we tune the parameters to make the simple distribution as close as possible to the posterior. The usual algorithm is coordinate ascent: update the local parameters for every data point, then update the global parameters from all of them, then repeat. When the data has a million documents, each round costs a full pass over the million documents before the global answer improves even once. The paper asks whether we can improve the global answer after looking at only a few documents, and still reach the same kind of solution.

How it works

The walkthrough below uses a toy model with one global hidden quantity and no local variables, so each step is easy to follow by hand. The full algorithm adds a local step per document, covered in the math section.

Setup: a coin has an unknown bias. Its hidden quantity is the probability of heads, and the global parameter λ\lambda is a pair of pseudo-counts (a,b)(a, b) that describe our belief about it. The prior is (1,1)(1, 1). We observe four flips, heads, tails, heads, heads, so N=4N = 4 and the data points are x=1,0,1,1x = 1, 0, 1, 1. The exact posterior counts are (1+3,1+1)=(4,2)(1 + 3, 1 + 1) = (4, 2).

  1. Start. Set the global parameter to the prior, λ(0)=(1,1)\lambda^{(0)} = (1, 1). Its mean a/(a+b)a / (a + b) is 0.50.5, which says nothing about the data yet. Check: 1+1=21 + 1 = 2, and 1/2=0.51 / 2 = 0.5.

  2. Sample one point. Pick one flip at random. Say it is heads, x1=1x_1 = 1. We do not look at the other three flips yet.

  3. Form an intermediate parameter. Ask what the global parameter would be if this one flip were repeated N=4N = 4 times. The prior counts plus four copies of the flip's counts give λ^1=(1,1)+4⋅(1,0)=(5,1)\hat{\lambda}_1 = (1, 1) + 4 \cdot (1, 0) = (5, 1). Check: the heads count goes 1+4=51 + 4 = 5, the tails count stays 11.

  4. Blend with the current value. Use a step size ρ1\rho_1. We take ρt=1/(t+1)\rho_t = 1 / (t + 1) here, so ρ1=1/2\rho_1 = 1/2. The new value is λ(1)=(1−ρ1)λ(0)+ρ1λ^1=(0.5,0.5)+(2.5,0.5)=(3,1)\lambda^{(1)} = (1 - \rho_1)\lambda^{(0)} + \rho_1 \hat{\lambda}_1 = (0.5, 0.5) + (2.5, 0.5) = (3, 1). Check: the mean is 3/4=0.753 / 4 = 0.75, which moved up from 0.50.5 toward the flip.

  5. Repeat with a new point. Sample tails, x2=0x_2 = 0. Its intermediate parameter is λ^2=(1,1)+4⋅(0,1)=(1,5)\hat{\lambda}_2 = (1, 1) + 4 \cdot (0, 1) = (1, 5). The step size is ρ2=1/3\rho_2 = 1/3, so λ(2)=(2/3)(3,1)+(1/3)(1,5)=(7/3,7/3)\lambda^{(2)} = (2/3)(3, 1) + (1/3)(1, 5) = (7/3, 7/3). Check: both entries are 7/37/3, so the mean is (7/3)/(14/3)=1/2(7/3) / (14/3) = 1/2.

  6. Keep going. The steps shrink, so later points change the answer less and less. A point that is heads again gives λ(3)=(3/4)(7/3,7/3)+(1/4)(5,1)=(3,2)\lambda^{(3)} = (3/4)(7/3, 7/3) + (1/4)(5, 1) = (3, 2), with mean 0.60.6. The exact answer for these four flips is (4,2)(4, 2) with mean 0.670.67. The estimate is moving toward it, and the stream never needed a full pass.

The visual below shows the loop. The paper's Figure 4 gives the same loop as pseudocode.

flowchart TD
    A["Current global parameter lambda"] --> B["Sample one data point"]
    B --> C["Optimize its local parameters"]
    C --> D["Form intermediate global parameter as if the point were repeated N times"]
    D --> E["Blend: new lambda = (1 minus rho) times old lambda plus rho times intermediate"]
    E --> A

The math

We start from the model class the paper requires. Observations x1:Nx_{1:N} come from local hidden variables z1:Nz_{1:N}, one per observation, and one set of global hidden variables β\beta shared across all of them. The joint distribution factorizes:

p(x,z,β∣α)=p(β∣α)∏n=1Np(xn,zn∣β).p(x, z, \beta \mid \alpha) = p(\beta \mid \alpha) \prod_{n=1}^{N} p(x_n, z_n \mid \beta).

Here α\alpha holds fixed prior settings. The paper assumes that the conditional distributions are in the exponential family (see Exponential Family), with natural parameters η\eta and sufficient statistics tt. The model is conditionally conjugate: the full conditional of the global variables has the same form as their prior, which is what makes the global update simple (see Conjugate Prior).

Step 1 in symbols: the variational family and the objective

The mean-field family factorizes the approximation completely:

q(z,β)=q(β∣λ)∏n=1N∏j=1Jq(znj∣ϕnj).q(z, \beta) = q(\beta \mid \lambda) \prod_{n=1}^{N} \prod_{j=1}^{J} q(z_{nj} \mid \phi_{nj}).

The global parameter λ\lambda governs the global variables and each ϕnj\phi_{nj} governs one local variable. The objective is the evidence lower bound

L(q)=Eq[log⁡p(x,z,β)]−Eq[log⁡q(z,β)],\mathcal{L}(q) = \mathbb{E}_q[\log p(x, z, \beta)] - \mathbb{E}_q[\log q(z, \beta)],

which lower-bounds log⁡p(x)\log p(x) by Jensen's inequality (see Jensen's Inequality). Maximizing it is the same as minimizing the Kullback–Leibler Divergence from the approximation to the posterior, up to a constant.

Step 2 in symbols: the coordinate updates

Holding the local parameters fixed, the objective as a function of λ\lambda is maximized when

λ=Eq[ηg(x,z,α)].\lambda = \mathbb{E}_q[\eta_g(x, z, \alpha)].

In words, the global parameter is set to the expected natural parameter of its full conditional. The local update has the same form, ϕnj=Eq[ηℓ(xn,zn,−j,β)]\phi_{nj} = \mathbb{E}_q[\eta_\ell(x_n, z_{n,-j}, \beta)], and depends only on the global parameter and the nnth data point. This is the classical algorithm. Its global step needs every local parameter, which is the cost the paper wants to avoid.

Step 3 in symbols: the natural gradient

Gradient ascent on λ\lambda uses the ordinary Euclidean gradient, which depends on parameterization. The natural gradient rescales it by the inverse Fisher information (see Fisher Information) and Natural Gradient Descent explains why this measures steps in distance between distributions. For the exponential family, the paper shows the natural gradient of the objective reduces to

∇^λL=Eϕ[ηg(x,z,α)]−λ.\hat{\nabla}_\lambda \mathcal{L} = \mathbb{E}_\phi[\eta_g(x, z, \alpha)] - \lambda.

A natural gradient step of size one lands exactly on the coordinate update. So coordinate ascent is a special case of natural gradient ascent with the largest possible step.

Step 4 in symbols: the stochastic update

Pick one data point xix_i at random. Its intermediate global parameter is the coordinate update computed as if xix_i were repeated NN times:

λ^t=α+N Eϕi[(t(xi,zi),1)].\hat{\lambda}_t = \alpha + N \, \mathbb{E}_{\phi_i}\big[(t(x_i, z_i), 1)\big].

This is an unbiased noisy version of the full update. In the coin example the sufficient statistic of a flip is its counts (xi,1−xi)(x_i, 1 - x_i), so a heads contributes (1,0)(1, 0), as in the walkthrough. The stochastic variational inference update is then

λ(t)=(1−ρt) λ(t−1)+ρt λ^t,\lambda^{(t)} = (1 - \rho_t)\, \lambda^{(t-1)} + \rho_t \, \hat{\lambda}_t,

which is the same as a noisy natural gradient step with size ρt\rho_t. The step sizes follow

ρt=(t+τ)−κ,κ∈(0.5,1],τ≥0.\rho_t = (t + \tau)^{-\kappa}, \qquad \kappa \in (0.5, 1], \quad \tau \geq 0.

The forgetting rate κ\kappa controls how fast old information is discounted. The delay τ\tau down-weights the first iterations. For κ\kappa in (0.5,1](0.5, 1] the sum of the ρt\rho_t diverges while the sum of their squares converges, which are the Robbins and Monro conditions for stochastic approximation (see Stochastic Approximation and Stochastic Gradient Descent). Under those conditions the global parameter converges to a local optimum of the objective. The paper sets τ=1\tau = 1 throughout its experiments.

Step 4b: minibatches

Sampling SS points at a time and averaging their intermediate parameters reduces noise. The update keeps the same form with λ^t\hat{\lambda}_t replaced by the average over the minibatch. The paper's experiments vary the minibatch size SS.

Step 5: the step-size check on the toy

In the coin example, ρt=1/(t+1)\rho_t = 1/(t+1) gives ρ1=1/2\rho_1 = 1/2, ρ2=1/3\rho_2 = 1/3 and ρ3=1/4\rho_3 = 1/4. Their sum keeps growing, and their squares add to a finite number, so the step sizes satisfy the conditions.

Worked examples

Example
Example 1: one flip, one step (tiny)

Prior λ(0)=(1,1)\lambda^{(0)} = (1, 1), data size N=4N = 4, first flip heads so x1=1x_1 = 1.

  1. Intermediate parameter: λ^1=(1,1)+4⋅(1,0)=(5,1)\hat{\lambda}_1 = (1, 1) + 4 \cdot (1, 0) = (5, 1).
  2. Step size: ρ1=1/(1+1)=1/2\rho_1 = 1/(1 + 1) = 1/2.
  3. Blend: λ(1)=(1/2)(1,1)+(1/2)(5,1)=(0.5,0.5)+(2.5,0.5)=(3,1)\lambda^{(1)} = (1/2)(1, 1) + (1/2)(5, 1) = (0.5, 0.5) + (2.5, 0.5) = (3, 1).
  4. Mean of the coin: 3/(3+1)=0.753 / (3 + 1) = 0.75.

Interpretation: one heads moved the belief from 0.50.5 to 0.750.75. It did not jump all the way to the four-flip answer of 0.670.67, because the step size is only one half. The method changes its belief in proportion to the step size, not all at once.

Example
Example 2: the standard case, two more steps

Start from λ(1)=(3,1)\lambda^{(1)} = (3, 1) and continue with the next two flips, tails then heads.

  1. Tails: x2=0x_2 = 0, so λ^2=(1,1)+4⋅(0,1)=(1,5)\hat{\lambda}_2 = (1, 1) + 4 \cdot (0, 1) = (1, 5).
  2. Step size: ρ2=1/(2+1)=1/3\rho_2 = 1/(2 + 1) = 1/3.
  3. Blend: λ(2)=(2/3)(3,1)+(1/3)(1,5)=(2,2/3)+(1/3,5/3)=(7/3,7/3)\lambda^{(2)} = (2/3)(3, 1) + (1/3)(1, 5) = (2, 2/3) + (1/3, 5/3) = (7/3, 7/3).
  4. Mean: (7/3)/(14/3)=1/2(7/3) / (14/3) = 1/2.
  5. Heads: x3=1x_3 = 1, so λ^3=(5,1)\hat{\lambda}_3 = (5, 1).
  6. Step size: ρ3=1/(3+1)=1/4\rho_3 = 1/(3 + 1) = 1/4.
  7. Blend: λ(3)=(3/4)(7/3,7/3)+(1/4)(5,1)=(7/4,7/4)+(5/4,1/4)=(3,2)\lambda^{(3)} = (3/4)(7/3, 7/3) + (1/4)(5, 1) = (7/4, 7/4) + (5/4, 1/4) = (3, 2).
  8. Mean: 3/5=0.63 / 5 = 0.6.

Interpretation: after three of the four flips, the mean is 0.60.6. The exact answer for all four is 4/6=0.674 / 6 = 0.67. The estimate keeps moving toward the truth, and each update used one flip.

Example
Example 3: the step size on a real topic model (realistic)

The paper's topic-model comparison uses forgetting rate κ=0.9\kappa = 0.9 and delay τ=1\tau = 1, with minibatches of 500 documents. Take the iteration t=1000t = 1000.

  1. Shifted iteration: t+τ=1000+1=1001t + \tau = 1000 + 1 = 1001.
  2. Power: 1001−0.9=1/10010.91001^{-0.9} = 1 / 1001^{0.9}. Since log⁡1001≈6.909\log 1001 \approx 6.909 and 0.9⋅6.909≈6.2180.9 \cdot 6.909 \approx 6.218, we get 10010.9≈e6.218≈5011001^{0.9} \approx e^{6.218} \approx 501.
  3. Step size: ρ1000≈1/501≈0.002\rho_{1000} \approx 1/501 \approx 0.002.
  4. Meaning: at iteration 1000 the global parameter moves about 0.2 percent of the way toward the intermediate estimate. That estimate comes from 500 documents, so each update is averaged over a lot of text and changes little.

Try it: with κ=0.5\kappa = 0.5 and τ=1\tau = 1, what is ρ99\rho_{99}? Answer: ρ99=(99+1)−0.5=1/100=0.1\rho_{99} = (99 + 1)^{-0.5} = 1/\sqrt{100} = 0.1. The steps shrink much more slowly when κ\kappa is near 0.50.5, which is why the paper finds that values near 11 work better.

Common mistakes and tradeoffs

It is tempting to think that stochastic variational inference is an approximation of an approximation, so it must be much worse than the classical method. In fact the intermediate parameter has the same expected value as the full coordinate update, because it is an unbiased estimate of that update. The noise is what you pay for speed, and the decreasing step size averages it out.

It is tempting to think that a larger step is always better, since it makes progress faster. In fact a step near one makes the global parameter follow the last few documents and forget the rest. The conditions require the step sizes to shrink, and the paper's experiments show that a forgetting rate near one reaches a better local optimum than a fast-forgetting one.

It is tempting to think that the method finds the true posterior. In fact it finds a local optimum of the lower bound, which is an approximation in two ways: the mean-field family ignores dependence between hidden variables, and the optimizer may stop at a local optimum. The paper's evaluation measures predictive likelihood on held-out documents, not how close the approximation is to the posterior.

When to use it: a model with a tractable exponential-family structure, and data too large for full passes. When not to use it: small data where a full coordinate pass is cheap, models whose conditionals are not in the exponential family, and problems that need calibrated posterior uncertainty. Alternatives include Markov chain Monte Carlo, which samples from the posterior instead of optimizing a bound.

Results

The experiments fit latent Dirichlet allocation (LDA) and the hierarchical Dirichlet process (HDP) topic model on three collections. Nature has 350,000 documents, 58 million observed words and a vocabulary of 4,200 terms. The New York Times collection has 1.8 million documents, 461 million words and 8,000 terms. Wikipedia has 3.8 million documents, 482 million words and 7,700 terms. Each collection holds out 10,000 documents as a test set.

The model is scored by predictive log likelihood: fit the topics on the training documents, observe part of a test document, and measure the log probability the model gives to the held-out words. A better model assigns them higher probability.

  • Model comparison. With the forgetting rate at 0.9 and minibatches of 500, the HDP gives consistently better predictive performance than LDA. LDA overfits as the number of topics grows, while the HDP stays robust. Traditional variational inference on subsets of each corpus did not perform as well (Figure 11).
  • Forgetting rate. For the HDP, a forgetting rate close to one leads to a better local optimum on all three corpora (Figure 12). For LDA, slower forgetting rates are also preferred (Figure 14).
  • Minibatch size. Batches of ten documents do worse, and sizes of 500 and 1,000 differ little. The New York Times corpus is the most sensitive to batch size and Wikipedia the least (Figure 13).

Limits: the evaluation is predictive likelihood on held-out text, not accuracy against a known answer. Every result is on topic models, and the method needs the forgetting rate and batch size to be tuned. The guarantee is convergence to a local optimum of the lower bound, so different settings can land in different solutions.

Reading guide

Recap

  • Variational inference turns posterior inference into maximizing the evidence lower bound over a simple family of distributions.
  • Mean-field coordinate ascent alternates local and global updates, and each global update needs every data point.
  • The natural gradient rescales steps so they follow changes in the distribution rather than in its parameters, and a natural gradient step of size one equals a coordinate update.
  • Stochastic variational inference samples one data point, forms the global parameter it would imply if repeated NN times, and blends that into the running estimate with a shrinking step.
  • The step sizes follow the Robbins and Monro conditions, which give convergence to a local optimum.

Next, read Topic Model for the application, Dirichlet Distribution for the priors on topics and mixtures, and Markov Chain Monte Carlo for the main alternative to variational approximations.

Check yourself

QuestionWhy does the classical coordinate update for the global parameter require a full pass over the data?Show answer

The global update is the expected natural parameter of the global complete conditional, which sums terms over every data point. Each sum term needs its own local parameters, so every document must be analyzed before the global parameter can be set.

QuestionIn the coin example, the prior is (1,1)(1, 1), N=4N = 4 and the sampled flip is tails, so x=0x = 0. Compute the intermediate parameter λ^\hat{\lambda}.Show answer

λ^=(1,1)+4⋅(0,1)=(1,5)\hat{\lambda} = (1, 1) + 4 \cdot (0, 1) = (1, 5). The tails count is the one that grows by four, because the flip is repeated four times as if it were the whole data set.

QuestionA learner uses ρt=(t+1)−1\rho_t = (t + 1)^{-1} and the current value λ(2)=(7/3,7/3)\lambda^{(2)} = (7/3, 7/3) with intermediate λ^3=(5,1)\hat{\lambda}_3 = (5, 1). What is the next value of λ\lambda, and what is its mean?Show answer

The step size is ρ3=1/4\rho_3 = 1/4, so λ(3)=(3/4)(7/3,7/3)+(1/4)(5,1)=(3,2)\lambda^{(3)} = (3/4)(7/3, 7/3) + (1/4)(5, 1) = (3, 2). The mean is 3/5=0.63 / 5 = 0.6.

QuestionWhy might a forgetting rate close to one work better than one close to one half, according to the paper's topic-model experiments?Show answer

A forgetting rate near one shrinks the step sizes quickly, so each update changes the global parameter less and averages over more of the recent history. The paper's experiments found that rates close to one reach a better local optimum. A rate near one half keeps steps large for longer, so the parameter tracks the last few minibatches rather than averaging over many of them.

QuestionA researcher says stochastic variational inference finds the true posterior of a topic model. What is wrong with this claim?Show answer

The method maximizes the evidence lower bound over a mean-field family, so it returns the best member of that family, not the posterior itself. The mean-field family drops dependence between hidden variables, and the optimizer converges only to a local optimum.

Sources

Read the full paper

Matt Hoffman, David M. Blei, Chong Wang et al., 2012. arXiv:1206.7051