Probabilistic Methods/Approximate Inference

Lesson 9.32,633 words

Approximate Inference

In a latent-variable model the quantity we need, the posterior p(hv)p(h\mid v) over hidden causes, is almost never computable, because its normalizer is an intractable sum over configurations. Approximate inference reframes the problem as optimization: maximize the evidence lower bound, a tractable functional whose gap to the true log-evidence equals a KL divergence.

╌╌╌╌

A structured probabilistic model with latent variables explains each observation as generated from a hidden cause : a joint factorizes through the latents, and training maximizes the marginal likelihood . Both learning and prediction route through one object, the posterior over the hidden cause,

The numerator is a single, cheap product of model factors. The denominator is the problem: a sum over every configuration of . For that is terms, and for the graphs that make a model interesting the sum admits no factorization.

Instead of computing , we search for a tractable distribution that stands in for it: inference as optimization.1

Inference as optimization: the evidence lower bound

Introduce an arbitrary distribution over the latents. We do not yet say what is; we only multiply and divide the log-evidence by it and apply Jensen's inequality. Start from and insert :

The logarithm is concave, so by Jensen's inequality the log of the expectation dominates the expectation of the log:

This functional is the evidence lower bound (ELBO). It is tractable whenever is: it requires only expectations under our chosen , never the intractable sum. The gap between and equals a KL divergence, which the next identity makes precise.

Expand the joint as inside and the bound splits:

The remaining expectation is the negative KL divergence from to the true posterior, giving the central decomposition of approximate inference:

Because always, (the bound is confirmed), and the slack is precisely how far sits from the posterior. Two consequences drive everything below.

The figure below shows this: the log-evidence is a fixed ceiling, and the ELBO rises toward it as the KL gap closes.

The log-evidence is fixed and splits as ELBO plus a KL gap; as approaches the posterior, the gap shrinks and the bound rises.

The one bound supports two distinct optimizations:

  • Learning: hold fixed and maximize over , raising a tractable surrogate for the likelihood.
  • Inference: hold fixed and maximize over , tightening the bound toward the likelihood.

Alternating them is the entire expectation–maximization algorithm.2

Expectation–maximization

Suppose the posterior is tractable for fixed (it is for mixtures, factor analyzers, HMMs). Then the inference step is exact: the that maximizes is the posterior itself, driving the KL gap to zero. Expectation–maximization (EM) alternates this exact inference with a likelihood climb in .

The M-step maximizes the expected complete-data log-likelihood, often written . With the latents' responsibilities frozen, this is usually a closed-form weighted fit: for a Gaussian mixture it reduces to the weighted means and covariances. The essential guarantee is that the true marginal likelihood never decreases.

Geometrically, the E-step lifts a tractable lower-bound curve until it touches the likelihood at ; the M-step climbs that surrogate to its peak; the next E-step builds a fresh tangent bound there. The likelihood rises along a staircase of tangent lower bounds.

EM as alternating optimization: the E-step builds a lower bound (black) tangent to (blue), the M-step jumps to its maximum, and likelihood never decreases.
Algorithm:ExpectationMaximization(v,p,θ0)\textsc{ExpectationMaximization}(v, p, \theta_0) — alternate exact inference and a likelihood climb
  1. 1
    initialize θθ0\theta \gets \theta_0
  2. 2
    repeat
  3. 3
    q(h)p(hv; θ)q(h) \gets p(h \mid v;\ \theta)
    E-step: exact posterior, bound becomes tight
  4. 4
    θargmaxθ Ehq[logp(v,hθ)]\theta \gets \arg\max_{\theta'}\ \mathbb{E}_{h\sim q}[\log p(v, h \mid \theta')]
    M-step: maximize expected complete-data log-likelihood
  5. 5
    until logp(v; θ)\log p(v;\ \theta) stops increasing
  6. 6
    return θ\theta

A worked E-step: responsibilities in a two-component mixture

For example, take a one-dimensional mixture of two Gaussians with equal mixing weights , means , , and unit variances, and ask for the posterior over the latent component that generated an observed point . The E-step is Bayes' rule on two numbers. The unnormalized responsibilities are the prior times the likelihood,

and normalizing gives the posterior that the E-step returns,

The point at is assigned almost entirely to component , consistent with its proximity to . The M-step then refits each Gaussian's mean as the responsibility-weighted average of the data, — a soft -means, where the responsibilities are the soft assignments. This is the entire EM loop: the E-step turns each point into a distribution over components, the M-step refits the components to those soft counts, and the marginal likelihood climbs at every round.

EM is the special case where the E-step is exact.3 When the posterior is itself intractable, the E-step can only be approximated, and that is variational inference. The simplest approximation is a point mass.

MAP inference as a point-mass

