Recurrent State-Space Models › The Recurrent State-Space Model

Michael BrenndoerferJune 25, 20269 min read
World Models HandbookAll chapters
Part 5 of 12World-Model Architectures
Chapter 25Recurrent State-Space Models › The Recurrent State-Space Model
Reading section 3 of 10 · The Recurrent State-Space Model · 9 minSection 3 of 10 · 9 min

Book contents

Section 3 of 10 · 9 min read

Recurrent State-Space Models › The Recurrent State-Space ModelOne section · Sections in this chapter3 / 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

This section assembles the architecture, name by name, and explains the wiring that makes the posterior and prior cooperate. Read it slowly: every design choice here has a reason, and the reasons compound.

The five components

An RSSM for a partially observable environment with observations oto_t and actions ata_t consists of:

  • Deterministic recurrence (the memory path). A GRU (or LSTM) updates hth_t from the previous deterministic state, the previous sampled latent, and the previous action:

    ht=fθ(ht−1, zt−1, at−1)h_t = f_\theta\big(h_{t-1},\, z_{t-1},\, a_{t-1}\big)
  • Prior (transition model). Before seeing anything at time tt, the model guesses the stochastic state:

    zt∼pθ(zt∣ht)=N(μθ(ht), diag(σθ2(ht)))z_t \sim p_\theta(z_t \mid h_t) = \mathcal{N}\big(\mu_\theta(h_t),\ \mathrm{diag}(\sigma^2_\theta(h_t))\big)
  • Posterior (inference model). During training and filtering, an encoder sees the current observation and re-places ztz_t where the evidence says it should be:

    zt∼qϕ(zt∣ht,ot)=N(μϕ(ht,ot), diag(σϕ2(ht,ot)))z_t \sim q_\phi(z_t \mid h_t, o_t) = \mathcal{N}\big(\mu_\phi(h_t, o_t),\ \mathrm{diag}(\sigma^2_\phi(h_t, o_t))\big)
  • Observation model (decoder). Reconstructs or predicts the observation from the joint state:

    ot∼pθ(ot∣ht,zt)o_t \sim p_\theta(o_t \mid h_t, z_t)
  • Optional heads. In the Dreamer line, a reward head pθ(rt∣ht,zt)p_\theta(r_t \mid h_t, z_t) and a discount or continuation head for episode boundaries (the original Dreamer already modeled early termination). We will use them in Part VII: Planning and Agency; here we focus on the observation model.

where:

  • fθf_\theta: the GRU or LSTM update, a neural network with parameters θ\theta applied identically at every step
  • μθ(ht)\mu_\theta(h_t), σθ2(ht)\sigma^2_\theta(h_t): the prior's mean vector and per-dimension variances, produced from hth_t by a small network head
  • μϕ(ht,ot)\mu_\phi(h_t, o_t), σϕ2(ht,ot)\sigma^2_\phi(h_t, o_t): the posterior's mean vector and per-dimension variances, produced from the pair (ht,ot)(h_t, o_t) by the encoder with parameters ϕ\phi
  • diag(⋅)\mathrm{diag}(\cdot): a covariance matrix carrying the given variance vector on its diagonal and zeros elsewhere, so the Gaussian treats the coordinates as independent conditional on its inputs
  • rtr_t: the reward at time tt

Notice the timing convention, because it trips everyone up at least once. The GRU input at time tt is (zt−1,at−1)(z_{t-1}, a_{t-1}), not ztz_t. So hth_t is the model's prediction of the situation at time tt before the observation oto_t arrives. The observation never touches hth_t directly; it enters the model only through the posterior, which shapes ztz_t, which enters the recurrence at the next step. All per-step information about the current observation must therefore flow through the stochastic latent. This is deliberate, and it is what makes the KL term meaningful, as you will see below. If the observation could leak into hth_t by some side channel, the prior itself could use the observation it is supposed to predict, and the KL would no longer measure a real prediction gap. By forcing all observation information through the sampled bottleneck ztz_t, the architecture guarantees that the only way the model can use the present is by encoding it in exactly the place the prior is trying to predict.

