Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Introduction to variational inference

In probabilistic machine learning, our goal is often not just to find a single “best” set of parameters for a model, but to quantify our uncertainty over all possible parameters. We accomplish this using Bayesian inference.

Let Θ⊆RP\Theta \subseteq \mathbb{R}^{P} denote the parameter space, and let θ∈Θ\theta \in \Theta be the vector of all parameters in our model (e.g., the weights and biases of a neural network). Let D={(xn,yn)}n=1N\mathcal{D} = \{(x_n, y_n)\}_{n=1}^{N} be our observed training data, consisting of NN input–target pairs. Bayesian inference updates our prior beliefs about the parameters, p(θ)p(\theta), after observing the data, yielding the posterior distribution p(θ∣D)p(\theta \mid \mathcal{D}).

This update is governed by Bayes’ theorem:

p(θ∣D)=p(D∣θ)p(θ)p(D).p(\theta \mid \mathcal{D}) = \frac{p(\mathcal{D} \mid \theta)p(\theta)}{p(\mathcal{D})}.

Here,

The central computational challenge in most modern Bayesian inference problems is evaluating p(D)p(\mathcal{D}), which requires integrating the joint distribution p(D,θ)p(\mathcal{D},\theta) over the parameter space Θ\Theta. For models such as Bayesian neural networks, this integral is high-dimensional and generally lacks an analytic closed-form solution. Consequently, the posterior density p(θ∣D)p(\theta \mid \mathcal{D}) usually cannot be evaluated in normalised form, motivating approximate inference methods. This chapter focuses on variational inference, also known as variational Bayes, a widely used family of scalable approximate inference methods Jordan et al., 1999Wainwright & Jordan, 2008Blei et al., 2017.

A Variational Approach: Bayesian Inference as Optimisation

The core idea of variational inference (VI) is to reframe Bayesian inference as an optimisation problem. Instead of representing the generally intractable posterior p(θ∣D)p(\theta \mid \mathcal{D}) exactly, we choose a tractable distribution q(θ;λ)q(\theta;\varparams) from a variational family Q\mathcal{Q}, where λ\varparams denotes its variational parameters. Concretely, Q\mathcal{Q} could be a family of Gaussian distributions or a family of factorised distributions, as developed in Chapter Gaussian mean-field variational inference. The goal is to find the member q(θ;λ∗)q(\theta;\varparams^\ast) that best approximates the posterior.

The Kullback–Leibler Divergence

Following Kullback & Leibler (1951), the Kullback–Leibler (KL) divergence from q(θ;λ)q(\theta;\varparams) to the posterior p(θ∣D)p(\theta \mid \mathcal{D}) is

DKL(q(θ;λ)∥p(θ∣D))=∫q(θ;λ)log⁡q(θ;λ)p(θ∣D) dθ=Eq(θ;λ)[log⁡q(θ;λ)−log⁡p(θ∣D)].\begin{aligned} D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta \mid \mathcal{D})\right) &= \int q(\theta;\varparams) \log\frac{q(\theta;\varparams)}{p(\theta \mid \mathcal{D})}\,\mathrm{d}\theta \\ &= \mathbb{E}_{q(\theta;\varparams)}\left[\log q(\theta;\varparams)-\log p(\theta \mid \mathcal{D})\right]. \end{aligned}

Because the KL divergence is asymmetric, its direction must be stated explicitly. VI seeks variational parameters satisfying

λ∗∈arg min⁡λDKL(q(θ;λ)∥p(θ∣D)).\varparams^\ast \in \operatorname*{arg\,min}_{\varparams} D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta \mid \mathcal{D})\right).

Rewriting the KL Objective

Expanding the posterior in Equation (2) using Bayes’ theorem gives

DKL(q(θ;λ)∥p(θ∣D))=Eq(θ;λ)[log⁡q(θ;λ)−log⁡p(D,θ)]+log⁡p(D).\begin{aligned} D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta \mid \mathcal{D})\right) &= \mathbb{E}_{q(\theta;\varparams)}\left[\log q(\theta;\varparams)-\log p(\mathcal{D},\theta)\right] + \log p(\mathcal{D}). \end{aligned}

Although the KL divergence itself cannot generally be evaluated exactly because log⁡p(D)\log p(\mathcal{D}) is unavailable, the log-evidence term is constant with respect to λ\varparams and therefore does not affect the optimiser in Equation (3). The remaining expectation does not require evaluating p(D)p(\mathcal{D}) and can be estimated using samples from q(θ;λ)q(\theta;\varparams) whenever the joint density p(D,θ)p(\mathcal{D},\theta) and variational density q(θ;λ)q(\theta;\varparams) can be evaluated.