The crudest approximation collapses onto a single point: a maximum a posteriori (MAP) estimate keeps only the most probable latent configuration and discards the rest of the posterior.

MAP is the limit of a variational whose spread is forced to zero: a Gaussian as the precision . The bound it maximizes keeps the joint at the mode but throws away all posterior uncertainty — fast and often good enough for a point prediction, but it ignores multimodality and variance.4 The variational families below retain more of the posterior.

Variational inference and the mean field

When is intractable, restrict to a family simple enough to optimize, and find the closest member. This is variational inference: minimize over , equivalently maximize over the family.

Geometrically, the true posterior sits outside the family, and inference projects it onto in the (reverse) KL geometry, landing on the member that best matches it where places its mass.

The intractable posterior (red) lies outside the tractable family ; variational inference returns its closest member in KL.

The standard choice for is the mean-field family: assume the latents are independent under ,

The true posterior generally couples the latents: knowing shifts the belief about . Mean field severs every such edge and keeps only the marginals. The diagram contrasts the two graphs.

Mean-field severs the posterior's couplings: the true (left) links the latents, while the factorized (right) keeps only independent marginals.

No couplings, so each factor optimizes almost independently. To derive the update for one factor , isolate its contribution to . Writing and holding all other factors fixed,

where denotes all factors except the -th and the constant absorbs the other entropies. Define the partial expectation . Then the -dependent part is

a single KL in . It is maximized when , giving the mean-field coordinate update:

Each factor is the exponentiated expected log-joint, averaged over the current estimates of every other factor: a fixed-point iteration that cycles through the coordinates and is guaranteed to raise at every step.5

Algorithm:MeanField(v,p)\textsc{MeanField}(v, p) — coordinate ascent on the factorized ELBO
  1. 1
    initialize each factor qiq_i (e.g. uniform)
  2. 2
    repeat
  3. 3
    for each latent j1j \gets 1 to mm do
  4. 4
    logqj(hj)Eqj[logp(v,h)]+c\log q_j(h_j) \gets \mathbb{E}_{q_{-j}}[\log p(v, h)] + c
    hold other factors fixed
  5. 5
    normalize qjq_j so hjqj(hj)=1\sum_{h_j} q_j(h_j) = 1
  6. 6
    until L(v,θ,q)\mathcal{L}(v, \theta, q) converges
  7. 7
    return q=iqiq = \prod_i q_i

The bound is genuinely loose here, and the direction of the KL we minimize determines the bias of the answer.

The two KL directions are not interchangeable; the choice is forced by tractability and it determines the qualitative failure mode of the approximation.

ObjectiveDirectionExpectations underBehaviourTractable?
reverse (chosen, simple)mode-seeking, under-dispersedyes — used in VI
forward (intractable posterior)mean-seeking, mass-coveringno — needs

Variational inference is committed to the reverse KL precisely because it requires only expectations under the we control, the same reason it inherits the mode-seeking bias.

For a correlated true posterior, the mean-field factorization cannot represent the correlation at all: its independence assumption forces an axis-aligned , which fits inside the tilted true density and underestimates the variance along the correlated directions.

A correlated posterior (red, tilted) approximated by a factorized (blue, axis-aligned): mean-field cannot tilt, so it underestimates variance.

Amortized inference: learn the encoder

Mean-field solves a fresh optimization for every datapoint , wasteful when millions of share structure. Amortized inference replaces per-example optimization with a single learned function: an inference network (encoder) that maps any directly to the parameters of its approximate posterior.

The ELBO becomes the training objective for (and jointly ): maximize by gradient ascent. This recovers the encoder of a variational autoencoder: the inference network is the encoder, the generative model is the decoder, and the reparameterization trick lets the gradient of flow back through the sampled into .6

The reparameterization trick

Gradient ascent on the amortized ELBO needs , and contains an expectation over whose distribution depends on . Differentiating through a sample is the obstacle: the naive score-function estimator is unbiased but high-variance, because it never uses the derivative of and instead weights raw samples by a log-density gradient.

The trick removes from the source of randomness. For a Gaussian , sample a parameter-free noise variable and push it through a deterministic map,

so is now a differentiable function of with the randomness isolated in . The expectation moves onto the fixed base distribution and the gradient passes straight through the sample,

The interchange of and is valid because the measure no longer carries ; a single Monte Carlo sample of gives a low-variance, unbiased gradient. Backpropagation flows through the two deterministic paths and and stops at the noise input, which has no gradient.

The reparameterization trick. The encoder emits and ; a sample is deterministic given the external noise , so the gradient (dashed) flows back through and but not through .

Without this reroute the VAE could not train by ordinary backpropagation; with it, the encoder, the sampling step, and the decoder form one differentiable graph.6

