Chapter 9 · World models and model-based learning

§9.x Hands-on exercise + chapter references

0:00/14:10
AI-narrated by Kokoro

Chapter 9 makes one claim that stays abstract until you watch it fail: a learned world model is only as useful as its rollouts are accurate, and its rollouts degrade the further into the future you ask them to reach. §9.1 defined the model, §9.2 built it, §9.3 searched it, and §9.5 argued about whether the whole apparatus is the future of robotics or a detour. None of that lands until you have trained a model, rolled it forward, and put its prediction next to what actually happened. The headline exercise does exactly that — train a compact Dreamer-style agent on a pixel task and lay its imagined rollout beside the ground-truth frames — which is the canonical Chapter 9 drill and the fastest way to see both the promise and the drift in one picture. The remaining drills probe the edges: how horizon length trades against accuracy, what the RSSM’s stochastic branch is actually for, and where the world-model bet of §9.5 stops being a slogan and becomes a measurement.

Budget about three hours. Most of that is wall-clock training time you spend doing something else; the code you write is short. The one real cost is the first exercise’s training run, so start it early and read while it runs.

pip install torch dm-control

You need one pixel-input control task with a low-dimensional action. DeepMind Control’s cartpole-swingup trains fastest and is the gentlest place to watch a latent rollout track reality; walker-walk is more interesting and more punishing if you have the patience. Everything after the install runs offline.

Exercise 9.x.1 — Train a world model, then watch it imagine

Build a small RSSM agent following §9.2. You do not need to reproduce DreamerV3’s full stack; a scaled-down version is enough to see the phenomena. The pieces, all named in §9.2: a convolutional encoder from pixels to a feature vector, a recurrent core that carries the deterministic hidden state hth_t forward, a stochastic state ztz_t with a prior (predicted from hth_t alone) and a posterior (corrected by the current observation), a decoder that reconstructs the frame, and reward and continue heads. Train it on replay data — either DreamerV3’s own actor collecting experience, or, if you want to skip the actor entirely for now, a buffer of random-policy episodes. Optimize the §9.2 loss: reconstruction plus reward prediction plus the KL between prior and posterior. Let the reconstruction loss plateau; on cartpole-swingup that is roughly a hundred thousand environment steps, well under an hour on a single GPU and a couple of hours on CPU.

Now the actual experiment, and the one the TOC names. Pick a held-out episode the model never trained on. Feed the model the first few frames so it can build up a posterior state — this is the “warm-up” or context window — then cut off the observations and let it run on the prior alone, imagining forward from its own predictions with the recorded actions as input. Decode each imagined latent back to a frame. Lay the imagined filmstrip directly above the ground-truth filmstrip, frame for frame, and look.

The first several frames will be nearly indistinguishable: the pole is where it should be, the cart is where it should be. Then the two strips begin to diverge, and the way they diverge is the lesson. Small dynamical errors compound — a pole angle off by a degree becomes off by ten, then the imagined pole is falling while the real one is upright. Mark the frame where you can first tell the two strips apart without squinting. That frame is your model’s honest planning horizon, and it is almost always shorter than you hoped. This is the drift §9.3 warned about and the reason §9.3’s planners re-solve every control step instead of trusting one long rollout.

Wall clock: training aside, about thirty minutes for the rollout and the side-by-side plot.

Exercise 9.x.2 — Find the horizon where planning stops helping

This drill turns the drift you just watched into a number that changes a design decision. Take the trained model from 9.x.1 and use it to plan, the §9.3 way: at the current latent state, sample action sequences of length HH, score each by predicted return under the model, and execute the first action of the best sequence — the cross-entropy method or MPPI, either is fine. Sweep the planning horizon HH across a range — say 1, 5, 15, 30, 50 steps — and record the achieved return in the real environment at each setting. Plot horizon against achieved return.

The curve is not monotonically increasing, and that is the point. Short horizons plan myopically and leave return on the table. But past some horizon the achieved return stops climbing and then falls, because the plan is now scoring itself against imagined frames the model got wrong — you are optimizing an action sequence to look good in a hallucination. The peak of that curve is the horizon where your model’s accuracy and your planner’s foresight are in balance, and it should sit near the divergence frame you marked in 9.x.1. For the written part, connect the two numbers explicitly and state the consequence: this is why §9.3’s receding-horizon loop plans short and often, and why TD-MPC (Hansen et al., 2022) bootstraps a learned value at the end of a short rollout instead of rolling out to the true horizon at all.

Wall clock: about forty-five minutes reusing 9.x.1’s model.

