Overview

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

FAQ

What is variational inference in Bayesian learning?Variational inference (VI) is an optimization-based method for approximating a difficult posterior distribution with a simpler distribution that is easier to work with. Instead of sampling until convergence like MCMC, VI searches over a family of candidate distributions and picks the one that best matches the true posterior.
Why is variational inference often faster than MCMC?VI is often faster because it turns inference into an optimization problem and can use simple approximating distributions, gradient descent, and minibatches. This avoids the expensive posterior exploration required by MCMC, especially for large datasets and high-dimensional models.
What role does the KL divergence play in variational inference?The KL divergence measures how different the approximating distribution q(θ) is from the true posterior p(θ | D). In VI, the goal is to minimize this divergence so that q becomes as close as possible to the posterior.
Why do we use the ELBO instead of directly minimizing the KL divergence?Directly computing the KL divergence is hard because it depends on the unknown posterior. The ELBO rewrites the objective using quantities that are easier to compute, such as the likelihood and prior, and maximizing the ELBO is equivalent to minimizing the KL divergence.
What is the approximating distribution q(θ) in variational inference?q(θ) is a simpler distribution chosen to approximate the posterior. A common choice is a normal distribution, whose parameters, such as mean and standard deviation, are optimized to make q resemble the true posterior as closely as possible.
What is the reparameterization trick?The reparameterization trick rewrites a random sample from q as a deterministic transformation of a sample from a fixed distribution, such as θᵢ = μ + σ · εᵢ with εᵢ ~ N(0,1). This makes the sampling step differentiable, allowing gradients to flow through the ELBO optimization.
Why is the reparameterization trick important for gradient descent in VI?Sampling is not differentiable, so autodiff cannot directly compute gradients through it. The reparameterization trick removes this obstacle by expressing samples in a differentiable form, enabling gradient-based optimization of the variational parameters.
What is minibatch variational inference?Minibatch VI estimates the ELBO using a small random subset of the full dataset at each optimization step, rather than using all data points. This makes VI scalable to very large datasets while keeping memory and computation manageable.
What are common drawbacks of variational inference?VI can produce biased approximations because it depends on the chosen family of approximating distributions. With simple families like a normal distribution, it may miss multiple modes and underestimate variance, leading to mode-seeking behavior.
When should I use variational inference instead of MCMC?Use VI when speed and scalability are important, such as with large datasets, real-time inference, or high-dimensional models. Use MCMC when accuracy and faithful uncertainty estimates matter most and you can afford the extra computation time.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bayes Grokking Bayes ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bayes Grokking Bayes ebook for free