How danijar/dreamer works
Purpose and entry points
This repository is a TensorFlow 2 implementation of Dreamer: a reinforcement-learning agent that learns a latent world model from environment interaction, then trains policy and value functions on imagined trajectories.
• Training/runtime entry point: dreamer.py
• Offline plotting entry point: plotting.py
• Core model definitions: models.py
Training orchestration
dreamer.py is the primary control plane. It accepts a task and log directory (for example, DM Control dmc_walker_walk), runs environment interaction, collects experience, coordinates model/behavior updates, and writes training artifacts for TensorBoard and later plotting.
Key boundary:
• Online environment data enters the learner through environment wrappers.
• Learner outputs include checkpoints/logs under the caller-provided log directory.
Paths: dreamer.py, wrappers.py, tools.py
Environment integration and experience boundary
Environment adaptation is isolated from the agent implementation. wrappers.py likely normalizes task APIs, observations, action handling, episode lifecycle, and/or metric reporting so Dreamer can operate against DeepMind Control tasks without embedding task-specific behavior in the model code.
This is the main external-system boundary:
• External simulator: DeepMind Control Suite (dm_control)
• Internal training loop and agent: dreamer.py
Paths: wrappers.py, dreamer.py
Latent world model
models.py defines the architecture-defining learned components: Dreamer’s compact latent dynamics model and associated prediction heads. The world model converts observation/action history into latent features and predicts future latent states; it is the differentiable substrate used for both reconstruction/prediction learning and imagined rollouts.
Primary flow:
1. Observations and actions from wrapped environments are encoded into latent state.
2. Latent dynamics predict future features.
3. Model predictions provide training losses and imagined inputs to behavior learning.
Paths: models.py, dreamer.py
Imagination-based behavior learning
The agent uses the learned dynamics rather than requiring all policy optimization to occur in the real simulator. dreamer.py drives imagined multi-step feature sequences from the world model; policy and value learning consume those sequences. Value gradients can propagate through predicted dynamics, supporting long-horizon control.
This separates:
• Real interaction for data acquisition.
• Latent imagination for policy/value optimization.
Paths: dreamer.py, models.py
Shared training utilities and observability
tools.py provides shared infrastructure used by the training pipeline, such as reusable training, scheduling, logging, data, or TensorFlow support mechanisms. Training output is intended for TensorBoard, while plotting.py reads experiment log directories and generates aggregate result plots.
Paths: tools.py, dreamer.py, plotting.py
Runtimes, dependencies, and result artifacts
Material runtime is Python with TensorFlow GPU 2.2.0 and TensorFlow Probability. The supported documented environment dependency is dm_control; analysis uses pandas and matplotlib. TensorBoard consumes the generated log directory for scalar graphs and GIF-related artifacts.
Reference score data is stored separately from the training code:
• scores/dreamer.json
• scores/baselines.json
Main entry points: dreamer.py Dreamer Training Loop · models.py Latent World Model · plotting.py Offline Plotting · wrappers.py Environment Wrappers
Components 17
Training Runtime
- Environment Wrappers · Normalizes environment APIs, observations, actions, and episodes.
wrappers.py - Dreamer Training Loop · Collects experience and coordinates world-model and behavior updates.
dreamer.py - Training Utilities · Reusable scheduling, logging, data, and TensorFlow support.
tools.py - Experience Data · Online observations, actions, and episode outcomes.
Dreamer Learning
- Latent World Model · Encodes history, predicts latent futures, and supplies learning losses.
models.py - Latent Features · Compact state representation for prediction and control.
- Prediction Heads · Produce reconstruction and prediction objectives.
models.py - Imagined Rollouts · Multi-step futures generated by learned dynamics.
- Policy · Selects actions from latent features.
dreamer.py - Value Function · Estimates long-horizon imagined returns.
dreamer.py
Observability & Analysis
- Experiment Log Directory · Stores checkpoints, scalar logs, and GIF-related artifacts.
- TensorBoard · Consumes training artifacts for scalar and media inspection.
- Offline Plotting · Aggregates experiment logs into result plots.
plotting.py - Aggregate Plots
- Dreamer Scores
scores/dreamer.json - Baseline Scores
scores/baselines.json
Other components
- DeepMind Control Suite · External task and episode source.
Connections 21
- DeepMind Control Suite → Environment Wrappers: task API
- Environment Wrappers → Dreamer Training Loop: normalized environment
- Dreamer Training Loop → Experience Data: collects
- Experience Data → Latent World Model: observation-action history
- Training Utilities → Dreamer Training Loop: training support
- Dreamer Training Loop → Latent World Model: trains
- Latent World Model → Latent Features: encodes
- Latent World Model → Prediction Heads: drives
- Prediction Heads → Latent World Model: prediction losses
- Latent World Model → Imagined Rollouts: predicts futures
- Latent Features → Policy: current features
- Imagined Rollouts → Policy: optimizes on
- Imagined Rollouts → Value Function: trains on returns
- Policy → Dreamer Training Loop: interaction actions
- Value Function → Policy: value gradients
- Dreamer Training Loop → Experiment Log Directory: writes checkpoints and logs
- Experiment Log Directory → TensorBoard: scalar and media artifacts
- Experiment Log Directory → Offline Plotting: experiment logs
- Dreamer Scores → Offline Plotting: reference scores
- Baseline Scores → Offline Plotting: baseline scores
- Offline Plotting → Aggregate Plots: generates