Simple Algorithm
Machine Learning · Convergence · 30 min

Variational Inference

Turning inference into optimization: the ELBO and the reparameterization trick.

Variational inference replaces an impossible integral with an optimization problem: find the member of a simple family that is closest to the posterior in KL divergence. That trade buys speed and scale, from topic models over millions of documents to deep generative models of images, at the price of a bias this lesson makes precise. You will derive the coordinate-ascent updates, see exactly how mean-field VI underestimates uncertainty, compute the gradients that make VI work at scale, and learn how to tell whether a variational fit can be trusted.

What you’ll learn

  • Derive the ELBO decomposition and its equivalent forms, and connect variational inference to EM and to the convex-duality view of exponential families.
  • Derive the coordinate-ascent (CAVI) update for mean-field VI, and compute the mean-field approximation of a correlated Gaussian exactly.
  • Contrast the reverse and forward KL divergences, and explain expectation propagation as local moment matching.
  • Derive the score-function and reparameterization gradient estimators, compare their variances, and explain stochastic VI, variational autoencoders and importance-weighted bounds.
  • Diagnose a variational fit with Pareto-smoothed importance sampling, and state what is known about the asymptotics of variational posteriors.

The ELBO, three ways

Let x\mathbf x be the data and z\mathbf z everything unobserved (latent variables and parameters). Variational inference chooses a family Q\mathcal Q of distributions over z\mathbf z and solves q⋆=arg min⁡q∈QDKL(q(z) ∥ p(z∣x))q^\star=\argmin_{q\in\mathcal Q}\KL\big(q(\mathbf z)\,\|\,p(\mathbf z\mid\mathbf x)\big). The divergence contains the unknown evidence, but the identity proved in Why EM Works splits it off:

log⁡p(x)=Eq[log⁡p(x,z)]−Eq[log⁡q(z)]⏟ELBO(q)+DKL(q(z) ∥ p(z∣x))\log p(\mathbf x)=\underbrace{\E_q\big[\log p(\mathbf x,\mathbf z)\big]-\E_q\big[\log q(\mathbf z)\big]}_{\mathrm{ELBO}(q)}+\KL\big(q(\mathbf z)\,\|\,p(\mathbf z\mid\mathbf x)\big)
Since log⁡p(x)\log p(\mathbf x) is fixed, maximizing the ELBO over Q\mathcal Q is the same as minimizing the KL divergence, and the ELBO needs only the joint density.
FormExpressionReading
Energy plus entropyEq[log⁡p(x,z)]+H(q)\E_q[\log p(\mathbf x,\mathbf z)]+H(q)put mass where the joint is high, but stay spread out
Fit minus complexityEq[log⁡p(x∣z)]−DKL(q(z) ∥ p(z))\E_q[\log p(\mathbf x\mid\mathbf z)]-\KL\big(q(\mathbf z)\,\|\,p(\mathbf z)\big)explain the data, but do not stray far from the prior
Evidence minus gaplog⁡p(x)−DKL(q ∥ p(z∣x))\log p(\mathbf x)-\KL\big(q\,\|\,p(\mathbf z\mid\mathbf x)\big)a lower bound on the log evidence, tight only at the posterior

If Q\mathcal Q contains every distribution, the optimum is the exact posterior and the bound is tight: that is EM's E-step. Restricting Q\mathcal Q trades exactness for tractability. When the latent variables are local (one per data point) and the parameters global, alternating a variational E-step with a maximization over parameters is variational EM, as in LDA (Topic Models & Latent Dirichlet Allocation).

Wainwright & Jordan (2008) give the general picture through convex duality. For an exponential family with sufficient statistics ϕ\boldsymbol\phi, the log-partition function has the variational representation A(θ)=sup⁡μ∈M{⟨θ,μ⟩−A∗(μ)}A(\boldsymbol\theta)=\sup_{\boldsymbol\mu\in\mathcal M}\{\langle\boldsymbol\theta,\boldsymbol\mu\rangle-A^\ast(\boldsymbol\mu)\}, where M\mathcal M is the set of realizable mean parameters (the marginal polytope, for discrete graphical models) and −A∗(μ)-A^\ast(\boldsymbol\mu) is the entropy of the maximum-entropy distribution with those means. Mean-field methods optimize over the means realizable by product distributions, an inner subset of M\mathcal M, so they produce lower bounds; belief propagation relaxes both the set and the entropy, and its Bethe approximation is in general not a bound at all.