Prior versus posterior

The prior pθ(zt∣ht)p_\theta(z_t \mid h_t) is the transition model: what the model expects before it looks. It exists at all times, including during imagination. The posterior qϕ(zt∣ht,ot)q_\phi(z_t \mid h_t, o_t) is the inference model: an approximate latent distribution after incorporating the evidence. It exists only when an observation is available, such as during training and filtering. The two have the same functional form but different inputs and different jobs.

Assumptions of the RSSM

Separate the modeled structure from assumptions about its adequacy:

  • The generative model is Markov in its joint internal state. Whether the learned representation retains enough history for the environment's prediction tasks must be tested, not assumed from its fixed width.
  • This example uses stationary dynamics and shared parameters across time. Other settings may require time or regime information in the state or inputs.
  • History and actions must contain enough information for the task. Relevant unobservable quantities require a belief over alternatives, not a promise of exact recovery; task-irrelevant hidden variables need not be recovered.
  • Recorded actions are available during training and evaluation. The model is not asked to infer which actions occurred.

The figure below shows the wiring unrolled over three steps. Read the middle column: the prior flows down from hth_t into ztz_t; the posterior flows up from oto_t into ztz_t; the decoder emits a predicted observation from (ht,zt)(h_t, z_t); and the GRU carries (ht−1,zt−1,at−1)(h_{t-1}, z_{t-1}, a_{t-1}) into hth_t. The third column is drawn faded deliberately: it is imagination, where the posterior arrows do not exist and the prior must carry the sequence on its own.

Out[6]:
Visualization
Diagram of RSSM boxes and arrows for prior, posterior, decoder, and GRU across three timesteps.
Schematic RSSM dependencies across three steps. Solid arrows show recurrent, prior, and decoder paths; dashed arrows show observation-conditioned inference and disappear in the imagined third step. For readability the extra h inputs to the posterior and decoder are written in their conditional-distribution labels rather than drawn as separate arrows. Both must be included in an implementation.

Why keep both paths

The division of labor between hh and zz is the design insight that makes the architecture work, so it deserves a careful statement.

The deterministic path provides a direct recurrent route for information and gradients. With differentiable gates, backpropagation through a GRU is an ordinary chain-rule calculation conditional on its inputs. This can help preserve information that would be difficult to carry through repeated stochastic transitions. It does not guarantee small gradient variance or stable optimization: the inputs still include sampled latents, and the recurrent Jacobians can attenuate or amplify gradients. PlaNet's ablation is evidence about its tested architecture and tasks, rather than a proof that stochastic-only models cannot learn.

The stochastic path supplies sampled latent variation. In the graph defined here, oto_t enters the joint state through the posterior distribution for ztz_t, then influences later memory through the recurrence. The KL compares distributions, not two sampled values: even identical distributions can generate different samples, and an observation can change posterior variance without changing its mean. Uncertainty can also reside in distribution parameters carried by memory, in the observation model, and in uncertainty about fitted parameters. No single latent draw contains all of those uncertainties.

The Kalman analogy concerns the order of computation: predict a state distribution, then update it using an observation. A Kalman filter represents uncertainty with both a mean and a covariance, and its innovation is an observation residual, not a KL divergence. Under the linear-Gaussian assumptions its update is exact; an RSSM uses learned nonlinear dynamics and approximate inference. Comparing filtering with imagination can help locate a failure, but does not isolate one guilty component: the encoder, transition, decoder, and available information all influence the observed errors.

DreamerV2 (Hafner et al., 2021) uses 32 categorical variables with 32 classes each, sampled with straight-through gradients; DreamerV3 retains categorical stochastic latents. Categoricals provide discrete alternatives in latent coordinates. A diagonal Gaussian is unimodal conditional on its context in those coordinates, but a nonlinear decoder can still turn Gaussian latents into multimodal or correlated observations. The high-level prior/posterior/recurrence graph remains recognizable across the variants, while the distribution parameterization, KL calculation, and gradient estimator change. Straight-through differentiation is a biased surrogate; its practical usefulness does not imply that score-function estimators inevitably have exploding variance.