Search

Search papers and lessons

World Model

Level 3 · 13 min read

A world model is a learned simulator: a network that predicts how the environment responds to an agent's actions, so the agent can practise inside the prediction.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Neural network basics
  • Discrete math
  • Generative models
  • Sign in to see your progress

TL;DR

A world model is a neural network that learns how an environment responds to actions. It predicts the next observation, or a compressed version of it, from the current state and the action taken. An agent can then plan or practise inside the model instead of inside the real environment, which is slow or costly. Ha and Schmidhuber's 2018 paper trained a small agent to drive a racing game and to survive in a Doom-style game, using only dreams generated by the learned model for the second task.

Where you'll see it: model-based reinforcement learning, robot control where real trials are expensive, and video-prediction systems that generate playable environments from data.

Intuition

A simulator the agent learned

A pilot trains on a flight simulator before flying a real plane. The simulator is not the aircraft, but it captures enough of its response to practise on. A world model is a simulator that nobody wrote by hand. It is fitted to recorded experience, so it captures whatever the data shows, including the parts that are hard to write down, such as how a crowd of monsters moves.

Once the model exists, the agent can roll it forward. Starting from a real observation, the agent proposes an action, the model predicts what happens, and the agent proposes the next action. The sequence of predictions is called a dream, or an imagined rollout. A policy trained on dreams can then be tested in the real environment.

Why compress the observation

Raw pixels are expensive to predict. A 64 by 64 colour frame has 12,288 numbers, and most of them are background that does not matter for the decision. The usual design splits the problem in three. An encoder compresses each frame into a short latent vector. A sequence model predicts how that vector changes given the action. A small controller chooses actions from the latent vector and the sequence model's memory. The controller can be tiny because the hard work of perception and prediction has been done elsewhere.

flowchart LR
    O[Frame o_t] --> V[Encoder V]
    V --> Z[Latent z_t]
    Z --> C[Controller C]
    H[Memory h_t] --> C
    C --> A[Action a_t]
    A --> M[Sequence model M]
    Z --> M
    M --> H
    M --> Zn["Predicted z(t+1)"]

The paper's choice for V is a variational autoencoder (see Variational Autoencoder), for M a recurrent network with a mixture-density output (see Recurrent Neural Network), and for C a linear map trained by evolution.

The math

Let oto_t be the observation at time tt, ata_t the action, and ztz_t the latent code of oto_t. The state of the sequence model is the hidden vector hth_t. The environment is assumed to be a Markov Decision Process, so the next observation depends on the current observation and action. The world model approximates the environment's dynamics with a learned distribution over the next latent code:

pθ(zt+1∣zt,at,ht).p_\theta(z_{t+1} \mid z_t, a_t, h_t).
Definition: World model

A world model is a learned approximation to the environment's transition distribution, written in a compressed space. In the Ha and Schmidhuber design it has three parts:

  1. An encoder zt=V(ot)z_t = V(o_t), trained first and without reward.
  2. A recurrent dynamics model ht+1=RNN(ht,zt,at)h_{t+1} = \text{RNN}(h_t, z_t, a_t) whose output is a distribution over zt+1z_{t+1}.
  3. A controller at=C(zt,ht)a_t = C(z_t, h_t) that uses the latent code and memory to choose actions.

The encoder is a variational autoencoder. It maps each frame to a latent vector and is trained to reconstruct the frame while keeping the latent code close to a standard Gaussian. The loss has the usual reconstruction term plus a Kullback–Leibler Divergence penalty, as described in Variational Autoencoder.

The mixture-density dynamics

A single Gaussian is too smooth for events that have several outcomes, such as a monster firing or not firing. The dynamics model therefore outputs a mixture of KK Gaussians:

p(zt+1∣zt,at,ht)=∑k=1Kπk N ⁣(zt+1∣μk,σk2),∑k=1Kπk=1,p(z_{t+1} \mid z_t, a_t, h_t) = \sum_{k=1}^{K} \pi_k \, \mathcal{N}\!\left(z_{t+1} \mid \mu_k, \sigma_k^2\right), \qquad \sum_{k=1}^{K} \pi_k = 1,

where πk\pi_k, μk\mu_k and σk\sigma_k are produced by the recurrent network. The mixture weights πk\pi_k say how likely each mode is, and each mode μk\mu_k is one possible next latent. Training maximises the log-likelihood of the observed next latent codes in recorded episodes.

Sampling with a temperature

During dreaming the model samples zt+1z_{t+1} from the mixture. The paper adds a temperature τ\tau to the sampling. Larger τ\tau makes the dream more random, and τ\tau near zero makes the dream almost deterministic. The paper uses this as a knob: a more random dream is harder to exploit.

The controller

The controller is a linear map at=Wc[zt;ht]+bca_t = W_c [z_t; h_t] + b_c. In the racing experiment it has 867 parameters. Because it has so few parameters, the paper trains it with CMA-ES, an evolution strategy that searches over parameters using only episode returns, with no gradient through the world model (see Evolution Strategies).

Worked examples

Example
A three-step dream on a toy line

The real world here is a point moving on a line with the rule zt+1=0.9 zt+atz_{t+1} = 0.9\, z_t + a_t. The model has learned the factor 0.90.9 exactly, and we ignore noise. Start at z0=2z_0 = 2 and take zero action.

  1. Step 1: z1=0.9×2=1.8z_1 = 0.9 \times 2 = 1.8.
  2. Step 2: z2=0.9×1.8=1.62z_2 = 0.9 \times 1.8 = 1.62.
  3. Step 3: z3=0.9×1.62=1.458z_3 = 0.9 \times 1.62 = 1.458.