Check your understanding

You fit two variational approximations to the same posterior; q1q_1 reaches ELBO −1050.3-1050.3 and q2q_2 reaches −1047.8-1047.8. What can you conclude?

  1. q2q_2 is closer to the posterior in DKL(q ∥ p(z∣x))\KL(q\,\|\,p(\mathbf z\mid\mathbf x)), by exactly 2.5 nats
  2. q2q_2 has better-calibrated credible intervals
  3. log⁡p(x)=−1047.8\log p(\mathbf x)=-1047.8
  4. Nothing: ELBOs of different approximations cannot be compared
Show the answer

A. q2q_2 is closer to the posterior in DKL(q ∥ p(z∣x))\KL(q\,\|\,p(\mathbf z\mid\mathbf x)), by exactly 2.5 nats

Both are bounds on the same log⁡p(x)\log p(\mathbf x), so the ELBO difference equals the difference of their KL gaps. That says q2q_2 is closer in reverse KL; it does not say how close either is, nor that its intervals are calibrated.

Mean field and coordinate ascent

The mean-field family factorizes completely, q(z)=∏jqj(zj)q(\mathbf z)=\prod_jq_j(z_j) (or over blocks of variables). Optimizing one factor at a time has a closed-form answer.

DerivationThe CAVI update
  1. ELBO=Eqj[E−j[log⁡p(x,z)]]−Eqj[log⁡qj(zj)]+const\mathrm{ELBO}=\E_{q_j}\Big[\E_{-j}\big[\log p(\mathbf x,\mathbf z)\big]\Big]-\E_{q_j}\big[\log q_j(z_j)\big]+\text{const}

    Hold every factor except qjq_j fixed and write E−j\E_{-j} for the expectation over the others. Collect the terms of the ELBO that involve qjq_j:

  2. ELBO=−DKL(qj ∥ q~j)+const\mathrm{ELBO}=-\KL\big(q_j\,\|\,\tilde q_j\big)+\text{const}

    Define the normalized density q~j(zj)∝exp⁡(E−j[log⁡p(x,z)])\tilde q_j(z_j)\propto\exp\big(\E_{-j}[\log p(\mathbf x,\mathbf z)]\big). The two terms combine into a negative KL divergence plus a constant:

  3. qj⋆(zj) ∝ exp⁡(E−j[log⁡p(x,z)])q_j^\star(z_j)\ \propto\ \exp\Big(\E_{-j}\big[\log p(\mathbf x,\mathbf z)\big]\Big)

    The KL is minimized, at zero, by qj=q~jq_j=\tilde q_j. Cycling through jj is coordinate ascent variational inference (CAVI): each update cannot decrease the ELBO, which is generally non-convex, so CAVI converges to a local optimum.

In conditionally conjugate models, where each complete conditional p(zj∣z−j,x)p(z_j\mid\mathbf z_{-j},\mathbf x) is in an exponential family, qj⋆q_j^\star stays in that family and its natural parameter is the expected natural parameter of the complete conditional. That is why the LDA updates involve digamma functions: they are expectations of log⁡θ\log\theta under a Dirichlet. A fully worked case shows both the strength and the weakness of mean field.

