Skip to content
BIASED

Stochastic variational inference

How to fit a variational approximation to data too large for full passes: global and local variables, conditional conjugacy, the natural gradient of the ELBO, minibatch updates with a Robbins–Monro step size, and online LDA.

Global and local variables

The topic Latent Dirichlet allocation ended with a cost problem. Mean-field VI for LDA updates the proportions γd\gamma_d and responsibilities rdnr_{dn} of every document, and only then recomputes the topics λ\lambda. For a corpus of a million documents the topics move once per million local updates. This topic makes them move after every small batch.

The trick works for a whole class of models. Their joint density splits into a part shared by all objects and one factor per object:

p(x,z,θ)=p(θ)∏i=1np(xi,zi∣θ).p(x, z, \theta) = p(\theta) \prod_{i=1}^{n} p(x_i, z_i \mid \theta).

Here θ\theta are the global variables: every object depends on them. Each ziz_i is a local variable: it belongs to object xix_i alone, and given θ\theta the pair (xi,zi)(x_i, z_i) is independent of all other pairs. In LDA the topics ϕk\phi_k are global, while the proportions θd\theta_d and the assignments zdnz_{dn} of a document are local; so in LDA the global variables are written ϕ\phi, and the letter θd\theta_d keeps its LDA meaning.

The running example is the two-component Gaussian mixture of the topic EM and Bayesian PCA, now with a prior on the means. The components have unit variances, and to keep the example small their weights are fixed at 12\tfrac12. Each mean gets a broad prior:

μk∼N(0,σ02),σ02=100,zi∼Cat⁡(12,12),xi∣zi,μ∼N(μzi,1).\mu_k \sim \mathcal N(0, \sigma_0^2), \quad \sigma_0^2 = 100, \qquad z_i \sim \operatorname{Cat}(\tfrac12, \tfrac12), \qquad x_i \mid z_i, \mu \sim \mathcal N(\mu_{z_i}, 1).

The means θ=(μ1,μ2)\theta = (\mu_1, \mu_2) are global, the labels ziz_i are local.

The mean-field family mirrors the split: q(θ∣λ)∏iq(zi∣ξi)q(\theta \mid \lambda) \prod_i q(z_i \mid \xi_i), with a global variational parameter λ\lambda and local parameters ξi\xi_i (for the mixture, the responsibilities rik=q(zi=k)r_{ik} = q(z_i = k)). The ELBO splits the same way, into a global term and a sum of nn local ones:

L(λ,ξ)=Eq[log⁡p(θ)]−Eq[log⁡q(θ∣λ)]+∑i=1n(Eq[log⁡p(xi,zi∣θ)]−Eq[log⁡q(zi∣ξi)]).\mathcal L(\lambda, \xi) = \mathbb E_q[\log p(\theta)] - \mathbb E_q[\log q(\theta \mid \lambda)] + \sum_{i=1}^{n} \Big( \mathbb E_q[\log p(x_i, z_i \mid \theta)] - \mathbb E_q[\log q(z_i \mid \xi_i)] \Big).

Coordinate ascent from the topic Variational inference alternates two steps: update all nn local parameters, then update λ\lambda. The first pass is the most wasteful one: nn local updates are computed under a random initial λ\lambda, and all of them are used for a single global step.

Conditional conjugacy and the coordinate update

To write the global update in closed form we need one assumption, conditional conjugacy: the complete conditional of the global variables, their distribution given everything else, lies in an exponential family,

p(θ∣x,z)=h(θ)exp⁡ ⁣(ηg(x,z)⊤u(θ)−Ag(ηg(x,z))),ηg(x,z)=λ0+∑i=1ns(xi,zi).p(\theta \mid x, z) = h(\theta) \exp\!\big( \eta_g(x, z)^\top u(\theta) - A_g(\eta_g(x, z)) \big), \qquad \eta_g(x, z) = \lambda_0 + \sum_{i=1}^{n} s(x_i, z_i).

This is the general conjugate prior from the topic Conjugate priors and the exponential family, applied to the complete-data likelihood p(xi,zi∣θ)p(x_i, z_i \mid \theta): λ0\lambda_0 is the natural parameter of the prior, s(xi,zi)s(x_i, z_i) the sufficient statistics of one object, and AgA_g the log-partition function. Note what is not required: the prior need not be conjugate to p(xi∣θ)p(x_i \mid \theta) with ziz_i summed out. For a mixture it is not.

Take q(θ∣λ)q(\theta \mid \lambda) from the same family, with natural parameter λ\lambda; then λ=λ0\lambda = \lambda_0 is the prior itself. The mean-field rule log⁡q∗(θ)=Eq(z)[log⁡p(x,z,θ)]+const\log q^*(\theta) = \mathbb E_{q(z)}[\log p(x, z, \theta)] + \text{const} gives the coordinate update

