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 points and a minibatch of size , the exact ELBO gradient with respect to a variational parameter (say the mean of a mean-field Gaussian approximation) sums a per-point term over all points. SVI approximates that sum by averaging over the minibatch and rescaling by :
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 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. ) lets the noise wash out over iterations while the parameter still converges.
Worked example 1: one update by hand
Suppose data points, minibatch , fixed noise variance , current mean estimate . The minibatch values are , and the per-point gradient of the log-likelihood term with respect to is . Average over the minibatch: . Scale by : . That huge number is exactly why SVI uses a tiny step size — with , the update is , nudging toward the data mean of about without ever touching the other 999,996 points.
Worked example 2: cost comparison
Full-batch VI on points needs one full pass (1,000,000 evaluations) per gradient step; to take 500 steps costs 500 million evaluations. SVI with costs only 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.
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 . 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 — using the raw minibatch-average gradient as if it were the full gradient silently shrinks every update by a factor of , 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)