Search

Search papers and lessons

World Models

David Ha, Jürgen Schmidhuber · 2018

World Models compresses frames with a VAE, predicts the next latent with an RNN, and trains a tiny linear controller inside those predictions, solving CarRacing with a 867-parameter policy.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Neural network basics
  • Reinforcement learning
  • Inference
  • Linear algebra
  • Sign in to see your progress

TL;DR

World Models is an agent with three parts: a vision model that compresses each frame into a small latent vector, a recurrent memory model that predicts the next latent, and a small controller that picks actions from both. The first two are trained without reward signals, so most of the agent's complexity lives in the world model. The controller is so small that an evolution strategy can train it.

Where you'll see it: model-based RL in pixel environments, where a learned latent model is used to plan or to generate practice episodes.

The problem

Model-free reinforcement learning methods of the time used small networks, with roughly 10310^3 to 10610^6 parameters according to the paper, because the credit assignment problem makes it hard to learn the millions of weights of a large model from reward alone. The authors want to train large recurrent models efficiently, and they note that backpropagation can train large networks well when the loss is differentiable.

The paper argues that the agent should learn a predictive model of its world from observations, without rewards, using ordinary gradient descent. Only a small policy should then be trained against reward, which can be done with an optimizer that does not need gradients through the whole environment.

Key idea

Divide the agent into a large world model and a small controller. The world model learns to compress what the agent sees and to predict what comes next, given the agent's action. The controller maps the compressed present and the model's memory to an action, and it is trained only on the reward.

The paper offers a human analogy. A batter hitting a fastball does not simulate the pitch step by step before swinging. Their trained prediction of the ball's path drives a fast reflex. The controller is meant to be that reflex, and the world model is the learned prediction underneath it.

The method

The agent has three components, called V (vision), M (memory) and C (controller).

V: a VAE for each frame

The vision model is a variational autoencoder (VAE). It compresses each image frame into a low-dimensional latent vector zz, and it is trained to reconstruct the frame from zz. The car-racing agent uses z∈R32z \in \mathbb{R}^{32}, and the VizDoom agent uses z∈R64z \in \mathbb{R}^{64}. The VAE uses a Gaussian latent, so its encodings carry a measure of uncertainty.

M: an MDN-RNN for the future

The memory model is a recurrent network with a mixture density output. It models the distribution of the next latent given the current latent, the action and its hidden state:

P(zt+1∣at,zt,ht)P(z_{t+1} \mid a_t, z_t, h_t)

The output is a mixture of Gaussians rather than a single prediction, which lets it represent several possible futures. The paper uses a temperature parameter τ\tau when sampling from the mixture. Raising τ\tau makes the sampled futures more random, and it is later used to control the difficulty of the simulated environment. The model's hidden state hth_t summarizes the past, and in the car-racing experiment it carries the information the controller uses to anticipate upcoming turns.

For the VizDoom experiment the same RNN also predicts a binary flag dt+1d_{t+1} for whether the agent dies on the next step. With this flag, the model can stand in for a full environment.

C: a linear controller

The controller is a single linear map from the latent and the hidden state to an action:

at=Wc [zt  ht]+bca_t = W_c\,[z_t\;h_t] + b_c

The paper keeps C deliberately small, so that most of the agent's complexity lives in V and M. The car-racing controller has 867 parameters, and the VizDoom controller has 1,088. Because C is so small, the paper optimizes it with CMA-ES, an evolution strategy, which needs only the final cumulative reward of each rollout rather than gradients through the environment. The rollouts can run in parallel on CPU cores.

Training procedure

The agent is trained in three stages.

  1. Collect 10,000 rollouts from a random policy.
  2. Train V on the frames, then use V to encode every frame and train M to predict the sequence of latents.
  3. Optimize C with CMA-ES to maximize the expected cumulative reward, using rollouts of the agent in the environment (for car racing) or in the model's own hallucinated environment (for VizDoom).

Worked examples