λ=Eξ[ηg(x,z)]=λ0+∑i=1nEξi[s(xi,zi)].\lambda = \mathbb E_{\xi}[\eta_g(x, z)] = \lambda_0 + \sum_{i=1}^{n} \mathbb E_{\xi_i}[s(x_i, z_i)].

The local update has the same shape with the local complete conditional p(zi∣xi,θ)p(z_i \mid x_i, \theta), and it touches only object ii.

In the mixture, use the natural parameters of a Gaussian from that topic: q(μk)∝exp⁡(λk1μk+λk2μk2)q(\mu_k) \propto \exp(\lambda_{k1} \mu_k + \lambda_{k2} \mu_k^2), so u(μk)=(μk,μk2)u(\mu_k) = (\mu_k, \mu_k^2) and

λk=(Pkmk, −12Pk),\lambda_k = \big(P_k m_k,\ -\tfrac12 P_k\big),

where mkm_k is the mean of q(μk)q(\mu_k) and PkP_k its precision. The prior gives λ0=(0,−12σ02)\lambda_0 = (0, -\tfrac{1}{2\sigma_0^2}), and object ii contributes s=([zi=k] xi, −12[zi=k])s = \big([z_i = k]\, x_i,\ -\tfrac12 [z_i = k]\big). In plain terms the update reads

Pk=1σ02+∑i=1nrik,Pkmk=∑i=1nrikxi,P_k = \frac{1}{\sigma_0^2} + \sum_{i=1}^{n} r_{ik}, \qquad P_k m_k = \sum_{i=1}^{n} r_{ik} x_i,

and the local step is rik∝exp⁡ ⁣(ximk−12(mk2+1/Pk))r_{ik} \propto \exp\!\big( x_i m_k - \tfrac12 (m_k^2 + 1/P_k) \big). Precisions add, as in the Normal mean with known variance; the global sum runs over all nn objects.

The natural gradient of the ELBO

Now look at the same update as an optimization step. Fix the local parameters and keep only the terms of the ELBO that depend on λ\lambda. Using Eq[u(θ)]=∇Ag(λ)\mathbb E_q[u(\theta)] = \nabla A_g(\lambda) and writing ηˉ=Eξ[ηg(x,z)]\bar\eta = \mathbb E_\xi[\eta_g(x, z)],

L(λ)=(ηˉ−λ)⊤∇Ag(λ)+Ag(λ)+const,∇λL=∇2Ag(λ) (ηˉ−λ).\mathcal L(\lambda) = (\bar\eta - \lambda)^\top \nabla A_g(\lambda) + A_g(\lambda) + \text{const}, \qquad \nabla_\lambda \mathcal L = \nabla^2 A_g(\lambda)\, (\bar\eta - \lambda).

If the ξi\xi_i are themselves optimal for the current λ\lambda, the gradient through them vanishes, so this is also the gradient of the ELBO as a function of λ\lambda alone.

The matrix G(λ)=∇2Ag(λ)G(\lambda) = \nabla^2 A_g(\lambda) is the covariance of u(θ)u(\theta) under qq, which is the Fisher information of q(θ∣λ)q(\theta \mid \lambda). The natural gradient multiplies the ordinary gradient by G−1G^{-1}:

∇^λL=G(λ)−1∇λL=ηˉ−λ.\hat\nabla_\lambda \mathcal L = G(\lambda)^{-1} \nabla_\lambda \mathcal L = \bar\eta - \lambda.

Why G−1G^{-1}? An ordinary gradient is the steepest direction when a step is measured by the Euclidean length of dλd\lambda, which depends on how we chose to write qq. The natural gradient is the steepest direction when a step is measured by how much the distribution changes: KL(qλ ∥ qλ+dλ)≈12 dλ⊤G(λ) dλ\mathrm{KL}(q_\lambda \,\|\, q_{\lambda + d\lambda}) \approx \tfrac12\, d\lambda^\top G(\lambda)\, d\lambda. That makes it independent of the parameterization of qq.

The payoff: a natural-gradient step of length ρ\rho is a convex blend, λ+ρ(ηˉ−λ)=(1−ρ)λ+ρ ηˉ\lambda + \rho(\bar\eta - \lambda) = (1 - \rho)\lambda + \rho\,\bar\eta, and the step of length 11 is exactly coordinate ascent. No Fisher matrix is ever formed or inverted.