Exercise 9.x.3 — Delete the stochastic state and break the drawer

§9.2 argued that splitting the latent into a deterministic hth_t and a stochastic ztz_t is the one design choice that makes the RSSM learnable, and the argument is easy to nod along to and hard to feel. Feel it. Take your 9.x.1 model and ablate the stochastic branch: force ztz_t to a deterministic function of hth_t, so the model can no longer represent “I don’t know yet.” Retrain and re-run the imagination rollout from 9.x.2.

On a fully observed task like cartpole-swingup the ablated model may barely suffer, which is itself worth noting — determinism costs nothing when nothing is hidden. To make the cost appear, introduce partial observability: mask a patch of the frame, or switch to a task where an outcome is genuinely uncertain until revealed (the stapler-or-snake drawer of §9.2 is the mental image). Now the deterministic model is forced to commit to one guess about the hidden variable and is punished by reconstruction loss for guessing wrong, while the full RSSM can spread probability across outcomes and pay less. Report the reconstruction loss of both models on the partially observed task. The gap is the concrete value of the stochastic state, and it is exactly the failure §9.2 predicted rather than one you had to be told about.

Wall clock: about forty minutes if you reuse the training code and only change the latent.

Exercise 9.x.4 — Weigh the world-model bet against a policy

§9.5 laid out the disagreement — learn dynamics and plan on top, or map observations straight to actions and let physics be absorbed implicitly — and insisted you would spend the rest of the book in the second camp. Before you do, price the first camp honestly. Take your trained world model and, without any further environment interaction, train a policy purely inside its imagination: roll the model forward from replayed start states, let an actor-critic learn on the imagined trajectories, and deploy the resulting policy on the real task. This is Dreamer’s actual training loop, and the thing to measure is sample efficiency: how many real environment steps did the whole pipeline consume to reach a given return, counting only the steps used to fit the model?

Compare that number against a model-free baseline from Chapter 7 — the SAC agent from Exercise 7.x.2 is the clean comparison — run to the same return on the same task. The world-model pipeline should reach competence in dramatically fewer real steps, because it manufactures cheap experience in imagination; that efficiency is the §9.5 world-model bet stated as a measurement. Then write the counter-bet in one paragraph: name what the pipeline cost you in return for that efficiency — the model to build and debug, the drift that caps the horizon, the reconstruction objective that spends capacity on pixels a policy would have ignored. That paragraph is the §9.5 debate in your own words, grounded in a number you produced, and it is the frame you should carry into Part 4, where every system drops the world model and bets the other way.

Wall clock: about forty-five minutes if 9.x.1’s model is already trained; longer if you train the SAC baseline from scratch.

Chapter 9 reading list

The works below are cited in §9.1–§9.6, grouped by purpose. Full bibliographic entries for everything cited in the book live in Appendix E.2; this is the chapter-local subset.

The idea of a learned simulator

Latent dynamics and Dreamer

Planning against a learned model

Video-prediction world models

The architecture debate

Chapter summary

Chapter 9 was the road not taken, walked far enough to know what is on it. You can now say precisely what a world model is — a learned approximation of the transition and reward functions, fit by supervised learning on transitions the agent has already seen, whose value is that it lets you try an action in imagination before paying for it in reality. You can build the RSSM that makes this work on pixels, and explain why its split into a deterministic memory and a stochastic “what I don’t yet know” state is the choice that keeps it learnable — a claim Exercise 9.x.3 turned from assertion into a reconstruction gap you measured. You can plan against such a model with a receding-horizon search, and you know from Exercise 9.x.2 why that search must be short and frequent: rollout accuracy decays with horizon, and past a point the planner optimizes against its own hallucinations. And you can state the architecture bet of §9.5 in both directions — learn dynamics first, or map observations to actions and let physics take care of itself — and back the trade-off with the sample-efficiency number Exercise 9.x.4 had you produce. That bet is the hinge of the book. Every system in Part 4 takes the second side of it, and Chapter 10 begins there, with the generative machinery — diffusion and flow models — that lets a policy emit smooth, multimodal actions without ever modeling the world it acts in.

This section has been read times.

References

  1. Ha & Schmidhuber (2018). World Models. NeurIPS.
  2. Hafner et al. (2019). Learning latent dynamics for planning from pixels (PlaNet). ICML.
  3. Hafner et al. (2023). Mastering diverse domains through world models (DreamerV3). arXiv:2301.04104.
  4. Bruce et al. (2024). Genie: generative interactive environments. ICML.
  5. LeCun (2022). A path towards autonomous machine intelligence. OpenReview position paper.