Latent-Variable Representations › Variational Inference: Making Latents Probabilistic

Michael BrenndoerferJune 15, 20267 min read
World Models HandbookAll chapters
Part 3 of 12Representing Agents and Worlds
Chapter 15Latent-Variable Representations › Variational Inference: Making Latents Probabilistic
Reading section 3 of 11 · Variational Inference: Making Latents Probabilistic · 7 minSection 3 of 11 · 7 min

Book contents

Section 3 of 11 · 7 min read

Latent-Variable Representations › Variational Inference: Making Latents ProbabilisticOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

This section builds the variational machinery step by step: the latent-variable model and its intractable marginal, the evidence lower bound with its two-term decomposition, and the two techniques, reparameterization and amortization, that make the objective trainable by gradient descent.

Latent-variable models and the intractable marginal

Latent-variable model

A latent-variable model posits that each observation xx was produced by first drawing an unobserved variable zz from a prior pθ(z)p_\theta(z), then drawing the observation from a likelihood pθ(x∣z)p_\theta(x \mid z). The joint density is pθ(x,z)=pθ(z) pθ(x∣z)p_\theta(x, z) = p_\theta(z)\, p_\theta(x \mid z), and the model's density over observations is the marginal pθ(x)=∫pθ(x,z) dzp_\theta(x) = \int p_\theta(x, z)\, dz.

This is the modeling direction we want: generate an observation xx from a latent zz, and infer a distribution over zz after observing xx. The factorization is statistical, not automatically causal. Calling zz a physical cause requires additional structural assumptions and validation through time or interventions. Even without that causal interpretation, the direction is useful: it supports sampling, conditioning, and later models of action-conditioned latent transitions.

Training means adjusting θ\theta so that pθ(x)p_\theta(x) is high on real observations. For a generic nonlinear neural-network decoder, the marginal usually has no tractable closed form, even with a low-dimensional continuous zz; analytic special cases such as the linear-Gaussian model do exist. The ELBO gives us a differentiable surrogate for the generic case.

The classical move, which you met in the filtering chapter, is to approximate the posterior with variational inference. Introduce a family of distributions qϕ(z∣x)q_\phi(z \mid x), the approximate posterior or encoder, and optimize it to stay close to the true posterior pθ(z∣x)p_\theta(z \mid x). There is a pleasing symmetry in the setup: the generative model runs in the direction z→xz \to x, from state to observation, while the inference model runs in the reverse direction x→zx \to z, from observation to belief. In a Bayes filter, the direct analogue of pθ(x∣z)p_\theta(x \mid z) is the observation likelihood, and inference combines that likelihood with a prior belief in the measurement update. A separate transition model supplies the prior belief when temporal dynamics are introduced. Here the observation and inference directions are learned together, and the next section shows how a single objective trains them.

The ELBO, step by step

We cannot minimize KL(qϕ(z∣x) ∥ pθ(z∣x))\mathrm{KL}(q_\phi(z \mid x) \,\|\, p_\theta(z \mid x)) directly because the true posterior contains the intractable marginal: by Bayes' rule, pθ(z∣x)=pθ(x,z)/pθ(x)p_\theta(z \mid x) = p_\theta(x, z) / p_\theta(x), and it is exactly pθ(x)p_\theta(x) that we cannot compute. The trick is to rewrite the log marginal density in a form that sidesteps it. For the following identity, assume qϕ(z∣x)q_\phi(z \mid x) is positive wherever pθ(x,z)p_\theta(x,z) is positive for the fixed xx, and that the displayed expectations exist. Start from the definition of the marginal and multiply and divide by qϕ(z∣x)q_\phi(z \mid x):

log⁡pθ(x)=log⁡∫qϕ(z∣x) pθ(x,z)qϕ(z∣x) dz.\log p_\theta(x) = \log \int q_\phi(z \mid x)\, \frac{p_\theta(x, z)}{q_\phi(z \mid x)}\, dz.

This manipulation puts the chosen approximation qq inside the integral for log⁡pθ(x)\log p_\theta(x), creating an expectation over qq of the ratio pθ(x,z)/qϕ(z∣x)p_\theta(x, z)/q_\phi(z \mid x). The logarithm of an expectation is hard, so apply Jensen's inequality, which holds because log⁡\log is concave:

log⁡pθ(x)  ≥  Eqϕ(z∣x)[log⁡pθ(x,z)qϕ(z∣x)]  =  L(θ,ϕ;x).\log p_\theta(x) \;\ge\; \mathbb{E}_{q_\phi(z \mid x)} \left[ \log \frac{p_\theta(x, z)}{q_\phi(z \mid x)} \right] \;=\; \mathcal{L}(\theta, \phi; x).

The right-hand side is the evidence lower bound, or ELBO. Two facts make it the workhorse of representation learning.

First, the gap is exactly the quantity we could not compute:

log⁡pθ(x)−L(θ,ϕ;x)=KL(qϕ(z∣x) ∥ pθ(z∣x))  ≥  0.\log p_\theta(x) - \mathcal{L}(\theta, \phi; x) = \mathrm{KL}\big(q_\phi(z \mid x) \,\|\, p_\theta(z \mid x)\big) \;\ge\; 0.

For fixed generative parameters θ\theta, maximizing the ELBO over ϕ\phi reduces the KL gap to the true posterior within the chosen variational family. Joint optimization over θ\theta and ϕ\phi also raises the bound used to fit the generative model, although neither quantity is guaranteed to improve monotonically at every stochastic update. When the gap is zero, the encoder has recovered the exact posterior for that model.