A worked case without local variables: the Normal mean with known variance 11, prior N(0,100)\mathcal N(0, 100), and ten points with mean 22. The data add (∑ixi, −n2)=(20,−5)(\sum_i x_i,\ -\tfrac{n}{2}) = (20, -5) to λ0=(0,−0.005)\lambda_0 = (0, -0.005), so ηˉ=(20,−5.005)\bar\eta = (20, -5.005). Start from q=N(0,1)q = \mathcal N(0, 1), that is λ=(0,−0.5)\lambda = (0, -0.5). The natural gradient is (20,−4.505)(20, -4.505), and the step of length 11 lands on ηˉ\bar\eta: the exact posterior, with precision 10.0110.01 and mean 20/10.01≈1.99820 / 10.01 \approx 1.998. The ordinary gradient G (20,−4.505)G\,(20, -4.505) has a different direction and length; the check below shows where it lands.

The minibatch update

The natural gradient ηˉ−λ\bar\eta - \lambda still contains a sum over all nn objects. Replace it with an unbiased estimate. Draw a minibatch SS of BB objects uniformly at random, fit their local parameters under the current λ\lambda, and pretend the dataset consists of n/Bn/B copies of the minibatch. This gives the intermediate global parameter

λ^S=λ0+nB∑i∈SEξi[s(xi,zi)].\hat\lambda_S = \lambda_0 + \frac{n}{B} \sum_{i \in S} \mathbb E_{\xi_i}[s(x_i, z_i)].

Each object enters SS with probability B/nB/n, so ES[λ^S]=ηˉ\mathbb E_S[\hat\lambda_S] = \bar\eta, and λ^S−λ\hat\lambda_S - \lambda is an unbiased estimate of the natural gradient. A step of length ρt\rho_t along it is

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

One iteration of stochastic variational inference (SVI) is therefore: sample SS; run the local updates for i∈Si \in S only; form λ^S\hat\lambda_S; blend it into λ\lambda; forget the ξi\xi_i. An iteration costs O(B)O(B) instead of O(n)O(n), and the local parameters of the other objects are never stored.

A worked update for component 11 of the mixture. Since λ1\lambda_1 is linear in P1m1P_1 m_1 and P1P_1, blending λ1\lambda_1 is the same as blending these two numbers. Let n=10 000n = 10\,000, B=4B = 4, and let the current factor be m1=1m_1 = 1, P1=2000P_1 = 2000, so P1m1=2000P_1 m_1 = 2000. The minibatch has x=(2.5, 3.0, −1.0, 2.1)x = (2.5,\ 3.0,\ -1.0,\ 2.1) with responsibilities ri1=(0.9, 1.0, 0, 0.6)r_{i1} = (0.9,\ 1.0,\ 0,\ 0.6), so ∑ri1xi=6.51\sum r_{i1} x_i = 6.51 and ∑ri1=2.5\sum r_{i1} = 2.5. With n/B=2500n/B = 2500 the intermediate values are

Pm^=2500⋅6.51=16 275,P^=0.01+2500⋅2.5=6250.01.\widehat{P m} = 2500 \cdot 6.51 = 16\,275, \qquad \hat P = 0.01 + 2500 \cdot 2.5 = 6250.01.

With ρ=0.1\rho = 0.1: P1m1=0.9⋅2000+0.1⋅16 275=3427.5P_1 m_1 = 0.9 \cdot 2000 + 0.1 \cdot 16\,275 = 3427.5 and P1=0.9⋅2000+0.1⋅6250.01≈2425P_1 = 0.9 \cdot 2000 + 0.1 \cdot 6250.01 \approx 2425, so m1≈1.413m_1 \approx 1.413. Blend natural parameters, never means and variances: the mean moves by a precision-weighted amount, not by ρ\rho times the gap.

Step sizes and the Robbins–Monro conditions

Unrolling the update shows what ρt\rho_t does. The estimate λ^s\hat\lambda_s from step ss enters λ(t)\lambda^{(t)} with weight ρs∏u=s+1t(1−ρu)\rho_s \prod_{u = s+1}^{t} (1 - \rho_u).

  • With ρt=1/t\rho_t = 1/t every weight equals 1/t1/t: λ(t)\lambda^{(t)} is the plain average of all minibatch estimates so far. In a model without local variables (the Normal mean) the estimates are independent and unbiased, and their average converges to the exact posterior parameter.
  • With a constant ρ\rho the weights decay geometrically: old minibatches are forgotten, the noise never averages out, and λ\lambda ends up jittering in a cloud around the optimum whose size grows with ρ\rho.

The general answer is the Robbins–Monro conditions:

∑t=1∞ρt=∞,∑t=1∞ρt2<∞.\sum_{t=1}^{\infty} \rho_t = \infty, \qquad \sum_{t=1}^{\infty} \rho_t^2 < \infty.

The first lets λ\lambda travel any distance from a bad start; the second keeps the total injected noise finite. Under them, and mild regularity, SVI converges to a local optimum of the ELBO. The standard schedule is

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