Now suppose the model learned 0.850.85 instead. Its dream gives 1.71.7, 1.4451.445, 1.2281.228, while the real world gives 1.81.8, 1.621.62, 1.4581.458. The error after three steps is 1.458−1.228=0.2301.458 - 1.228 = 0.230, about 16 percent of the true value.

Interpretation: a small error in the learned rule compounds across steps. This is the core risk of dreaming. A policy can look good in a model that is slightly wrong, which is why the paper checks dream-trained agents in the real environment.

Example
Numbers from the car racing experiment

The paper reports a score of 906±21906 \pm 21 for the full world model on CarRacing-v0, over 100 random trials. The environment counts a score of 900 averaged over 100 consecutive trials as solving it. The encoder-only baseline (V model with a controller and no memory) scores 632±251632 \pm 251.

  1. Difference: 906−632=274906 - 632 = 274 points.
  2. Relative gain over the baseline: 274/632≈0.434274 / 632 \approx 0.434, about 43 percent.
  3. Spread: the full model's ±21\pm 21 is much tighter than the baseline's ±251\pm 251, so the gain is also more consistent across trials.

Interpretation: the memory from the recurrent model is what separates the two. The encoder sees only the current frame, while the full agent also sees the memory hth_t, which carries information about motion and the track ahead. The paper also reports that adding a hidden layer to the controller reaches 788 points, which the authors judged not quite enough to solve the environment.

Example
Compression in the encoder

A 64 by 64 RGB frame has 64×64×3=12,28864 \times 64 \times 3 = 12{,}288 numbers. The paper's latent vector has 64 dimensions for the Doom experiment.

  1. Ratio: 12,288/64=19212{,}288 / 64 = 192.
  2. So the encoder keeps about one number in 192 of the raw pixel data.

Interpretation: the dynamics model works on 64 numbers per step rather than 12,288. The paper trains the encoder on 10,000 random rollouts of the racing game, which is a small enough dataset for the model to be cheap to fit. The price is that the encoder decides what is kept. Details that the encoder does not learn to keep are invisible to the agent, a limit the paper names directly.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Model slightly wrong, policy trained only in dreamsThe policy exploits errors in the model and fails in realityLower the sampling temperature or mix in real data; check in the real environment
Model too noisy (high temperature)The dream is too hard to learn anything fromTune the temperature to a middle value: in the paper's Doom sweep, policies trained at τ=0.1\tau = 0.1 or 0.50.5 scored about 193-196 in the real game, while τ=1.15\tau = 1.15 gave about 1092
Rare events not in the dataThe model never predicts them, so the agent is unpreparedCollect data that covers them; use exploration
Encoder trained without the taskLatent misses task-relevant details, such as small objectsTrain the encoder jointly or add task-specific losses
Long horizon with a deterministic modelCompounding error drifts far from realityShort rollouts, or a model that predicts distributions

The paper also notes a limit of the design: the controller has access to the memory state, so it can learn to exploit whatever the dynamics model gets wrong. Sampling with a temperature is the main defence the paper uses.

History

The idea of a model used for planning is older than deep learning. Sutton's Dyna architecture from 1990 combined learning a model from experience with planning in that model, and the 2018 paper says it distills a line of work from 1990 to 2015 by Schmidhuber on combining recurrent-network world models with controllers. The 2018 paper by David Ha and Jürgen Schmidhuber brought the idea back with modern tools: a variational autoencoder for perception, an LSTM with a mixture-density output for prediction, and evolution strategies for a tiny controller. Its VizDoom experiment trained a policy entirely inside the learned model and then ran it in the real game. Later, Dreamer learned its policy by gradient methods inside imagined latent rollouts.

Check yourself

QuestionIn one sentence, what does a world model predict, and what is it used for?Show answer

A world model predicts how the environment responds to an action, usually the next observation or its compressed latent code. The agent uses it to imagine rollouts, so it can plan or train a policy without many real interactions.

QuestionA toy world has the rule zt+1=0.9 ztz_{t+1} = 0.9\, z_t. Starting from z0=10z_0 = 10, what does the true world give at step 2, and what does a model that learned 0.80.8 give? What is the difference?Show answer

The true world gives z1=9z_1 = 9 and z2=8.1z_2 = 8.1. The model gives z1=8z_1 = 8 and z2=6.4z_2 = 6.4. The difference at step 2 is 8.1−6.4=1.78.1 - 6.4 = 1.7.

QuestionWhy does the paper use a mixture of Gaussians rather than a single Gaussian for the next latent code?Show answer

Environments often have discrete outcomes, such as an enemy either firing or not. A single Gaussian would average the outcomes into a blurred middle that corresponds to nothing that can happen. A mixture gives one mode per outcome, and the weights say how likely each one is, so sampled dreams contain plausible events.

QuestionYou train a driving policy only inside a learned world model and it scores well in the model but poorly on the road. What is the most likely cause, and what would you change first?Show answer

The most likely cause is that the policy found errors in the model and exploits them, a compounding-error or exploitation problem. The first change is to check the model's accuracy on held-out real rollouts, then reduce how far the policy can exploit it, for example by shortening imagined rollouts or by adding real driving data. Raising the sampling temperature can also make the model harder to exploit, but it must be tuned so that learning still works.

Sources

  • David Ha and Jürgen Schmidhuber, "World Models," 2018. arXiv:1803.10122
  • Diederik P. Kingma and Max Welling, "Auto-Encoding Variational Bayes," 2013. arXiv:1312.6114
  • Danijar Hafner et al., "Dream to Control: Learning Behaviors by Latent Imagination," 2019. arXiv:1912.01603