Stochastic variational inference (SVI) is a method for approximate Bayesian inference that optimizes a variational objective with noisy gradients computed on small random subsets of the data. Among probabilistic models, it is the standard way to fit latent variable models such as topic models and mixture models to datasets that are too large for classical batch algorithms.
SVI was formalized by Hoffman, Blei, Wang and Paisley in 2013. It combines variational inference, which turns posterior estimation into optimization, with stochastic optimization, which replaces full-data gradients with unbiased minibatch estimates. The result is an algorithm whose cost per update does not grow with dataset size.
How stochastic variational inference works
Variational inference chooses a tractable family of distributions q and adjusts its parameters to maximize the evidence lower bound (ELBO), which is equivalent to minimizing the Kullback-Leibler divergence from q to the true posterior. Classical coordinate ascent requires a full pass over all data for each update of the global parameters, which becomes prohibitive with millions of observations.
SVI exploits a common model structure. The model has global latent variables shared by all data points, such as topic distributions, and local latent variables tied to individual data points, such as per-document topic proportions. Each iteration follows a fixed loop:
- Sample a minibatch of data points uniformly at random.
- Optimize the local variational parameters for those points while holding the global parameters fixed.
- Compute an intermediate global estimate as if the minibatch were replicated to the size of the full dataset, scaling by N divided by the batch size.
- Move the global parameters a small step toward that estimate, using a decreasing step size.
For conditionally conjugate exponential-family models, the step in point four is a natural gradient step. Natural gradients follow the geometry of the distribution space rather than the raw parameter space, and in this model class they take a simple closed form, which makes each update cheap and numerically stable.
Key components and parameters
The behavior of SVI depends on a small number of tuning choices that control noise, speed and convergence. The table below summarizes them and their typical settings.
| Parameter | Typical setting | Effect |
|---|---|---|
| Minibatch size | Tens to thousands of points | Larger batches reduce gradient noise but cost more per step |
| Step size schedule | rho_t = (t + tau) to the power of minus kappa | Controls how fast new information replaces old estimates |
| Forgetting rate kappa | Between 0.5 and 1 | Required range for convergence guarantees |
| Delay tau | 1 to 1,000 | Damps the influence of early, unreliable iterations |
| Local iterations | Until local parameters stabilize | Too few steps bias the global gradient |
The step sizes must satisfy the Robbins-Monro conditions: their sum diverges while the sum of their squares converges. Under these conditions SVI converges to a local optimum of the ELBO, not necessarily the global one, so random restarts or careful initialization remain useful.
SVI compared with related inference methods
SVI occupies a position between exact but slow sampling methods and fast but rigid point estimates. The comparison below covers the methods practitioners most often weigh against it.
| Method | Scales to large data | Main limitation |
|---|---|---|
| Batch variational inference | Poorly | Each update touches every data point |
| Stochastic variational inference | Very well | Approximation bias from the variational family |
| Markov chain Monte Carlo | Poorly without special variants | Slow mixing on large or high-dimensional problems |
| Stochastic gradient MCMC | Well | Harder to diagnose convergence |
| Amortized inference | Very well | Amortization gap from the shared encoder |
Modern practice often generalizes SVI beyond conjugate models. Black-box variational inference estimates gradients by sampling from q, and the reparameterization trick lowers their variance for continuous variables. Libraries such as Pyro and NumPyro use the name SVI for this broader family of minibatch ELBO optimizers.
When to use stochastic variational inference
SVI is the right choice when data volume makes full passes impractical and an approximate posterior is acceptable. It is less suitable when calibrated uncertainty is the primary goal or the dataset is small.
| Situation | Recommended approach | Reason |
|---|---|---|
| Millions of documents for topic modeling | SVI with natural gradients | Constant cost per update and closed-form steps |
| Streaming data arriving continuously | SVI with a fixed small step | Tracks a slowly changing posterior |
| Small dataset, accurate uncertainty needed | Hamiltonian Monte Carlo | Asymptotically exact samples |
| Deep generative model with neural encoder | Amortized SVI | One network infers local variables for all points |
In practice, SVI is commonly applied to latent Dirichlet allocation, hierarchical Dirichlet processes, probabilistic matrix factorization and Bayesian neural networks. Its online character also allows models to be updated incrementally as new data arrives.
Limitations and common mistakes
SVI inherits the biases of variational inference. A mean-field family typically underestimates posterior variance, so credible intervals can appear narrower than they should be. Stochastic optimization adds its own problems, summarized below.
| Symptom | Likely cause | Fix |
|---|---|---|
| ELBO oscillates without settling | Step size too large | Increase tau or kappa |
| Global parameters barely change | Step size decays too fast | Lower kappa toward 0.5 |
| Topics collapse or duplicate | Poor initialization | Use several restarts and compare ELBO |
| Results depend on batch order | Non-random minibatches | Shuffle data before sampling |
A frequent error is forgetting the N over batch size scaling of the likelihood term, which makes the prior dominate and yields overly smooth posteriors. Another is judging convergence by a single noisy ELBO estimate instead of a moving average or a held-out predictive score.
A non-technical view
Imagine estimating the favorite topics of a whole country by reading every newspaper ever printed. That would take forever. Instead, you read a small random stack each day. You update your opinion a little after every stack.
Stochastic variational inference works the same way. It learns from small random samples of the data instead of all of it at once. Early updates count more, and later ones only fine-tune. The final answer is an approximation, but a fast and useful one.
A concrete example
A digital library holds 3.8 million scientific abstracts and wants to group them into 200 research themes for search and recommendations. A batch topic model would need many full passes over the collection, each taking hours on the available servers.
The data team fits latent Dirichlet allocation with SVI. It uses minibatches of 512 abstracts, a delay tau of 1,024 and a forgetting rate kappa of 0.7. Each update takes under a second, and the topics stabilize after roughly one pass through the corpus.
When 20,000 new abstracts arrive each week, the team continues the same optimization instead of retraining from scratch. Held-out perplexity stays stable, and curators confirm that the themes remain coherent and useful.