with the forgetting rate κ\kappa and the delay τ\tau. The delay damps the first steps: with τ=0\tau = 0 the first step has ρ1=1\rho_1 = 1 and replaces λ\lambda entirely by one noisy minibatch estimate, so a component that the batch barely touched is reset to the prior.

A simulation on the mixture with n=100 000n = 100\,000 points and true means ±2\pm 2: coordinate ascent needs 22 full passes to come within 0.0010.001 nats per point of its optimal ELBO, while SVI with B=100B = 100, τ=1\tau = 1, κ=0.7\kappa = 0.7 gets there after about 0.0050.005 of a pass, that is, half a dozen minibatches. With a constant ρ=0.1\rho = 0.1 the means keep jittering with a standard deviation of about 0.030.03, roughly seven times the posterior standard deviation 0.00450.0045; the decaying schedule shrinks the jitter to the posterior's own scale.

SVI for LDA

SVI was introduced for LDA as online LDA (Hoffman, Blei and Bach). We keep the notation of the topic Latent Dirichlet allocation; the paper writes βk\beta_k for the topics, η\eta for their prior and ϕdn\phi_{dn} for the responsibilities. There are DD documents, topics ϕk∼Dir⁡(β)\phi_k \sim \operatorname{Dir}(\beta), proportions θd∼Dir⁡(α)\theta_d \sim \operatorname{Dir}(\alpha), assignments zdnz_{dn} and words wdnw_{dn}. The global factor is q(ϕk∣λk)=Dir⁡(λk)q(\phi_k \mid \lambda_k) = \operatorname{Dir}(\lambda_k), the local ones are q(θd∣γd)q(\theta_d \mid \gamma_d) and q(zdn∣rdn)q(z_{dn} \mid r_{dn}), and the prior parameter λ0\lambda_0 is the vector β\beta.

Local step for each document dd of the minibatch: starting from some γd\gamma_d, repeat until it stops changing

rdnk∝exp⁡ ⁣(Eq[log⁡θdk]+Eq[log⁡ϕkwdn]),γdk=α+∑nrdnk.r_{dnk} \propto \exp\!\big( \mathbb E_q[\log \theta_{dk}] + \mathbb E_q[\log \phi_{k w_{dn}}] \big), \qquad \gamma_{dk} = \alpha + \sum_{n} r_{dnk}.

Global step: count the expected uses of word vv by topic kk in the minibatch, scale to the corpus and add the prior,

λ^kv=β+DB∑d∈S∑nrdnk [wdn=v],λ←(1−ρt) λ+ρt λ^.\hat\lambda_{kv} = \beta + \frac{D}{B} \sum_{d \in S} \sum_{n} r_{dnk}\, [w_{dn} = v], \qquad \lambda \leftarrow (1 - \rho_t)\, \lambda + \rho_t\, \hat\lambda.

On the tiny corpus of that topic (D=3D = 3, β=0.1\beta = 0.1), a minibatch of document 3 alone counts as three copies of it: if its token goal has r3n1=0.9r_{3n1} = 0.9, then λ^1,goal=0.1+3⋅0.9=2.8\hat\lambda_{1,\text{goal}} = 0.1 + 3 \cdot 0.9 = 2.8.

Batch mean-field VI for LDA is the same pair of formulas with B=DB = D and ρ=1\rho = 1. The difference is what SVI buys: memory for λ\lambda alone (K×VK \times V numbers) instead of γd\gamma_d and rdnr_{dn} for the whole corpus, topics that improve from the first minibatch, and documents that can arrive as a stream and be seen once.

Where SVI falls short

SVI is coordinate ascent made stochastic, and it inherits every assumption of the closed-form update.

  • Conditional conjugacy. ηˉ=Eξ[ηg]\bar\eta = \mathbb E_\xi[\eta_g] must be computable. A logistic likelihood (the topic The Laplace approximation and Bayesian classification) or a neural-network decoder breaks it: the complete conditional is no longer in an exponential family.
  • A cheap local step. Each minibatch object needs its own inner optimization; if that is expensive, it dominates the cost.
  • Mean-field limits. Reverse KL is mode-seeking, so variances come out too small and local optima remain.
  • Tuning. BB, τ\tau and κ\kappa matter, and the full ELBO costs a pass over the data to evaluate, so in practice one monitors the predictive likelihood of held-out data.

The first limit is the deepest. The natural gradient ηˉ−λ\bar\eta - \lambda exists in closed form only because of conjugacy. Without it, we can still write the gradient of the ELBO as an expectation under qq and estimate it from samples of qq, for any model whose log⁡p(x,z)\log p(x, z) we can evaluate. That is black-box variational inference, the next topic.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.