Recurrent State-Space Models › Learning the Model: Posterior Inference and the ELBO

Michael BrenndoerferJune 25, 202610 min read
World Models HandbookAll chapters
Part 5 of 12World-Model Architectures
Chapter 25Recurrent State-Space Models › Learning the Model: Posterior Inference and the ELBO
Reading section 4 of 10 · Learning the Model: Posterior Inference and the ELBO · 10 minSection 4 of 10 · 10 min

Book contents

Section 4 of 10 · 10 min read

Recurrent State-Space Models › Learning the Model: Posterior Inference and the ELBOOne section · Sections in this chapter4 / 10 ↓
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

You now know what the parts are. This section explains how they are trained, and why the training objective has the peculiar two-part structure it does.

The filtering ELBO

Consider a single training sequence of observations o1:To_{1:T} and actions a1:Ta_{1:T}. The generative story is:

  1. Start from an initial belief (h1h_1 can be a learned vector or fixed zeros, with the initial latent prior conditioned on it).
  2. For each tt: draw ztz_t from the prior pθ(zt∣ht)p_\theta(z_t \mid h_t), emit oto_t from pθ(ot∣ht,zt)p_\theta(o_t \mid h_t, z_t), then update ht+1=fθ(ht,zt,at)h_{t+1} = f_\theta(h_t, z_t, a_t).

The action-conditioned likelihood pθ(o1:T∣a1:T−1)p_\theta(o_{1:T}\mid a_{1:T-1}) integrates over latent trajectories. For a general nonlinear neural model, that sum or integral is intractable; linear-Gaussian models are an important tractable exception. We use the filtering variational factorization q(z1:T∣o1:T,a1:T−1)=∏tqϕ(zt∣ht,ot)q(z_{1:T}\mid o_{1:T},a_{1:T-1})=\prod_t q_\phi(z_t\mid h_t,o_t), where each hth_t depends on earlier sampled latents. Jensen's inequality gives an evidence lower bound on the log likelihood. Written with all expectations visible, it is

L=∑t=1TEq(z<t) ⁣[Eqϕ(zt∣ht,ot)[log⁡pθ(ot∣ht,zt)]⏟reconstruction−KL ⁣(qϕ(zt∣ht,ot) ∥ pθ(zt∣ht))⏟prior-posterior regularization]\mathcal{L} = \sum_{t=1}^{T}\mathbb{E}_{q(z_{<t})}\!\left[ \underbrace{\mathbb{E}_{q_\phi(z_t\mid h_t,o_t)}[\log p_\theta(o_t\mid h_t,z_t)]}_{\text{reconstruction}} -\underbrace{\mathrm{KL}\!\left(q_\phi(z_t\mid h_t,o_t)\,\|\,p_\theta(z_t\mid h_t)\right)}_{\text{prior-posterior regularization}} \right]

The outer expectation averages over the earlier posterior draws that determine hth_t; at t=1t=1 there are no earlier latents. The inner expectation averages the current reconstruction likelihood. We estimate these expectations with a sampled trajectory, drawing zt=μϕ+σϕ⊙ϵtz_t=\mu_\phi+\sigma_\phi\odot\epsilon_t, where ϵt∼N(0,I)\epsilon_t\sim\mathcal{N}(0,I) is parameter-independent noise and σϕ>0\sigma_\phi>0. This reparameterization exposes a differentiable map from the mean and scale to the sample, so ordinary backpropagation supplies a pathwise Monte Carlo gradient under the usual differentiability and integrability conditions. The expected objective is a lower bound; an individual Monte Carlo estimate is not guaranteed to lie below the log likelihood.

Filtering uses only past and current observations. If a smoothing variational family contains this filtering family, its optimal ELBO cannot be lower, because it can use future observations too. That is a statement about the families' optima, not a promise about two fitted networks. One posterior trajectory is also not a particle filter with importance weighting and resampling. Finally, the decoder log density at a given sample may be analytic while its expectation through a nonlinear decoder is not. The conditional diagonal-Gaussian KL does have the closed form below; its outer expectation over earlier latent history is still estimated by sampling.

KL(q ∥ p)=∑i[log⁡σp,iσq,i+σq,i2+(μq,i−μp,i)22σp,i2−12]\mathrm{KL}(q \,\|\, p) = \sum_{i} \left[ \log \frac{\sigma_{p,i}}{\sigma_{q,i}} + \frac{\sigma_{q,i}^2 + (\mu_{q,i} - \mu_{p,i})^2}{2\sigma_{p,i}^2} - \frac{1}{2} \right]