The Evidence Lower Bound (ELBO)

Rearranging Equation (4) yields

log⁡p(D)=Eq(θ;λ)[log⁡p(D,θ)−log⁡q(θ;λ)]⏟L(λ)+DKL(q(θ;λ)∥p(θ∣D))⏟≥0.\log p(\mathcal{D}) = \underbrace{\mathbb{E}_{q(\theta;\varparams)}\left[\log p(\mathcal{D},\theta)-\log q(\theta;\varparams)\right]}_{\mathcal{L}(\varparams)} + \underbrace{D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta \mid \mathcal{D})\right)}_{\geq 0}.

The first term defines the evidence lower bound (ELBO) L(λ)\mathcal{L}(\varparams). Because the KL divergence is non-negative,

L(λ)≤log⁡p(D).\mathcal{L}(\varparams) \leq \log p(\mathcal{D}).

The gap between the ELBO and the log evidence is exactly DKL(q(θ;λ)∥p(θ∣D))D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta \mid \mathcal{D})\right); the closer the variational distribution is to the posterior in this divergence, the smaller the gap. Since log⁡p(D)\log p(\mathcal{D}) is constant with respect to λ\varparams, Equation (5) also gives

arg max⁡λL(λ)=arg min⁡λDKL(q(θ;λ)∥p(θ∣D)).\operatorname*{arg\,max}_{\varparams}\mathcal{L}(\varparams) = \operatorname*{arg\,min}_{\varparams} D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta \mid \mathcal{D})\right).

Thus, VI replaces evaluation of the posterior in normalised form with optimisation of an objective that can be estimated under suitable choices of likelihood, prior, and variational family.

Interpreting the ELBO

Using p(D,θ)=p(D∣θ)p(θ)p(\mathcal{D},\theta)=p(\mathcal{D} \mid \theta)p(\theta), the ELBO can be written as

L(λ)=Eq(θ;λ)[log⁡p(D∣θ)]−DKL(q(θ;λ)∥p(θ)).\begin{aligned} \mathcal{L}(\varparams) &= \mathbb{E}_{q(\theta;\varparams)}\left[\log p(\mathcal{D} \mid \theta)\right] - D_{KL}\left(q(\theta;\varparams)\middle\|p(\theta)\right). \end{aligned}

The alternative form in Equation (8) reveals a trade-off between two terms:

VI selects the member of Q\mathcal{Q} that balances fit to the observed data against departure from the prior.

Other Lower Bounds on the Log-Evidence

The ELBO is one lower bound on log⁡p(D)\log p(\mathcal{D}). More generally, strictly positive unbiased estimators of p(D)p(\mathcal{D}) can induce lower bounds through Jensen’s inequality; this optional subsection states the construction underlying importance-weighted and sequential Monte Carlo objectives.

Assume that q(θ;λ)>0q(\theta;\varparams)>0 whenever p(D,θ)>0p(\mathcal{D},\theta)>0. Then

Eq(θ;λ)[p(D,θ)q(θ;λ)]=∫p(D,θ) dθ=p(D).\mathbb{E}_{q(\theta;\varparams)}\left[\frac{p(\mathcal{D},\theta)}{q(\theta;\varparams)}\right] = \int p(\mathcal{D},\theta)\,\mathrm{d}\theta = p(\mathcal{D}).

For a concave function φ\varphi and random variable XX, Jensen’s inequality states that φ(E[X])≥E[φ(X)]\varphi(\mathbb{E}\left[X\right])\geq\mathbb{E}\left[\varphi(X)\right]. Taking φ=log⁡\varphi=\log in Equation (9) gives

L(λ)=Eq(θ;λ)[log⁡p(D,θ)q(θ;λ)]≤log⁡Eq(θ;λ)[p(D,θ)q(θ;λ)]=log⁡p(D).\begin{aligned} \mathcal{L}(\varparams) &= \mathbb{E}_{q(\theta;\varparams)}\left[\log\frac{p(\mathcal{D},\theta)}{q(\theta;\varparams)}\right] \\ &\leq \log \mathbb{E}_{q(\theta;\varparams)}\left[\frac{p(\mathcal{D},\theta)}{q(\theta;\varparams)}\right] = \log p(\mathcal{D}). \end{aligned}

