A PyTorch implementation of the World Models architecture (Ha & Schmidhuber, 2018) applied to the CarRacing-v3 environment from Gymnasium.
The agent learns entirely inside a learned "dream" — it never trains the controller in the real environment. Instead, a VAE compresses visual observations into a compact latent space, an MDN-RNN predicts future latent states, and a lightweight Controller is evolved with CMA-ES using imagined rollouts.
Observation (96×96 RGB)
│
▼
┌───────────┐
│ VAE │ → latent vector z (32-dim)
└───────────┘
│
▼
┌───────────┐
│ MDN-RNN │ → predicts next z + reward
└───────────┘
│
▼
┌────────────┐
│ Controller │ → action (steering, gas, brake)
└────────────┘
│
evolved via CMA-ES on dream rollouts
| File | Description |
|---|---|
VAE.py |
Convolutional VAE — encodes 64×64 frames into a 32-dim latent space |
MDRNN.py |
MDN-RNN — LSTM with a Mixture Density Network head; predicts future latent states and rewards |
controller.py |
Linear controller — maps (z, h) → action |
collect_data.py |
Collects random rollouts from the real environment and saves them as .npz files |
train_vae.py |
Trains the VAE on collected frames |
train_mdrnn.py |
Encodes the dataset with the trained VAE, then trains the MDN-RNN |
cma_es.py |
Optimises the controller weights using CMA-ES over batched dream rollouts |
dream.py |
Runs imagination rollouts (single and batched) entirely in latent space |
pip install torch gymnasium[box2d] numpy tqdm cmaTested with Python 3.10+. On Apple Silicon, MPS acceleration is used automatically.
python collect_data.pySaves random rollout episodes to ./data/ as compressed .npz files (frames, actions, rewards).
python train_vae.pyTrains the visual model on collected frames. Saves checkpoint to checkpoint/.
python train_mdrnn.pyLoads the trained VAE, encodes the dataset into latent sequences, and trains the memory model.
python cma_es.pyUses CMA-ES to optimise controller weights entirely through dream rollouts in latent space.
VAE — 4-layer convolutional encoder/decoder, latent dim = 32, trained with MSE reconstruction loss + β-weighted KL divergence.
MDN-RNN — Single-layer LSTM (hidden dim = 256) feeding into a Mixture Density Network (5 Gaussian mixtures) over the latent space. Also predicts per-step reward via a separate linear head.
Controller — Single linear layer mapping the concatenated latent vector z (32-dim) and LSTM hidden state h (256-dim) directly to a 3-dim continuous action (steering, gas, brake).
- Ha, D., & Schmidhuber, J. (2018). World Models. arXiv:1803.10122
- Gymnasium CarRacing-v3