Amortized inference: one shared encoder maps each input to its posterior parameters, replacing per-datapoint optimization with a single forward pass.

Variational inference versus MCMC

Variational methods are not the only route to an intractable posterior. Markov chain Monte Carlo (MCMC) makes the opposite trade-off: instead of optimizing a simple , it builds a Markov chain whose stationary distribution is exactly , then draws correlated samples from it. The two families sit at opposite ends of a bias–variance and speed trade-off.

For training deep latent-variable models on millions of examples, the variational side wins: a biased gradient available in one forward and backward pass beats an unbiased estimate that needs a long chain per datapoint. MCMC remains the reference when an unbiased answer is worth the cost.5

The methods of approximate inference

Every method above is one choice of optimized against the same ELBO; they differ only in what is allowed to be, how it is computed, and therefore how exact and how costly the resulting inference is.

MethodForm of Inference costKL gap (exactness)
Exact / EM E-step, full posteriortractable only for special modelszero — bound is tight
MAPpoint mass one optimization for the modelarge — discards all variance
Mean-field VIfactorized coordinate ascent per datapointnonzero — misses correlations
Amortized VI, one shared netone forward pass after trainingnonzero amortization gap

EM is the exact corner; MAP the degenerate corner; mean-field trades exactness for a tractable factorization; amortization trades a little more exactness for inference in a single forward pass. Across the table, one quantity varies: how much of the true posterior is permitted to capture, with cost and fidelity trading off against each other.

Richer posteriors and tighter bounds

The mean-field family is deliberately crude — its independence assumption is the source of the mode-seeking, variance-shrinking bias diagrammed above. Three developments since Goodfellow (2016) relax that restriction while keeping the ELBO machinery intact.

Normalizing-flow posteriors widen the family . Rezende & Mohamed (2015, ICML) replace the factorized Gaussian with a simple base distribution pushed through a chain of invertible maps , whose change-of-variables Jacobian keeps the density tractable. The resulting can be correlated and multimodal, so the axis-aligned ellipse from the correlated-posterior figure can tilt to match the true density — the KL gap shrinks because the family is no longer forced to be independent.

Tighter bounds attack the slack directly. The importance-weighted autoencoder (IWAE; Burda et al., 2016, ICLR) averages importance samples inside the log,

and this multi-sample bound is provably tighter than the single-sample ELBO, approaching as . It trades compute for exactness: posterior samples per gradient step instead of one.

Black-box / stochastic VI removes the last hand-derivation. Ranganath et al. (2014, AISTATS) and Hoffman et al. (2013, JMLR) showed the ELBO gradient can be estimated by Monte Carlo for any model — using the score-function estimator, or the reparameterization gradient this lesson derived — so variational inference no longer needs a model-specific mean-field update worked out by hand. This is what makes the probabilistic-programming systems of the previous lesson possible: write the generative model, and the ELBO is optimized automatically. All three developments (richer , tighter bound, automatic gradient) still maximize the same evidence lower bound this lesson is built on.

Takeaways

  • Inference is intractable because needs the normalizer , an exponential sum with no factorization for interesting models; the solution is to reframe inference as optimization over a surrogate .
  • The ELBO lower-bounds the log-evidence with slack equal to a KL; maximizing over tightens it, maximizing over learns the model.
  • EM is exact inference (E-step: ) alternated with a likelihood climb (M-step: maximize ); the likelihood is monotone non-decreasing, a staircase of tangent lower bounds.
  • MAP is the point-mass ; mean-field VI factorizes with the coordinate update , biased mode-seeking because it minimizes the reverse KL.
  • Amortized inference learns one encoder that emits posterior parameters in a forward pass, exactly the encoder of a variational autoencoder.

Footnotes

  1. Goodfellow, Deep Learning, §19.1 — Inference as Optimization: recasting the intractable posterior computation as maximizing the evidence lower bound over a surrogate .
  2. Goodfellow, Deep Learning, §19.1 — the ELBO decomposition and its two optimizations (learning in , inference in ).
  3. Goodfellow, Deep Learning, §19.2 — Expectation Maximization: the exact-E-step special case and the monotone-ascent guarantee on the marginal likelihood.
  4. Goodfellow, Deep Learning, §19.3 — MAP Inference and Sparse Coding: the point-mass as the zero-variance limit of a variational family.
  5. Goodfellow, Deep Learning, §19.4 — Variational Inference and Learning: the mean-field factorization, the coordinate-ascent fixed point, and the mode-seeking bias of the reverse KL. 2
  6. Chollet, Deep Learning with Python, Ch. 12 — the VAE encoder as an amortized inference network emitting , trained end-to-end through the reparameterization trick. 2

╌╌ END ╌╌