More generally, let r(u;λ)r(\mathbf{u};\varparams) denote the sampling density of all random variables u\mathbf{u} used to construct a strictly positive unbiased estimator g(D;u,λ)g(\mathcal{D};\mathbf{u},\varparams) of p(D)p(\mathcal{D}), and suppose that Er(u;λ)[∣log⁡g(D;u,λ)∣]<∞\mathbb{E}_{r(\mathbf{u};\varparams)}\left[\lvert\log g(\mathcal{D};\mathbf{u},\varparams)\rvert\right]<\infty. Then

Er(u;λ)[g(D;u,λ)]=p(D).\mathbb{E}_{r(\mathbf{u};\varparams)}\left[g(\mathcal{D};\mathbf{u},\varparams)\right] = p(\mathcal{D}).

Jensen’s inequality gives the lower bound

Lg(λ):=Er(u;λ)[log⁡g(D;u,λ)]≤log⁡p(D).\mathcal{L}_g(\varparams) := \mathbb{E}_{r(\mathbf{u};\varparams)}\left[\log g(\mathcal{D};\mathbf{u},\varparams)\right] \leq \log p(\mathcal{D}).

The standard ELBO is recovered by taking u=θ\mathbf{u}=\theta, r(θ;λ)=q(θ;λ)r(\theta;\varparams)=q(\theta;\varparams), and g(D;θ,λ)=p(D,θ)/q(θ;λ)g(\mathcal{D};\theta,\varparams)=p(\mathcal{D},\theta)/q(\theta;\varparams). For example, Burda et al. (2016) introduce the importance-weighted autoencoder (IWAE) objective, which uses ss independent samples θj∼q(⋅;λ)\theta_j\sim q(\cdot;\varparams) and the estimator

g(D;u,λ)=1s∑j=1sp(D,θj)q(θj;λ),u={θj}j=1s.g(\mathcal{D};\mathbf{u},\varparams) = \frac{1}{s}\sum_{j=1}^{s} \frac{p(\mathcal{D},\theta_j)}{q(\theta_j;\varparams)}, \qquad \mathbf{u}=\{\theta_j\}_{j=1}^{s}.

Related lower bounds can be constructed using sequential Monte Carlo Maddison et al., 2017. Although Lg(λ)\mathcal{L}_g(\varparams) may not be available in closed form, it can often be optimised using Monte Carlo gradient estimators. However, a tighter bound alone does not guarantee better optimisation or predictive performance, because the signal-to-noise ratio and other properties of the associated gradient estimator also matter Rainforth et al., 2018.

In summary, VI approximates p(θ∣D)p(\theta \mid \mathcal{D}) by optimising a distribution from a tractable family, with the ELBO providing an objective that balances data fit against departure from the prior. Chapter Gaussian mean-field variational inference develops the mean-field construction in detail, and subsequent chapters consider further variational families and optimisation methods.

References
  1. Jordan, M. I., Ghahramani, Z., Jaakkola, T. S., & Saul, L. K. (1999). An Introduction to Variational Methods for Graphical Models. Machine Learning, 37(2), 183–233.
  2. Wainwright, M. J., & Jordan, M. I. (2008). Graphical Models, Exponential Families, and Variational Inference. Foundations and Trends in Machine Learning, 1(1–2), 1–305.
  3. Blei, D. M., Kucukelbir, A., & McAuliffe, J. D. (2017). Variational Inference: A Review for Statisticians. Journal of the American Statistical Association, 112(518), 859–877.
  4. Kullback, S., & Leibler, R. A. (1951). On Information and Sufficiency. The Annals of Mathematical Statistics, 22(1), 79–86.
  5. Burda, Y., Grosse, R., & Salakhutdinov, R. (2016). Importance weighted autoencoders. International Conference on Learning Representations (ICLR).
  6. Maddison, C., Lawson, J., Tucker, G., Heess, N., Norouzi, M., Mnih, A., Doucet, A., & Teh, Y. (2017). Filtering Variational Objectives. In I. Guyon, U. V. Luxburg, S. Bengio, H. Wallach, R. Fergus, S. Vishwanathan, & R. Garnett (Eds.), Advances in Neural Information Processing Systems (Vol. 30). Curran Associates, Inc.
  7. Rainforth, T., Kosiorek, A., Le, T. A., Maddison, C., Igl, M., Wood, F., & Teh, Y. W. (2018). Tighter Variational Bounds are Not Necessarily Better. In J. Dy & A. Krause (Eds.), Proceedings of the 35th International Conference on Machine Learning (Vol. 80, pp. 4277–4285). PMLR.