where:

  • qq: shorthand for the posterior Gaussian qϕ(zt∣ht,ot)q_\phi(z_t \mid h_t, o_t), with mean μq,i\mu_{q,i} and standard deviation σq,i\sigma_{q,i} along latent dimension ii
  • pp: shorthand for the prior Gaussian pθ(zt∣ht)p_\theta(z_t \mid h_t), with mean μp,i\mu_{p,i} and standard deviation σp,i\sigma_{p,i}
  • ii: an index over the latent dimensions, so the sum runs over the coordinates of ztz_t

The three summands divide the work: the log-scale ratio and normalized variance compare their widths, the normalized squared mean gap penalizes mean disagreement, and −1/2-1/2 subtracts the posterior's expected standardized quadratic contribution. The terms cancel jointly: when the two Gaussians coincide the log ratio is zero and the variance term plus the constant −1/2-1/2 sum to zero, so the divergence vanishes exactly when the two Gaussians coincide. This closed form is what the code below implements directly. Notice also the asymmetry of the divergence: because it is the KL with qq first, the prior's standard deviation σp,i\sigma_{p,i} appears in the denominator, so a prior that is too confident (small σp\sigma_p) pays a steep price when the posterior disagrees with it. For distinct prior-head parameters under this posterior-sampled objective, the 1/σp21/\sigma_p^2 denominator punishes prior overconfidence steeply, and the KL is the only term whose gradient reaches the prior distribution's parameters (the mean and scale head of pθ(zt∣ht)p_\theta(z_t \mid h_t); the reconstruction gradient still reaches the deterministic transition fθf_\theta through the sampled ztz_t). Its gradient pulls the posterior toward the prior too. That is the collapse route the stabilizers below are designed to suppress.

The asymmetry is worth seeing as a curve rather than a formula.

In[7]:
Code
import numpy as np

## KL( N(mu_q, 1) || N(mu_p, sigma_p) ) for a unit-width posterior, as the
## posterior mean mu_q moves away from the prior mean mu_p = 0, for several
## prior standard deviations. Fixed seed is not needed: the grid is
## deterministic.
gap = np.linspace(0.0, 3.0, 300)
prior_stds = np.array([0.25, 0.5, 1.0, 2.0])
kl_curves = {}
for sp in prior_stds:
    kl_curves[sp] = np.log(sp) + (1.0 + gap**2) / (2.0 * sp**2) - 0.5
Out[8]:
Console
prior sigma=0.25: KL at gap 1 =  14.11 nats, KL at gap 3 =    78.11 nats
prior sigma=0.50: KL at gap 1 =   2.81 nats, KL at gap 3 =    18.81 nats
prior sigma=1.00: KL at gap 1 =   0.50 nats, KL at gap 3 =     4.50 nats
prior sigma=2.00: KL at gap 1 =   0.44 nats, KL at gap 3 =     1.44 nats
Out[9]:
Visualization
Rising curves of KL versus mean gap for four prior widths.
KL divergence against the posterior-prior mean gap for priors of different width, with the posterior held at unit variance. An overconfident prior (small sigma) pays a steeply growing penalty for the same disagreement, while a wider prior has a smaller mean-mismatch penalty over this range but still pays for variance mismatch.

Read the vertical gap between the curves: at a mean gap of one, the narrowest prior already pays about 13.7 nats more than the widest one (14.11 versus 0.44) for the identical posterior. The KL direction therefore punishes prior overconfidence, which is exactly the behavior a dynamics-learning signal should have.

What each term teaches

The two terms have distinct jobs. Their traces can suggest where to investigate a broken training run, but do not uniquely identify a failing component.

The reconstruction term trains the posterior, decoder, and recurrent transition to make observations predictable. It encourages the posterior to encode current information that helps the decoder, but does not force use of every latent dimension. The current reconstruction has no dependency on ht+1h_{t+1}: it is later losses that reward retaining useful information there. In this observation-only example, reconstruction is the direct incentive to encode observations; an agent with reward or other auxiliary heads would have additional incentives.

The KL term trains the prior and regularizes the posterior. Either distribution can move to reduce it. Its initial value and subsequent trend depend on both networks, so neither a large starting KL nor a monotonic decline is guaranteed. Residual KL is not sensor-noise variance. In a scalar exact Gaussian update with prior variance PP and measurement variance RR, expected information gain is 12log⁡(1+P/R)\tfrac12\log(1+P/R), while innovation variance is P+RP+R. Increasing sensor noise can therefore decrease expected information gain while increasing innovation variance. Nats and squared observation units describe different quantities.

Posterior collapse, free nats, and KL balancing

Posterior collapse

Posterior collapse means the posterior stops using observation information beyond what the prior already supplies. Exact equality q(zt∣ht,ot)=p(zt∣ht)q(z_t\mid h_t,o_t)=p(z_t\mid h_t) gives zero conditional KL, but zero KL alone does not prove useless prediction: the history may already explain the relevant observation. Collapse is harmful when the latent drops information needed for the task. Check observation dependence and prediction quality, rather than treating a low scalar KL as a complete diagnosis.

