Two independent sources of stochasticity turned VI from a model-by-model derivation into a general-purpose tool. Stochastic variational inference (SVI) (Hoffman et al. 2013; Blei et al. 2017, Sec. 4.3) subsamples data: for conditionally conjugate models the natural gradient of the ELBO is “coordinate update minus current value,” and a single rescaled data point gives an unbiased estimate of it. Black-box variational inference (BBVI) (Ranganath et al. 2014) samples latent variables: it writes ∇λELBO=Eq[∇λlogq(z∣λ)(logp(x,z)−logq(z∣λ))] and estimates it by Monte Carlo, requiring only evaluations of logp(x,z). The score-function estimator is fully general but high-variance, which is why differentiable models use the reparameterization gradient instead.
Overview
CAVI has two costs. Computational: “the coordinate ascent structure of the algorithm requires iterating through the entire data set at each iteration” (Blei et al., Sec. 4.3). Human: every new model needs its expectations derived by hand, and for nonconjugate models (even Bayesian logistic regression, Sec. 5.3) those expectations have no closed form. Ranganath et al. describe the second cost as “tedious bookkeeping” that “hinders us from rapidly exploring modeling assumptions.”
Both costs are removed by the same idea: replace exact gradients of the ELBO with noisy unbiased gradients and use stochastic approximation.
Robbins-Monro conditions ^thm-robbins-monro
Stochastic gradient ascent λt+1=λt+ρtg^t with E[g^t]=∇f(λt) converges to a (local) optimum when the step sizes satisfy
t∑ρt=∞,t∑ρt2<∞,
e.g. ρt=t−κ with κ∈(0.5,1] (Blei et al., Eq. 58; Ranganath et al., Sec. 2). In practice both papers use adaptive per-coordinate rates (AdaGrad, ρt=ηdiag(Gt)−1/2 with Gt the running sum of gradient outer products), which shrink the step where gradient variance is large.
Main Content
Stochastic VI: subsampling the data
Setting: the conditionally conjugate model of the CAVI note with global variational parameter λ, local parameters φi, and conjugate-prior natural parameter α.
Natural gradient of the ELBO ^thm-natural-gradient
The Euclidean gradient is ∇λELBO=a′′(λ)(Eφ[α^]−λ), where Eφ[α^] is the CAVI global update. The natural gradient premultiplies by the inverse Fisher information a′′(λ)−1 (the inverse covariance of the sufficient statistic), giving
g(λ)=Eφ[α^]−λ
(Blei et al., Eqs. 51-52). Natural gradients “warp the parameter space in a sensible way, so that moving the same distance in different directions amounts to equal change in symmetrized KL divergence.” A step of size εt is a convex combination: λt=(1−εt)λt−1+εtEφ[α^] (Eq. 54).
Since Eφ[α^]=α+[∑iEφi∗[t(zi,xi)],n]⊤ is a sum over data, sample one index t∼Unif(1,…,n) and rescale:
Sample a data point t∼Unif(1,…,n) (or a minibatch).
Optimize its local parameters, φt∗=Eλ[η(β,xt)].
Form the coordinate update as though xt were repeated n times: λ^=α+nEφt∗[t(zt,xt)].
λ←(1−εt)λ+εtλ^.
“SVI requires no new derivation beyond what is needed for CAVI. Any implementation of CAVI can be immediately scaled up to a stochastic algorithm.”
The showcase is latent Dirichlet allocation on 1.8M New York Times articles (Blei et al., Fig. 7), where CAVI-era topic models handled only thousands to tens of thousands of documents.
Black-box VI: sampling the latent variables
Score-function gradient of the ELBO ^thm-score-gradient
For a variational family q(z∣λ),
∇λL=Eq[∇λlogq(z∣λ)(logp(x,z)−logq(z∣λ))],
with Monte Carlo estimate S1∑s=1S∇λlogq(zs∣λ)(logp(x,zs)−logq(zs∣λ)), zs∼q(⋅∣λ) (Ranganath et al., Eqs. 2-3). The derivation uses ∇λq=q∇λlogq (the log-derivative identity) and the fact that the score has mean zero, Eq[∇λlogq(z∣λ)]=0.
Why “black box”: “the score function and sampling algorithms depend only on the variational distribution, not the underlying model.” The practitioner supplies a function returning logp(x,z); nothing else about the model is used, not even its gradient. Discrete latent variables are therefore allowed, which the reparameterization approach cannot handle.
The catch is variance. Kingma & Welling (2013, Sec. 2.2) call this “naive” estimator one that “exhibits very high variance… and is impractical for our purposes.” Ranganath et al. agree that the raw estimator’s variance “can be too large to be useful” and add two model-agnostic variance reductions.
Rao-Blackwellization ^def-rao-blackwell
Replace a function by its conditional expectation: J^(X)=E[J(X,Y)∣X] has the same mean and variance reduced by E[(J−J^)2]. For a mean-field family q(z∣λ)=∏iq(zi∣λi), integrating out everything outside the Markov blanket of zi gives
where pi collects only the factors of the joint that involve zi (Ranganath et al., Eq. 5). No model-specific integral is needed: one simply drops the irrelevant terms of logp.
Control variates ^def-control-variate
For any h with known mean, f^(z)=f(z)−a(h(z)−E[h(z)]) has E[f^]=E[f] and Var(f^)=Var(f)+a2Var(h)−2aCov(f,h), minimized at a∗=Cov(f,h)/Var(h) (Ranganath et al., Eq. 7). BBVI uses the score itself, h=∇λlogq(z∣λ), whose mean is exactly zero for every family; a∗ is estimated from the same samples. Algorithm 2 of the paper combines both reductions with AdaGrad and data subsampling (“doubly stochastic”).
Score-function versus reparameterization gradients
Score function (BBVI)
Reparameterization (ADVI, VAE)
Needs from model
logp(x,z) values
∇zlogp(x,z)
Needs from q
∇λlogq, sampler
differentiable sampler z=gλ(ϵ)
Discrete z
Yes
No
Variance
High; needs Rao-Blackwellization and control variates
Low; “a single sample suffices”
Kucukelbir et al. (2017, Sec. 3.2, Fig. 8) compare the two on a univariate model with a Gamma(10,10) posterior and on a 100-dimensional nonlinear regression with likelihood N(y∣tanh(x⊤β),I). Across M=1 to 103 Monte Carlo samples the reparameterization gradient has lower variance than BBVI, including BBVI with control variates: “While BBVI is more general… its gradients can suffer from high variance.” The intuition is that the score estimator only learns about logp through scalar values at sampled points, while the pathwise estimator uses the model’s gradient, which says in which direction to move each sample. The ADVI authors suggest the score estimator as the route to discrete latents, “with some care as these gradients will exhibit higher variance” (Sec. 5).
Examples
BBVI in a dozen lines ^ex-bbvi-code
Mean-field Gaussian q, score-function gradient with the score as control variate (per-coordinate a∗):
import numpy as npdef bbvi_step(log_joint, mu, log_sig, S=200, lr=0.01, rng=np.random.default_rng()): sig = np.exp(log_sig) z = mu + sig * rng.standard_normal((S, mu.size)) # z_s ~ q logq = -0.5 * (((z - mu) / sig) ** 2).sum(1) - log_sig.sum() w = np.array([log_joint(zs) for zs in z]) - logq # log p(x,z) - log q(z) score = np.hstack([(z - mu) / sig**2, # d log q / d mu ((z - mu) / sig) ** 2 - 1.0]) # d log q / d log_sig f = score * w[:, None] a = (((f - f.mean(0)) * (score - score.mean(0))).mean(0) / score.var(0)) # a* = Cov(f,h)/Var(h) grad = (f - a * score).mean(0) return mu + lr * grad[:mu.size], log_sig + lr * grad[mu.size:]
log_joint is never differentiated. Swap the Gaussian for a Gamma or a categorical and only logq, score and the sampler change, which is the sense in which the method is a reusable library.
Applied reading. Ranganath et al. fit several Gamma and Gamma-Normal time-series factor models to longitudinal lab data from 976 chronic kidney disease patients (33K visits, 17 lab measurements), using 1,000 samples and batch size 25. Against Metropolis-Hastings-within-Gibbs on a 20-hour budget, BBVI reached better held-out predictive likelihood faster (their Fig. 1). The variance study (Sec. 5.4, Fig. 2) found that Rao-Blackwellization “reduces the variance by several orders of magnitude,” control variates reduce it further, and the plain estimator of Algorithm 1 “failed to make noticeable progress” in the time allotted. The broader point of that study is workflow: models “generally outside the realm of variational methods” could be proposed, fitted and compared by predictive likelihood without new derivations. This is the same iterate-quickly argument the Bayesian Workflow literature makes for approximate computation in early model building.