Second, the ELBO splits into two interpretable terms. Expand log⁡pθ(x,z)=log⁡pθ(x∣z)+log⁡pθ(z)\log p_\theta(x, z) = \log p_\theta(x \mid z) + \log p_\theta(z):

L(θ,ϕ;x)=Eqϕ(z∣x)[log⁡pθ(x∣z)]⏟reconstruction  −  KL(qϕ(z∣x) ∥ pθ(z))⏟prior regularization.\mathcal{L}(\theta, \phi; x) = \underbrace{\mathbb{E}_{q_\phi(z \mid x)} \big[ \log p_\theta(x \mid z) \big]}_{\text{reconstruction}} \;-\; \underbrace{\mathrm{KL}\big(q_\phi(z \mid x) \,\|\, p_\theta(z)\big)}_{\text{prior regularization}}.

The first term rewards reconstruction; the second penalizes each approximate posterior for deviating from the prior. This defines a reference measure for sampling and indirectly pressures the aggregated posterior toward that prior. It does not guarantee hole-free coverage, plausible interpolation, or semantic distance. Those geometric properties require direct evaluation or additional structure. What the KL does provide is an explicit price for information: every posterior deviation from the prior has to earn its reconstruction benefit.

It is worth being precise about what the regularization term is not. It does not require encoded training data to fill the prior, and prior density is not semantic geometry. Interpolation and metric alignment remain empirical questions.

The reparameterization trick and amortized inference

Two engineering problems stand between the ELBO and gradient descent, and both have elegant, now-standard solutions.

Problem 1: a black-box draw blocks ordinary pathwise backpropagation to the distribution parameters. Score-function estimators are possible, but often have high variance. The reparameterization trick instead writes a draw as a differentiable transform of parameter-free noise. For a Gaussian encoder,

z=μϕ(x)+σϕ(x)⊙ε,ε∼N(0,I),z = \mu_\phi(x) + \sigma_\phi(x) \odot \varepsilon, \qquad \varepsilon \sim \mathcal{N}(0, I),

where μϕ\mu_\phi and σϕ\sigma_\phi are network outputs and ⊙\odot is elementwise multiplication. All randomness now lives in ε\varepsilon, which does not depend on any parameter, and gradients flow through μϕ\mu_\phi and σϕ\sigma_\phi. The conceptual shift is subtle but important: instead of sampling a value of zz, we sample a source of randomness and then compute zz deterministically from it. The function from parameters to samples is now smooth, so the expectation over ε\varepsilon can be estimated by a handful of Monte Carlo draws, often just one, and each estimate carries useful gradients. This trick was one key ingredient that made the variational autoencoder of Kingma and Welling (2013) and Rezende, Mohamed, and Wierstra (2014) practical; amortized inference, introduced next, supplies the other ingredient needed to encode new observations efficiently. Reparameterization recurs throughout this book whenever a stochastic layer must be threaded through a differentiable computation.

Problem 2: a separate optimization per data point. Traditional local variational inference often optimizes a dedicated set of variational parameters for each observation. That is poorly suited to a world model that must encode new observations under a tight latency budget, because a long inner optimization loop may not fit between camera frames. Instead we amortize inference: one encoder network qϕ(z∣x)q_\phi(z \mid x) learns to output the variational parameters for any xx. The term emphasizes that the cost of learning an inference procedure is shared across the data distribution rather than paid through a fresh optimization for every observation. Amortization trades per-observation optimization cost for a single upfront training cost, and it is what lets a learned world model ingest a fresh camera frame in one forward pass. The trade has a known downside worth naming: the encoder can be systematically imprecise on inputs it has rarely seen, a form of amortization error that dedicated per-observation optimization can reduce, though not necessarily eliminate. Local optimization may remain incomplete, and the chosen variational family can still be misspecified. For agents with tight latency budgets, amortized inference is often the practical default; whether to add bounded per-observation refinement depends on the available compute, the approximation error, and the cost of an inference mistake.

The variational autoencoder (VAE)

A VAE is a latent-variable model trained by maximizing the ELBO with a reparameterized, amortized encoder. For a Gaussian prior p(z)=N(0,I)p(z) = \mathcal{N}(0, I) and Gaussian encoder, the per-observation objective is

L=Eε[log⁡pθ(x∣μϕ(x)+σϕ(x)⊙ε)]−KL(N(μϕ(x),diag(σϕ2(x))) ∥ N(0,I)),\mathcal{L} = \mathbb{E}_{\varepsilon}\big[ \log p_\theta(x \mid \mu_\phi(x) + \sigma_\phi(x) \odot \varepsilon) \big] - \mathrm{KL}\big(\mathcal{N}(\mu_\phi(x), \mathrm{diag}(\sigma_\phi^2(x))) \,\|\, \mathcal{N}(0, I)\big),

where the KL term has a closed form: −12∑j(1+log⁡σj2−μj2−σj2)-\tfrac{1}{2} \sum_j \big(1 + \log \sigma_j^2 - \mu_j^2 - \sigma_j^2\big).

The closed form of the Gaussian KL shows what the regularizer asks of the encoder. Each dimension is minimized when the encoder's mean μj\mu_j is zero and its variance σj2\sigma_j^2 is one. The KL pressure pulls each encoding distribution toward zero mean and unit variance. The reconstruction term pushes the other way by rewarding informative codes.