# danijar/dreamer architecture

> Architecture diagram of the GitHub repository danijar/dreamer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/danijar/dreamer
- Repository: https://github.com/danijar/dreamer

## Overview

#### 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`](https://github.com/danijar/dreamer/blob/master/dreamer.py) (Dreamer Training Loop), [`models.py`](https://github.com/danijar/dreamer/blob/master/models.py) (Latent World Model), [`plotting.py`](https://github.com/danijar/dreamer/blob/master/plotting.py) (Offline Plotting), [`wrappers.py`](https://github.com/danijar/dreamer/blob/master/wrappers.py) (Environment Wrappers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["Training Runtime"]
  node_wrappers["Environment Wrappers<br/>environment adapter<br/>[wrappers.py]"]
  node_dreamer["Dreamer Training Loop<br/>orchestrator<br/>[dreamer.py]"]
  node_tools["Training Utilities<br/>shared infrastructure<br/>[tools.py]"]
  node_experience["Experience Data<br/>trajectory stream"]
end

subgraph group_learning["Dreamer Learning"]
  node_world_model["Latent World Model<br/>learned dynamics<br/>[models.py]"]
  node_latent_state["Latent Features<br/>latent state"]
  node_prediction_heads["Prediction Heads<br/>model heads<br/>[models.py]"]
  node_imagined_rollouts["Imagined Rollouts<br/>latent trajectories"]
  node_policy["Policy<br/>behavior model<br/>[dreamer.py]"]
  node_value["Value Function<br/>behavior model<br/>[dreamer.py]"]
end

subgraph group_analysis["Observability &amp; Analysis"]
  node_log_dir[("Experiment Log Directory<br/>training artifacts")]
  node_tensorboard{{"TensorBoard<br/>observability UI"}}
  node_plotting["Offline Plotting<br/>result analysis<br/>[plotting.py]"]
  node_result_plots["Aggregate Plots<br/>analysis artifacts"]
  node_dreamer_scores["Dreamer Scores<br/>reference data<br/>[dreamer.json]"]
  node_baseline_scores["Baseline Scores<br/>reference data<br/>[baselines.json]"]
end

node_dm_control{{"DeepMind Control Suite<br/>external simulator"}}

node_dm_control -->|"task API"| node_wrappers
node_wrappers -->|"normalized environment"| node_dreamer
node_dreamer -->|"collects"| node_experience
node_experience -->|"observation-action history"| node_world_model
node_tools -->|"training support"| node_dreamer
node_dreamer -->|"trains"| node_world_model
node_world_model -->|"encodes"| node_latent_state
node_world_model -->|"drives"| node_prediction_heads
node_prediction_heads -->|"prediction losses"| node_world_model
node_world_model -->|"predicts futures"| node_imagined_rollouts
node_latent_state -->|"current features"| node_policy
node_imagined_rollouts -->|"optimizes on"| node_policy
node_imagined_rollouts -->|"trains on returns"| node_value
node_policy -->|"interaction actions"| node_dreamer
node_value -.->|"value gradients"| node_policy
node_dreamer -->|"writes checkpoints and logs"| node_log_dir
node_log_dir -->|"scalar and media artifacts"| node_tensorboard
node_log_dir -->|"experiment logs"| node_plotting
node_dreamer_scores -->|"reference scores"| node_plotting
node_baseline_scores -->|"baseline scores"| node_plotting
node_plotting -->|"generates"| node_result_plots

click node_wrappers "https://github.com/danijar/dreamer/blob/master/wrappers.py"
click node_dreamer "https://github.com/danijar/dreamer/blob/master/dreamer.py"
click node_tools "https://github.com/danijar/dreamer/blob/master/tools.py"
click node_world_model "https://github.com/danijar/dreamer/blob/master/models.py"
click node_prediction_heads "https://github.com/danijar/dreamer/blob/master/models.py"
click node_policy "https://github.com/danijar/dreamer/blob/master/dreamer.py"
click node_value "https://github.com/danijar/dreamer/blob/master/dreamer.py"
click node_plotting "https://github.com/danijar/dreamer/blob/master/plotting.py"
click node_dreamer_scores "https://github.com/danijar/dreamer/blob/master/scores/dreamer.json"
click node_baseline_scores "https://github.com/danijar/dreamer/blob/master/scores/baselines.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_wrappers,node_dreamer,node_tools,node_experience toneBlue
class node_world_model,node_latent_state,node_prediction_heads,node_imagined_rollouts,node_policy,node_value toneAmber
class node_log_dir,node_tensorboard,node_plotting,node_result_plots,node_dreamer_scores,node_baseline_scores toneMint
class node_dm_control toneNeutral
```

## Components

### Training Runtime

- [Environment Wrappers](https://github.com/danijar/dreamer/blob/master/wrappers.py): environment adapter · Normalizes environment APIs, observations, actions, and episodes. · `wrappers.py`
- [Dreamer Training Loop](https://github.com/danijar/dreamer/blob/master/dreamer.py): orchestrator · Collects experience and coordinates world-model and behavior updates. · `dreamer.py`
- [Training Utilities](https://github.com/danijar/dreamer/blob/master/tools.py): shared infrastructure · Reusable scheduling, logging, data, and TensorFlow support. · `tools.py`
- **Experience Data**: trajectory stream · Online observations, actions, and episode outcomes.

### Dreamer Learning

- [Latent World Model](https://github.com/danijar/dreamer/blob/master/models.py): learned dynamics · Encodes history, predicts latent futures, and supplies learning losses. · `models.py`
- **Latent Features**: latent state · Compact state representation for prediction and control.
- [Prediction Heads](https://github.com/danijar/dreamer/blob/master/models.py): model heads · Produce reconstruction and prediction objectives. · `models.py`
- **Imagined Rollouts**: latent trajectories · Multi-step futures generated by learned dynamics.
- [Policy](https://github.com/danijar/dreamer/blob/master/dreamer.py): behavior model · Selects actions from latent features. · `dreamer.py`
- [Value Function](https://github.com/danijar/dreamer/blob/master/dreamer.py): behavior model · Estimates long-horizon imagined returns. · `dreamer.py`

### Observability & Analysis

- **Experiment Log Directory**: training artifacts · Stores checkpoints, scalar logs, and GIF-related artifacts.
- **TensorBoard**: observability UI · Consumes training artifacts for scalar and media inspection.
- [Offline Plotting](https://github.com/danijar/dreamer/blob/master/plotting.py): result analysis · Aggregates experiment logs into result plots. · `plotting.py`
- **Aggregate Plots**: analysis artifacts
- [Dreamer Scores](https://github.com/danijar/dreamer/blob/master/scores/dreamer.json): reference data · `scores/dreamer.json`
- [Baseline Scores](https://github.com/danijar/dreamer/blob/master/scores/baselines.json): reference data · `scores/baselines.json`

### Other components

- **DeepMind Control Suite**: external simulator · External task and episode source.

## Connections

- 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