DerivationMean field on a correlated Gaussian
  1. log⁡q1⋆(z1)=−12Λ11z12+z1(Λ11μ1−Λ12(m2−μ2))+const,m2=Eq[z2]\log q_1^\star(z_1)=-\tfrac12\Lambda_{11}z_1^2+z_1\big(\Lambda_{11}\mu_1-\Lambda_{12}(m_2-\mu_2)\big)+\text{const},\qquad m_2=\E_q[z_2]

    Let the target be N(μ,Λ−1)\mathcal N(\boldsymbol\mu,\Lambda^{-1}) in two dimensions, with precision matrix Λ\Lambda. As a function of z1z_1, E2[log⁡p(z)]\E_{2}[\log p(\mathbf z)] is quadratic, so q1⋆q_1^\star is Gaussian:

  2. Var⁡q⋆(zj)=1Λjj ≤ (Λ−1)jj=Var⁡p(zj)\Var_{q^\star}(z_j)=\frac1{\Lambda_{jj}}\ \le\ (\Lambda^{-1})_{jj}=\Var_p(z_j)

    So q1⋆=N(m1,Λ11−1)q_1^\star=\mathcal N(m_1,\Lambda_{11}^{-1}) with m1=μ1−Λ11−1Λ12(m2−μ2)m_1=\mu_1-\Lambda_{11}^{-1}\Lambda_{12}(m_2-\mu_2), and symmetrically for q2q_2. The fixed point has the exact means, mj=μjm_j=\mu_j, but variances 1/Λjj1/\Lambda_{jj}:

  3. Var⁡q⋆(zj)=1−ρ2\Var_{q^\star}(z_j)=1-\rho^2

    For unit variances and correlation ρ\rho, Λ=11−ρ2(1−ρ−ρ1)\Lambda=\frac1{1-\rho^2}\begin{pmatrix}1&-\rho\\-\rho&1\end{pmatrix}, so the mean-field variance is 1−ρ21-\rho^2. CAVI's iterates satisfy m1−μ1=ρ (m2−μ2)m_1-\mu_1=\rho\,(m_2-\mu_2): each sweep shrinks the error by ρ2\rho^2, the same rate as the Gibbs sampler on this target.

Predict before you look

The posterior is a bivariate Gaussian with unit marginal standard deviations and correlation 0.9. What standard deviation does the mean-field approximation assign to each coordinate?

Show the answer

0.44

1−0.81=0.19≈0.44\sqrt{1-0.81}=\sqrt{0.19}\approx0.44. The means are exact, but every marginal credible interval is less than half as wide as it should be. A factorized qq cannot tilt, so it shrinks to fit inside the narrow diagonal ridge: the conditional spread, not the marginal one.

Which KL? Mode seeking versus mass covering

The direction of the divergence decides the character of the approximation.

  • Reverse KL, DKL(q ∥ p)=Eq[log⁡q−log⁡p]\KL(q\,\|\,p)=\E_q[\log q-\log p], the one VI minimizes, is infinite if qq puts mass where pp has none (zero-forcing). The optimal qq hides inside the posterior: it locks onto one mode of a multimodal posterior and is too narrow within a mode, as the Gaussian example showed.
  • Forward KL, DKL(p ∥ q)\KL(p\,\|\,q), is infinite if qq misses mass that pp has (zero-avoiding). The optimal qq spreads to cover all of pp, bridging modes and overestimating spread in the directions it cannot tilt.
DerivationForward KL over an exponential family is moment matching
  1. DKL(p ∥ qη)=−η⊤Ep[ϕ(z)]+A(η)+const\KL(p\,\|\,q_{\boldsymbol\eta})=-\boldsymbol\eta^\top\E_p[\boldsymbol\phi(\mathbf z)]+A(\boldsymbol\eta)+\text{const}

    Let qη(z)=h(z)exp⁡(η⊤ϕ(z)−A(η))q_{\boldsymbol\eta}(\mathbf z)=h(\mathbf z)\exp\big(\boldsymbol\eta^\top\boldsymbol\phi(\mathbf z)-A(\boldsymbol\eta)\big). Only the cross-entropy term of DKL(p ∥ qη)\KL(p\,\|\,q_{\boldsymbol\eta}) depends on η\boldsymbol\eta:

  2. Eqη[ϕ(z)]=Ep[ϕ(z)]\E_{q_{\boldsymbol\eta}}\big[\boldsymbol\phi(\mathbf z)\big]=\E_p\big[\boldsymbol\phi(\mathbf z)\big]

    This is convex in η\boldsymbol\eta, and ∇A(η)=Eqη[ϕ]\nabla A(\boldsymbol\eta)=\E_{q_{\boldsymbol\eta}}[\boldsymbol\phi]. Setting the gradient to zero matches the expected sufficient statistics: for a Gaussian qq, the mean and covariance of pp; for a factorized family, pp's marginals.

