Stochastic variational inference

Also known as: SVI

On this page 7

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:

  1. Sample a minibatch of data points uniformly at random.
  2. Optimize the local variational parameters for those points while holding the global parameters fixed.
  3. 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.
  4. 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.

ParameterTypical settingEffect
Minibatch sizeTens to thousands of pointsLarger batches reduce gradient noise but cost more per step
Step size schedulerho_t = (t + tau) to the power of minus kappaControls how fast new information replaces old estimates
Forgetting rate kappaBetween 0.5 and 1Required range for convergence guarantees
Delay tau1 to 1,000Damps the influence of early, unreliable iterations
Local iterationsUntil local parameters stabilizeToo few steps bias the global gradient
Main SVI parameters and their effects

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 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.

MethodScales to large dataMain limitation
Batch variational inferencePoorlyEach update touches every data point
Stochastic variational inferenceVery wellApproximation bias from the variational family
Markov chain Monte CarloPoorly without special variantsSlow mixing on large or high-dimensional problems
Stochastic gradient MCMCWellHarder to diagnose convergence
Amortized inferenceVery wellAmortization gap from the shared encoder
SVI and neighboring inference approaches

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.

SituationRecommended approachReason
Millions of documents for topic modelingSVI with natural gradientsConstant cost per update and closed-form steps
Streaming data arriving continuouslySVI with a fixed small stepTracks a slowly changing posterior
Small dataset, accurate uncertainty neededHamiltonian Monte CarloAsymptotically exact samples
Deep generative model with neural encoderAmortized SVIOne network infers local variables for all points
Choosing an inference strategy

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.

SymptomLikely causeFix
ELBO oscillates without settlingStep size too largeIncrease tau or kappa
Global parameters barely changeStep size decays too fastLower kappa toward 0.5
Topics collapse or duplicatePoor initializationUse several restarts and compare ELBO
Results depend on batch orderNon-random minibatchesShuffle data before sampling
Typical SVI problems and their fixes

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.