6 Variational inference: Scaling the large datasets
Variational inference provides a faster alternative to sampling-based Bayesian inference by turning posterior estimation into an optimization problem. Instead of using a long MCMC chain to explore an intractable posterior, it chooses a simple family of distributions and searches for the member that best approximates the true posterior. This makes it especially useful for large datasets and high-dimensional models, where MCMC can become slow because it must repeatedly process much of the full data and thoroughly traverse a complex posterior landscape.
The core of the method is to measure how far the approximating distribution is from the target using KL divergence, then optimize that gap indirectly through the evidence lower bound. Because the exact posterior is unknown, the ELBO reformulation lets the algorithm work with quantities that are computable: the likelihood, the prior, and the chosen approximation. Gradient descent is then used to tune the parameters of the approximating distribution, and the reparameterization trick makes this possible even when sampling is involved by rewriting random draws as differentiable transformations of fixed-noise samples.
To scale further, variational inference can be run with minibatches so each update uses only a small random subset of the data, greatly reducing computation while preserving an unbiased learning signal over time. The chapter also shows how this idea is used in PyMC, how convergence is checked by monitoring the ELBO, and how VI compares with MCMC in practice. VI is typically much faster and more scalable, but it can be biased, may miss multiple posterior modes, and often underestimates uncertainty, so it is best used when speed and scale matter more than exact fidelity.
Unlike MCMC, which generates samples to approximate an unknown posterior, variational inference (VI) fits a simple distribution like a normal distribution to the posterior and uses the fitted distribution as the approximation.
The KL divergence is large when the approximating distribution 𝑞 yields high density where 𝑝 is small. When the two distributions match, the KL divergence decreases to be close to 0.
The evolution of the approximating distribution 𝑞 and how it’s adjusted by gradient descent throughout optimization.
Approximate posteriors produced by MCMC (left) and variational inference (right) in the Bayesian linear regression problem. The two approximations are similar, having the same mean and 94% highest-density interval.
Optimization progress of the negative ELBO across 200,000 steps. The function’s value flattens out towards the end, indicating convergence to an optimum.
Demonstrations of pathological behaviors in variational inference. Left: mode-seeking causes the approximator to miss out on other modes. Right: variance underestimation results from the KL divergence formulation.
Summary
- MCMC is a powerful inference method for Bayesian models without conjugate priors but it can be slow to converge, especially for high-dimensional models or large datasets.
- Variational inference (VI) reframes Bayesian inference as optimization, fitting a simple distribution to approximate the posterior. This yields a fast but inexact inference method.
- The KL divergence measures the gap between the approximating distribution and the true posterior; maximizing the ELBO minimizes this gap.
- Gradient descent with automatic differentiation is a common optimization method in machine learning and is the main tool for optimizing the ELBO for VI. However, sampling blocks gradients, preventing automatic differentiation from keeping track of the gradients. This blocking problem is solved by the reparameterization trick.
- Minibatch VI allows scaling to very large datasets, similar to how deep learning trains.
- Pathological behaviors: VI tends to be mode-seeking and can underestimate variance due to KL’s asymmetry.
- Advanced VI methods (mean-field, structured VI, mixtures, amortized inference, normalizing flows) extend flexibility while balancing speed.
- Mean-field VI scales well to high dimensions by assuming that unknown variables to be modeled are not correlated to one another.
- Structured VI makes specific assumptions about the structure of the distribution of the unknown variables to account for correlations but still enable fast optimization.
- Mixture VI models can flexibly fit to a multimodal distribution but take longer to optimize.
- Amortized VI learns a universal mapping (typically a neural network) going from each datapoint to its own posterior distribution.
- Normalizing flows transform simple distributions (like normal distributions) to more complex ones to be used in VI.
Grokking Bayes ebook for free