Matching moments needs expectations under pp, which is the hard part, so forward KL cannot be minimized globally. Expectation propagation (Minka, 2001) applies it locally. Write the posterior as a product of factors, p∝∏ifip\propto\prod_if_i, and the approximation as q∝∏if~iq\propto\prod_i\tilde f_i with each f~i\tilde f_i in an exponential family. Repeatedly remove one approximate factor, multiply in the exact fif_i, project the result back onto the family by moment matching, and update f~i\tilde f_i accordingly. EP unifies assumed-density filtering and loopy belief propagation; it is often very accurate (Gaussian-process classification is a classic use) but has no convergence guarantee and gives no bound on log⁡p(x)\log p(\mathbf x).

PropertyReverse KL (VI)Forward KL (EP, moment matching)
Penalizesmass where pp is smallmissing mass where pp is large
On a multimodal pppicks one modecovers all modes, with mass in between
Correlated Gaussian, factorized qqexact means; variances 1/Λjj1/\Lambda_{jj} (too small)exact means; the true marginal variances
Needsexpectations under qq onlyexpectations under pp (locally, in EP)
Bound on log⁡p(x)\log p(\mathbf x)yes, the ELBOno

Gradients of an expectation: scaling up

Outside conditionally conjugate models, CAVI's expectations have no closed form. Modern VI instead takes stochastic gradients of the ELBO with respect to the variational parameters λ\boldsymbol\lambda. The ELBO is an expectation under qλq_{\boldsymbol\lambda} of fλ(z)=log⁡p(x,z)−log⁡qλ(z)f_{\boldsymbol\lambda}(\mathbf z)=\log p(\mathbf x,\mathbf z)-\log q_{\boldsymbol\lambda}(\mathbf z), and there are two ways to differentiate through the sampling.

DerivationTwo unbiased gradient estimators
  1. ∇λ ELBO=Eqλ[fλ(z) ∇λlog⁡qλ(z)]\nabla_{\boldsymbol\lambda}\,\mathrm{ELBO}=\E_{q_{\boldsymbol\lambda}}\Big[f_{\boldsymbol\lambda}(\mathbf z)\,\nabla_{\boldsymbol\lambda}\log q_{\boldsymbol\lambda}(\mathbf z)\Big]

    Score function (the REINFORCE or likelihood-ratio estimator; Williams, 1992; used for black-box VI by Ranganath, Gerrish & Blei, 2014). Differentiate the density, using ∇λq=q∇λlog⁡q\nabla_{\boldsymbol\lambda}q=q\nabla_{\boldsymbol\lambda}\log q; the term from ff's own dependence on λ\boldsymbol\lambda vanishes because Eq[∇λlog⁡qλ]=0\E_q[\nabla_{\boldsymbol\lambda}\log q_{\boldsymbol\lambda}]=0. It needs only samples from qq and ∇log⁡q\nabla\log q, so it works for discrete latent variables too:

  2. ∇λ Eqλ[f(z)]=Eϵ[∇λf(gλ(ϵ))]\nabla_{\boldsymbol\lambda}\,\E_{q_{\boldsymbol\lambda}}[f(\mathbf z)]=\E_{\boldsymbol\epsilon}\Big[\nabla_{\boldsymbol\lambda}f\big(g_{\boldsymbol\lambda}(\boldsymbol\epsilon)\big)\Big]

    Reparameterization (Kingma & Welling, 2013; Rezende, Mohamed & Wierstra, 2014). Write z=gλ(ϵ)\mathbf z=g_{\boldsymbol\lambda}(\boldsymbol\epsilon) with parameter-free noise, such as z=μ+σ⊙ϵ\mathbf z=\boldsymbol\mu+\boldsymbol\sigma\odot\boldsymbol\epsilon, ϵ∼N(0,I)\boldsymbol\epsilon\sim\mathcal N(\mathbf 0,I). The expectation is now over a fixed distribution, so the gradient moves inside. It needs continuous z\mathbf z and a differentiable model, and uses the model's gradient:

  3. Var⁡[z2(z−μ)]=E[(μ+ϵ)4ϵ2]−4μ2=μ4+14μ2+15\Var\big[z^2(z-\mu)\big]=\E\big[(\mu+\epsilon)^4\epsilon^2\big]-4\mu^2=\mu^4+14\mu^2+15

    A variance comparison. Take f(z)=z2f(z)=z^2 and q=N(μ,1)q=\mathcal N(\mu,1), so the true gradient in μ\mu is 2μ2\mu. The reparameterization estimator is 2(μ+ϵ)2(\mu+\epsilon), with variance 4 for every μ\mu. The score-function estimator is z2(z−μ)z^2(z-\mu); expanding (μ+ϵ)4ϵ2(\mu+\epsilon)^4\epsilon^2 and using Eϵ2=1\E\epsilon^2=1, Eϵ4=3\E\epsilon^4=3, Eϵ6=15\E\epsilon^6=15:

