Quant Memo
Advanced

Stochastic Variational Inference

Fitting an approximate posterior to a huge dataset does not require looking at every data point on every update — a small random minibatch gives an unbiased peek at the direction to move, and enough small nudges land in the same place as looking at everything.

Prerequisites: The Evidence Lower Bound, The Mean-Field Approximation

Ordinary variational inference fits an approximate distribution to a posterior by climbing the ELBO (the evidence lower bound) with gradient ascent, but computing that gradient exactly requires summing a term over every single data point in the dataset on every single update. With a million rows, that is a million-row pass just to take one step. Stochastic variational inference (SVI) asks: what if each step only looked at a small random handful of rows, scaled up to stand in for the whole dataset?

The analogy: polling instead of a full census

To estimate an election result, you do not need to survey every voter — a well-chosen random sample of a few hundred people, scaled up to the size of the electorate, gives an estimate that is unbiased and, on average, points the right direction, even though any single poll is noisy. SVI treats each gradient update the same way: grab a random minibatch, compute the update as if the whole dataset looked like this minibatch repeated many times, and take a small step. Individual steps are noisy polls, not censuses, but averaged over many steps they walk to the same place a full census would have reached.

The mechanics

For a dataset of NN points and a minibatch of size MM, the exact ELBO gradient with respect to a variational parameter (say the mean mm of a mean-field Gaussian approximation) sums a per-point term over all NN points. SVI approximates that sum by averaging over the minibatch and rescaling by N/MN/M:

g^=NMiminibatchmi(m)\hat{g} = \frac{N}{M} \sum_{i \in \text{minibatch}} \nabla_m \, \ell_i(m)

In words: take the average gradient contribution from the points you actually looked at, then multiply by how many times bigger the full dataset is than your sample. This g^\hat{g} is an unbiased estimator of the true full-data gradient — its average over many random minibatches equals the true gradient exactly, even though any one minibatch's estimate is off. Because gradient ascent only needs the right direction on average, a shrinking step size (the Robbins-Monro schedule, e.g. ρt1/t\rho_t \propto 1/t) lets the noise wash out over iterations while the parameter still converges.

Worked example 1: one update by hand

Suppose N=1,000,000N = 1{,}000{,}000 data points, minibatch M=4M = 4, fixed noise variance σ2=1\sigma^2=1, current mean estimate mt=0m_t = 0. The minibatch values are x={2.0,2.4,1.6,2.2}x = \{2.0, 2.4, 1.6, 2.2\}, and the per-point gradient of the log-likelihood term with respect to mm is (xim)/σ2(x_i - m)/\sigma^2. Average over the minibatch: (2.0+2.4+1.6+2.2)/4=2.05(2.0+2.4+1.6+2.2)/4 = 2.05. Scale by N/M=250,000N/M = 250{,}000: g^=250,000×2.05=512,500\hat{g} = 250{,}000 \times 2.05 = 512{,}500. That huge number is exactly why SVI uses a tiny step size ρt\rho_t — with ρt=4×107\rho_t = 4\times 10^{-7}, the update is mt+1=0+4×107×512,5000.205m_{t+1} = 0 + 4\times10^{-7} \times 512{,}500 \approx 0.205, nudging mm toward the data mean of about 2.052.05 without ever touching the other 999,996 points.

Worked example 2: cost comparison

Full-batch VI on N=1,000,000N=1{,}000{,}000 points needs one full pass (1,000,000 evaluations) per gradient step; to take 500 steps costs 500 million evaluations. SVI with M=100M=100 costs only 100×500=50,000100 \times 500 = 50{,}000 evaluations for the same 500 steps — 10,000 times cheaper — and because each step is an unbiased estimate, it still converges to essentially the same fitted distribution, just with a noisier, wigglier path there.

500,000,000 full-batch 50,000 SVI (M=100)
Same 500 gradient steps, radically different total evaluation cost — the SVI bar is barely visible next to full-batch VI's.

Gradient descent
parameter →
position -0.623loss -0.141gradient -0.930converging

Watch how a noisy, jittery descent path still lands near the same minimum as a smooth one — that jitter is exactly what a minibatch gradient estimate looks like next to a full-batch one.

Stochastic variational inference replaces the exact ELBO gradient (summed over every data point) with an unbiased estimate from a random minibatch, rescaled by N/MN/M. A shrinking step size lets per-step noise average out, so it converges to the same fitted approximation as full-batch VI at a fraction of the per-step cost.

What this means in practice

SVI is why variational autoencoders, topic models, and Bayesian neural networks can be fit on datasets with millions of rows using ordinary minibatch training loops — the same infrastructure built for stochastic gradient descent on a supervised loss works unchanged, because the ELBO gradient estimator plugs into exactly that machinery. The price is that convergence diagnostics get harder: a noisy ELBO trace can look like it has plateaued when it has actually converged, or look like it is still moving when it is just sampling noise.

A common mistake is forgetting to rescale the minibatch gradient by N/MN/M — using the raw minibatch-average gradient as if it were the full gradient silently shrinks every update by a factor of N/MN/M, making training appear to converge (parameters barely move) when it is actually just taking steps far too small to matter. The rescaling is not optional bookkeeping; it is what keeps the estimator unbiased.

Related concepts

Practice in interviews

Further reading

  • Hoffman, Blei, Wang & Paisley, Stochastic Variational Inference (2013)
  • Blei, Kucukelbir & McAuliffe, Variational Inference: A Review for Statisticians (2017)
ShareTwitterLinkedIn