Collapse happens when the KL cost of encoding information exceeds the reconstruction benefit of using it. The ELBO literature developed several remedies, and the RSSM line uses two that you should know by name.

Free nats replace the per-step KL by max⁡(KL,ϵfree)\max(\mathrm{KL},\epsilon_{\mathrm{free}}). PlaNet and the original Dreamer use 3 nats; the cited DreamerV3 paper uses 1 nat. Below the threshold, this term is constant and supplies no gradient through either KL argument. It removes marginal regularization pressure in that region; it does not measure irreducible surprise or guarantee that the posterior uses the available capacity. A collapsed posterior equal to its prior also has a clipped value at the floor. We use a 1-nat threshold below and log both raw and clipped KL to keep these cases distinguishable. Reconstruction and shared upstream parameters can still change the distributions while the clipped KL's own gradient is zero.

KL balancing (DreamerV2, kept in DreamerV3): split the KL gradient by stopping gradients through one argument at a time:

Ldyn=λdyn KL(sg[q] ∥ p),Lrep=λrep KL(q ∥ sg[p])\mathcal{L}_{\mathrm{dyn}} = \lambda_{\mathrm{dyn}}\, \mathrm{KL}\big(\mathrm{sg}[q] \,\big\|\, p\big), \qquad \mathcal{L}_{\mathrm{rep}} = \lambda_{\mathrm{rep}}\, \mathrm{KL}\big(q \,\big\|\, \mathrm{sg}[p]\big)

where:

  • qq and pp: shorthand for the per-step posterior qϕ(zt∣ht,ot)q_\phi(z_t \mid h_t, o_t) and prior pθ(zt∣ht)p_\theta(z_t \mid h_t)
  • sg[⋅]\mathrm{sg}[\cdot]: stop-gradient, meaning the bracketed distribution is held fixed, so that term's gradient can only reach the other argument
  • λdyn\lambda_{\mathrm{dyn}} and λrep\lambda_{\mathrm{rep}}: the loss weights. DreamerV2 introduced the balance with the factor α=0.8\alpha = 0.8, weighting the dynamics term four times the representation term; the DreamerV3 paper v2 applies loss scales of λdyn=1.0\lambda_{\mathrm{dyn}} = 1.0 and λrep=0.1\lambda_{\mathrm{rep}} = 0.1 to the two terms

Both terms are minimized alongside the negative reconstruction objective. The first differentiates through the prior argument with the posterior argument stopped; the second reverses those roles. Shared upstream parameters, including the recurrent context, may influence both distributions after an update. Larger dynamics coefficients emphasize direct prior-fitting gradients, but coefficient ratios are not gradient-norm ratios and do not guarantee an optimizer's outcome. The aim is to reduce the incentive to discard useful observation information merely to make matching easier. DreamerV3 reports results across its evaluated domains with a shared configuration and several coordinated design choices; that empirical result should not be attributed to KL balancing alone.

Loss units matter for both images and low-dimensional observations. A sum over many pixels differs from a mean over pixels, and the observation likelihood's scale determines reconstruction strength. For a fixed scalar Gaussian decoder variance σo2\sigma_o^2, negative log likelihood equals squared error divided by 2σo22\sigma_o^2, plus a parameter-independent constant. Thus squared error plus β KL\beta\,\mathrm{KL} is, up to positive scaling and an additive constant, a Gaussian negative-ELBO objective with σo2=β/2\sigma_o^2=\beta/2. Here β=0.1\beta=0.1 corresponds to variance 0.050.05 before clipping. Free-nat clipping modifies that objective; a learned variance would also introduce scale-dependent terms. Scalar loss magnitudes alone do not establish gradient dominance, especially when a large clipped term is locally constant.

The posterior-prior gap

During filtering, the posterior uses the currently available observation; during imagination, future states come from the prior. At fixed recurrent context hh, averaging KL(q(z∣h,o)∥p(z∣h))\mathrm{KL}(q(z\mid h,o)\|p(z\mid h)) over a specified observation distribution gives two terms: the mutual information between zz and oo under that joint distribution, plus the KL between the observation-averaged posterior and the prior. Consequently, expected KL upper-bounds that observation information but is not equal to it in general. It mixes useful or irrelevant encoded information with prior mismatch; it is not a direct measure of forecast error.

Keep observation-conditioned filtering and open-loop prediction separate when evaluating a model. The experiment below measures both; it does not train a policy or measure closed-loop decision usefulness. A good posterior reconstruction can coexist with poor imagination because observations supply information that an open-loop forecast lacks. Approximation and training failures can widen that difference too. A high held-out KL is a reason to investigate, not proof of a failed prior: check predictions, interval quality, and which observations change the inferred state.