Predict before you look

At μ=2\mu=2, how many times larger is the score-function estimator's variance than the reparameterization estimator's?

Show the answer

21.75

μ4+14μ2+15=16+56+15=87\mu^4+14\mu^2+15=16+56+15=87, against 4: a factor of about 22, and it grows like μ4\mu^4. Score-function gradients need variance reduction (control variates, Rao–Blackwellization) to be practical; reparameterization gradients often work with a single sample.

Stochastic variational inference (Hoffman, Blei, Wang & Paisley, 2013) adds data subsampling. In conditionally conjugate models, the natural gradient of the ELBO with respect to the global variational parameters is simple, so each step computes the local optimum for a minibatch, forms the global parameter λ^\hat{\boldsymbol\lambda} that the minibatch would imply if replicated to the full data size, and moves to (1−ρt)λ+ρtλ^(1-\rho_t)\boldsymbol\lambda+\rho_t\hat{\boldsymbol\lambda} with Robbins–Monro step sizes. Their experiments fit topic models to millions of documents.

Variational autoencoders amortize inference: instead of separate variational parameters for every data point, an encoder network outputs qϕ(z∣x)=N(μϕ(x),diag⁡σϕ2(x))q_\phi(\mathbf z\mid\mathbf x)=\mathcal N\big(\boldsymbol\mu_\phi(\mathbf x),\diag\boldsymbol\sigma^2_\phi(\mathbf x)\big), and a decoder defines pθ(x∣z)p_\theta(\mathbf x\mid\mathbf z) with prior z∼N(0,I)\mathbf z\sim\mathcal N(\mathbf 0,I); both are trained on the ELBO with reparameterization gradients.

python
import torch

def negative_elbo(x, encoder, decoder):
    """One-sample estimate of -ELBO for a VAE; encoder and decoder are user-defined modules."""
    mu, logvar = encoder(x)                          # q(z | x) = N(mu, diag(exp(logvar)))
    z = mu + torch.exp(0.5 * logvar) * torch.randn_like(mu)   # reparameterization
    log_px = decoder.log_likelihood(x, z)            # log p(x | z), one value per example
    kl = 0.5 * (mu.pow(2) + logvar.exp() - 1.0 - logvar).sum(dim=-1)   # KL(q(z|x) || N(0, I))
    return (kl - log_px).mean()                      # minimize with any SGD optimizer

The KL term between two Gaussians is closed-form, 12∑j(μj2+σj2−1−log⁡σj2)\tfrac12\sum_j(\mu_j^2+\sigma_j^2-1-\log\sigma_j^2); only the reconstruction term is estimated by sampling. With a very expressive decoder, the KL term can collapse to zero and the latent code is ignored (observed for text by Bowman et al., 2016).

