Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF003 · Algorithm

Stochastic variational inference (SVI)

Uses minibatch information to optimize a variational posterior over global latent quantities, avoiding a full-data coordinate pass at every update.

Partly runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method covers part of this entry; some options or variants aren't built yet.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Partly runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.topics. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Uses minibatch information to optimize a variational posterior over global latent quantities, avoiding a full-data coordinate pass at every update. Classical SVI exploits conjugate exponential-family structure and natural gradients; modern libraries also use SVI as a broader name for stochastic ELBO optimization.

Question / hypotheses

Can minibatches provide correctly scaled updates toward the full-data variational objective while local latent variables are fitted for the sampled observations?

Architecture / computational structure

Fit local factors for a minibatch, scale their expected statistics to represent N observations, construct a noisy global optimum, and move toward it. The Beta–Bernoulli component updates shape pseudo-counts; these are natural parameters plus one, so the same convex interpolation applies. No local optimization is required for that deliberately simple model.

Assumptions

The classical update assumes the stated conditional-conjugate model and an unbiased minibatch estimate of global sufficient statistics. Sampling weights must reflect nonuniform minibatches. Standard stochastic-approximation conditions require a diminishing schedule with divergent sum and square-summable squared steps; finite-budget practice needs empirical validation.

Formula / computational definition

λt+1=(1−ρt)λt+ρt(η+NB∑i∈BtEqi[t(xi,zi)]).\lambda_{t+1}=(1-\rho_t)\lambda_t+\rho_t\left(\eta+\frac{N}{B}\sum_{i\in\mathcal B_t}E_{q_i}[t(x_i,z_i)]\right).

Parameters and notation

Variational global natural parameters lambda are learned; eta represents prior natural parameters and t sufficient statistics. N is dataset size, B minibatch size. Hyperparameters include local-iteration budget, batch sampling, step schedule rho, initialization, and variational family. Natural parameters and moment parameters must not be mixed in this interpolation.

Fitting, fine-tuning, and adaptation

Continue variational optimization with a newly justified target or refit from a saved global state. Treat a true data stream explicitly: blindly reusing an old N or repeatedly adding the same batch changes the objective. Neural guides additionally require optimizer-state and trainable-module decisions; they are not implied by the conjugate update.

How to use and calibrate

Track minibatch ELBO estimates together with a stable held-out objective, not individual noisy steps alone. Compare batch sizes and learning schedules at matched processed-data budgets. Plot global moments versus iterations and check scaling against a full-data conjugate solution.

Example application

Fit a topic model to a large document corpus while maintaining global topic distributions and computing document-specific latent factors only for sampled documents.

Interpretation / pitfalls

Watch out

A modern API named SVI may use ordinary gradient optimization, not the original natural-gradient algorithm. Incorrect N/B scaling silently changes prior-to-data balance. Stochastic objective fluctuations are expected and do not contradict coordinate-ascent monotonicity because this is a different update regime.

Python and R code examples

Scope: One natural-parameter interpolation for a Beta–Bernoulli model; not a complete topic-model training system.

Implementation route: The component is native NumPy/base R. Python Pyro offers stochastic ELBO infrastructure; implementing the particular conjugate natural-gradient update requires the appropriate model-specific sufficient statistics. R can use the displayed custom loop or a separately documented probabilistic engine.

Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — One natural-parameter interpolation for a Beta–Bernoulli model; not a complete topic-model training system. · needs numpy

import numpy as np
batch=np.array([1,1,1,0,1,0,1,1,0,1]); N=1000; prior=np.array([1.,1.])
ab=np.array([1.,1.]); rho=.1
target=prior+N/len(batch)*np.array([batch.sum(),len(batch)-batch.sum()])
ab=(1-rho)*ab+rho*target
print('updated Beta shapes',ab,'mean',ab[0]/ab.sum())
R

Component demo, not a trained model — One natural-parameter interpolation for a Beta–Bernoulli model; not a complete topic-model training system.

batch <- c(1,1,1,0,1,0,1,1,0,1); N <- 1000; prior <- c(1,1)
ab <- c(1,1); rho <- .1
target <- prior+N/length(batch)*c(sum(batch),length(batch)-sum(batch))
ab <- (1-rho)*ab+rho*target
print(c(alpha=ab[1],beta=ab[2],mean=ab[1]/sum(ab)))

Sources

From the CodeSplash reference, entry INF003.