Example 1: size of the controller (from the paper's parameter tables). In the car-racing agent, the controller has 867 parameters, against 4,348,547 for the VAE and 422,368 for the MDN-RNN.

  1. Total parameters of V and M: 4,348,547+422,368=4,770,9154{,}348{,}547 + 422{,}368 = 4{,}770{,}915.
  2. Controller fraction: 867/(4,770,915+867)≈0.018%867 / (4{,}770{,}915 + 867) \approx 0.018\%.

Interpretation: almost all parameters are in the unsupervised world model, and the reward-trained part is tiny. This is why CMA-ES, which scales poorly to large parameter counts, is a practical choice for C.

Example 2: a hand computation of a mixture density output (illustrative). Suppose the MDN outputs two Gaussians with weights π1=0.7\pi_1 = 0.7 and π2=0.3\pi_2 = 0.3, means μ1=0\mu_1 = 0 and μ2=2\mu_2 = 2, and both standard deviations are 1. The predicted density at z=0z = 0 is

  1. Component 1: π1⋅N(0∣0,1)=0.7×0.3989=0.2792\pi_1 \cdot \mathcal{N}(0 \mid 0, 1) = 0.7 \times 0.3989 = 0.2792.
  2. Component 2: π2⋅N(0∣2,1)=0.3×0.0540=0.0162\pi_2 \cdot \mathcal{N}(0 \mid 2, 1) = 0.3 \times 0.0540 = 0.0162.
  3. Sum: 0.29540.2954.

Interpretation: a mixture can put probability on distinct outcomes, such as a fireball being shot or not, which a single Gaussian cannot do.

Example 3: temperature (values from the paper's Table 2). At τ=0.10\tau = 0.10 the virtual environment score is 2086 but the actual score is 193. At τ=1.15\tau = 1.15 the virtual score falls to 918 and the actual score rises to 1092. The paper's reading is that a sharper model, at low temperature, lets the controller exploit the model's errors, and a noisier model keeps the controller from doing so.

Results

Every number below comes from the paper's text or tables.

Car racing (CarRacing-v0). The task counts as solved at an average reward of 900 over 100 consecutive trials. The results in Table 1 are:

MethodAverage score
DQN343 ± 18
A3C (continuous)591 ± 45
A3C (discrete)652 ± 10
Gym leaderboard entry838 ± 11
V model only (controller sees only zz)632 ± 251
V model with a hidden layer788 ± 141
Full world model (V, M and C)906 ± 21

The full agent scores 906 ± 21 over 100 random trials. The paper reports this as solving the task and as a new state of the art over the methods listed. Giving the controller the hidden state hth_t as well as ztz_t makes the driving stable, and the paper argues the agent does not need to plan ahead, since hth_t already encodes the predicted future.

VizDoom "Take Cover". The agent avoids fireballs shot by monsters. The cumulative reward is the number of survival steps, with a maximum of 2100 steps, and the task counts as solved at an average of more than 750 steps over 100 rollouts. The controller learns inside the model's dream, scoring about 900 steps there. When the policy is transferred to the real environment, it scores about 1100 steps over 100 trials, which the paper reports as beyond the solved threshold.

Temperature and exploitation (Table 2). Training in the dream at several temperatures gives:

Temperature τ\tauVirtual scoreActual score
0.102086 ± 140193 ± 58
0.502060 ± 277196 ± 50
1.001145 ± 690868 ± 511
1.15918 ± 5461092 ± 556
1.30732 ± 269753 ± 139

Random policy: 210 ± 108 on the actual environment. Gym leader: 820 ± 58. The paper reports that at τ=0.1\tau = 0.1 the dream's monsters stop shooting, which lets the controller reach a near-perfect dream score that does not transfer. Higher temperature makes the dream harder and reduces this exploitation, but too much makes learning fail, so τ\tau is a hyperparameter to tune.

Limitations the paper reports. The VAE reproduces unimportant detail, such as wall brick patterns in Doom, but misses task-relevant road tiles in Car Racing, because it is trained without the task. The world model has limited capacity, and an LSTM may not hold all recorded experience. The controller can exploit errors in the model, and the paper notes that a deterministic model is especially easy to exploit.

Why it mattered

The paper shows that a large unsupervised predictive model can be trained from pixels, and that a small controller trained by evolution on top of its features can solve two pixel tasks. It shows that a policy trained only inside the learned model can transfer to the real environment in the VizDoom experiment. It also describes the failure mode of exploitation and the use of temperature as a control.

The paper's discussion gives its own research directions: iterative training that gathers new data to improve the model, and curiosity-driven exploration by flipping the sign of the model's loss in the real environment.

Reading guide

Read the paper in this order and use these lessons for the sections that need them.

  1. Introduction (Section 1): World Model for the idea of a predictive model of an environment, and Reinforcement Learning for the agent setting. Model-Based Reinforcement Learning gives the wider context.
  2. Agent model (Section 2): Variational Autoencoder for the V model, Recurrent Neural Network and Long Short-Term Memory for the memory model, and Mixture Density Network for the output layer of M. Gaussian (Normal) Distribution covers the Gaussian mixture components.
  3. Car racing (Section 3): Generative Model for why modeling observations is useful, and Self-Supervised Learning for training V and M without rewards. Evolution Strategies explains how C is optimized.
  4. VizDoom and temperature (Section 4): Softmax Temperature for the temperature parameter used to sample from the mixture, and Policy Gradient for the gradient-based alternative to evolution, which the paper's motivation contrasts with its choice of CMA-ES.
  5. Iterative training and discussion (Sections 5 and 7): no new prerequisites beyond the above.

Check yourself

QuestionWhy does the paper train the VAE and the MDN-RNN without rewards, and only the controller with rewards?Show answer
The world model's task is to compress and predict observations, which needs no reward signal. Training it on observations alone makes it reusable and lets most of the agent's parameters learn from the abundant data. The controller then needs only a few parameters, so it can be optimized from the reward alone with an evolution strategy. Mixing reward into the world model would tie its features to one task and require the much harder credit assignment problem for a large network.
QuestionIn the car-racing agent, the controller has 867 parameters and the VAE has 4,348,547. Using only these two numbers, what fraction of the VAE-plus-controller total does the controller represent?Show answer
The total is 4,348,547+867=4,349,4144{,}348{,}547 + 867 = 4{,}349{,}414. The controller fraction is 867/4,349,414≈0.0199%867 / 4{,}349{,}414 \approx 0.0199\%. This is the share of the VAE and controller only, so it is slightly larger than the share in the full agent that also counts the MDN-RNN, which is about 0.018%0.018\%.
QuestionAt temperature 0.10 the controller scores 2086 in the dream but 193 in the real environment. What explains the gap, and what does the paper suggest?Show answer
The model at low temperature is almost deterministic, so the controller learns a policy that exploits the model's errors, such as monsters that stop shooting fireballs in the dream. That policy does not work in the real environment. The paper suggests raising the temperature to make the dream harder and less exploitable, but too high a temperature makes learning fail, so the value must be tuned.
QuestionWhy does the paper's car-racing controller get the hidden state hth_t as well as ztz_t, and why does this help?Show answer
The latent ztz_t describes only the current frame. The hidden state hth_t of the MDN-RNN summarizes the past and encodes a distribution over the future, so it tells the controller what to expect next. Adding it lets the controller act reflexively on the prediction without planning ahead, which the paper reports makes the driving stable and lifts the score from the VV-only agent's 632 to 906.

Sources

Read the full paper

David Ha, Jürgen Schmidhuber, 2018. arXiv:1803.10122