Did it work? Diagnostics and theory

The ELBO cannot certify a fit, because the gap log⁡p(x)−ELBO\log p(\mathbf x)-\mathrm{ELBO} is unknown. Yao, Vehtari, Simpson & Gelman (2018) proposed using qq as an importance-sampling proposal. Draw zs∼q\mathbf z_s\sim q, compute the ratios rs=p(zs,x)/q(zs)r_s=p(\mathbf z_s,\mathbf x)/q(\mathbf z_s), fit a generalized Pareto distribution to the largest ones, and read its shape k^\hat k, which measures how heavy the ratios' tail is. Ratios with k>0.5k>0.5 have infinite variance (the χ2\chi^2 divergence from qq to pp is infinite), and k>1k>1 means even DKL(p ∥ q)\KL(p\,\|\,q) is infinite. Their recommendation:

  • k^<0.5\hat k<0.5: qq is close to the posterior, and Pareto-smoothed importance sampling can refine its estimates further.
  • 0.5<k^<0.70.5<\hat k<0.7: qq is not perfect but still useful, again with importance-sampling correction.
  • k^>0.7\hat k>0.7: the approximation is unreliable; change the family, the divergence or the method.

They also proposed variational simulation-based calibration, which checks point estimates by refitting on data simulated from the prior.

Combine the theorem with the Gaussian calculation above: the exact posterior is approximately N(θ0,(nI)−1)\mathcal N(\boldsymbol\theta_0,(nI)^{-1}) for Fisher information II, and its mean-field projection has the right center but variances 1/(nIjj)1/(nI_{jj}) instead of (I−1)jj/n(I^{-1})_{jj}/n. So mean-field VI gets point estimates right asymptotically and its uncertainty wrong whenever parameters are correlated a posteriori. Giordano, Broderick & Jordan (2018) recover better covariances from how the variational means respond to small perturbations of the model, a method that includes the Laplace approximation as a special case.

On the optimization side, the ELBO is non-convex in general, and convergence guarantees for black-box VI arrived only recently: Domke and coauthors (2023) proved convergence of proximal and projected stochastic gradient methods for dense Gaussian families with reparameterization gradients, for targets whose log-density is smooth and concave.

Key takeaways

  • log⁡p(x)=ELBO(q)+DKL(q ∥ p(z∣x))\log p(\mathbf x)=\mathrm{ELBO}(q)+\KL(q\,\|\,p(\mathbf z\mid\mathbf x)): maximizing the ELBO minimizes the reverse KL using only the joint density. With an unrestricted family it is EM's E-step; convex duality shows mean field as an inner approximation, hence a lower bound.
  • CAVI sets qj∝exp⁡(E−j[log⁡p(x,z)])q_j\propto\exp(\E_{-j}[\log p(\mathbf x,\mathbf z)]). On a correlated Gaussian it recovers the means exactly but reports variances 1/Λjj1/\Lambda_{jj}, 1−ρ21-\rho^2 for unit variances and correlation ρ\rho.
  • Reverse KL is mode-seeking and overconfident; forward KL over an exponential family is moment matching, which expectation propagation applies locally, without a bound or a convergence guarantee.
  • Score-function gradients are general but noisy; reparameterization gradients need continuous latents and are far lower in variance. Stochastic VI adds minibatches; VAEs amortize inference with an encoder; importance-weighted bounds tighten the ELBO toward log⁡p(x)\log p(\mathbf x).
  • Check VI with the PSIS k^\hat k (below 0.5 good, above 0.7 unreliable). Variational posteriors are asymptotically normal with consistent means, but mean field understates uncertainty under posterior correlation.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In mean-field coordinate ascent, what is the optimal factor qjq_j with all other factors held fixed?

  1. The exact posterior marginal p(zj∣x)p(z_j\mid\mathbf x)
  2. qj⋆(zj)∝exp⁡(E−j[log⁡p(x,z)])q_j^\star(z_j)\propto\exp\big(\E_{-j}[\log p(\mathbf x,\mathbf z)]\big)
  3. qj⋆(zj)∝E−j[p(x,z)]q_j^\star(z_j)\propto\E_{-j}[p(\mathbf x,\mathbf z)]
  4. The prior p(zj)p(z_j)
