A PyTorch implementation of the Dreamer v1 algorithm for model-based reinforcement learning in visual environments using Gymnasium. This project was associated with ME_395: ML for Mechanical Sciences at Northwestern University.
This project implements Dreamer v1, which learns to solve RL tasks by:
- Learning a world model (RSSM - Recurrent State Space Model) that predicts future observations and rewards from actions
- Planning in imagination by rolling out the learned world model without environment interaction
- Training an actor-critic policy using imagined trajectories to maximize long-horizon returns
The implementation supports both image-based and feature-augmented observations, with configurable architectures and hyperparameters via YAML config files.
├── dreamer/ # Main package
│ ├── __init__.py
│ ├── dreamer.py # Dreamer trainer (main Dreamer algorithm)
│ ├── trainer.py # Base RSSM trainer (independent world model learning)
│ ├── model_utils.py # Replay buffer, evaluation, plotting utilities
│ ├── runtime_utils.py # Environment setup, model building, config handling
│ ├── model/
│ │ ├── rssm.py # RSSM architecture (encoder/decoder + dynamics)
│ │ ├── encoder.py # Convolutional image encoder
│ │ ├── decoder.py # Convolutional image decoder
│ │ ├── loss.py # RSSM loss (reconstruction + KLD)
│ │ └── __init__.py
│ └── __init__.py
├── config/ # Configuration files
│ ├── train_rssm.yaml # Config for world model pre-training
│ └── train_dreamer.yaml # Config for full Dreamer training
├── train_rssm.py # Entry point: train world model only
├── train_dreamer.py # Entry point: train full Dreamer agent
├── runs/ # Training outputs (checkpoints, plots, videos)
└── README.md
-
Create a virtual environment:
python -m venv .venv source .venv/bin/activate -
Install Python dependencies:
pip install swig pip install -r requirements.txt
Pre-train the world model to accurately predict observations and rewards:
python train_rssm.py --config config/train_rssm.yamlTrain the complete Dreamer agent (world model + actor + value network):
python train_dreamer.py --config config/train_dreamer.yamlModify config/train_dreamer.yaml to change:
- Environment: environment name and settings
- Model architecture: RSSM settings, encoder/decoder sizes, latent dimensions, prediction horizon
- Training: learning rates, batch size, number of epochs, buffer capacity
- Loss weights: reconstruction multiplier, KLD weight, reward loss weight
- Dreamer hyperparameters: imagination horizon, discount factor, actor/value learning rates
Training outputs are saved to runs/{env_name}/{timestamp}/:
config.yaml- Training configuration usedcheckpoints/- Model checkpoints saved periodicallyplots/- Loss curves and episode return plotsvideos/- Evaluation episode videos (ground truth vs. predicted observations)
- Encoder: ConvNet that compresses observations into latent embeddings
- Prior: RNN that predicts next latent state from current state + action
- Posterior: Refines latent state using actual next observation
- Decoder: Reconstructs observations from latent states
- Reward head: Predicts rewards from latent states
- Actor: Learns to propose actions that maximize imagined returns
- Value network: Estimates long-horizon return from latent states
- Imagination loop: Rolls RSSM forward using actor's actions (no environment needed)
- Lambda returns: Combines N-step returns with value bootstrapping
- Collect episodes using current policy in environment
- World model update: Minimize reconstruction and KLD losses
- Imagination: Roll RSSM forward from posterior latent states
- Actor update: Maximize expected return + entropy in imagined trajectories
- Value update: Minimize MSE loss on imagined returns
Python 3.8+
See requirements.txt for package dependencies.
LLM-based tools were used to assist in the production of this code.
@misc{hafner2020dreamcontrollearningbehaviors,
title={Dream to Control: Learning Behaviors by Latent Imagination},
author={Danijar Hafner and Timothy Lillicrap and Jimmy Ba and Mohammad Norouzi},
year={2020},
eprint={1912.01603},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/1912.01603},
}