Show the answer

B. qj⋆(zj)∝exp⁡(E−j[log⁡p(x,z)])q_j^\star(z_j)\propto\exp\big(\E_{-j}[\log p(\mathbf x,\mathbf z)]\big)

Collecting the ELBO's terms in qjq_j gives −DKL(qj ∥ q~j)-\KL(q_j\,\|\,\tilde q_j) with q~j∝exp⁡(E−j[log⁡p])\tilde q_j\propto\exp(\E_{-j}[\log p]). The expectation is of the log joint, which is what makes conditionally conjugate models tractable.

Question 2

A Bayesian logistic regression has two strongly correlated coefficients. Compared with MCMC, what will a mean-field variational fit typically report for them?

  1. Wider intervals, since VI is conservative
  2. Identical intervals once the ELBO has converged
  3. Shifted means but correct widths
  4. Roughly correct means but intervals that are too narrow
Show the answer

D. Roughly correct means but intervals that are too narrow

Reverse KL with a factorized family captures the conditional spread, 1/Λjj1/\Lambda_{jj}, rather than the marginal spread, (Λ−1)jj(\Lambda^{-1})_{jj}. For the Gaussian case the means are exact; asymptotically the variational mean is consistent (Wang & Blei), but the uncertainty is understated whenever posterior correlations are strong.

Question 3

A model has discrete latent variables. Which gradient estimator can be used directly for black-box VI, and what is its main drawback?

  1. The score-function estimator; its variance is often high and needs control variates
  2. The reparameterization estimator; it is biased for discrete variables
  3. Either one, with identical variance
  4. Neither: discrete latent variables rule out variational inference
Show the answer

A. The score-function estimator; its variance is often high and needs control variates

The score-function estimator needs only samples and ∇log⁡q\nabla\log q, so it handles discrete z\mathbf z. Reparameterization needs z\mathbf z to be a differentiable function of parameter-free noise, which discrete variables are not (relaxations exist, at the cost of bias).

Question 4

After fitting VI, the Pareto-smoothed importance sampling diagnostic gives k^=0.9\hat k=0.9. What should you do?

  1. Trust the fit: k^\hat k below 1 means the KL divergence is finite
  2. Report the importance-weighted estimates, which fix any approximation
  3. Treat the approximation as unreliable, and change the variational family, the divergence or switch to MCMC
  4. Run the optimizer longer, which always lowers k^\hat k
Show the answer

C. Treat the approximation as unreliable, and change the variational family, the divergence or switch to MCMC

Above 0.7 the importance ratios are so heavy-tailed that even Pareto-smoothed importance sampling converges impractically slowly. The approximation misses posterior mass, so a richer family or a different method is needed.

Further reading

  • M. I. Jordan, Z. Ghahramani, T. S. Jaakkola & L. K. Saul, "An introduction to variational methods for graphical models," Machine Learning 37(2), 183–233 (1999).
  • M. J. Wainwright & M. I. Jordan, "Graphical models, exponential families, and variational inference," Foundations and Trends in Machine Learning 1(1–2), 1–305 (2008).
  • D. M. Blei, A. Kucukelbir & J. D. McAuliffe, "Variational inference: A review for statisticians," Journal of the American Statistical Association 112(518), 859–877 (2017).
  • D. P. Kingma & M. Welling, "Auto-encoding variational Bayes," arXiv:1312.6114 (2013).
  • Y. Yao, A. Vehtari, D. Simpson & A. Gelman, "Yes, but did it work?: Evaluating variational inference," Proceedings of the 35th International Conference on Machine Learning, PMLR 80, 5581–5590 (2018).