DreamerV3
INFO
Esta página ainda não foi traduzida. Leia em inglês
Hafner et al., Mastering Diverse Domains through World Models, 2023. Paper · Code · Site · Notebook
DreamerV3's world model is a recurrent state-space model (RSSM). The model state stoch categoricals with classes classes each:
The posterior
with
Actor-critic in imagination. From every posterior state of the batch, the actor and the prior imagine horizon = 15 steps. The critic regresses the λ-returns
Usage
from torch_world.models import DreamerV3
from torch_world.planning import CEM
model = DreamerV3(action_dim=2) # 64x64 RGB; small defaults, see below for the paper's sizes
opt = torch.optim.Adam(model.param_groups()) # world model 1e-4, actor and critic 3e-5
out = model(obs, actions) # reward-free: (B, T + 1, 3, 64, 64), (B, T, A)
# out: {"loss", "image_loss", "dyn_loss", "rep_loss", "kl"}
out = model(obs, actions, rewards) # + rewards (B, T) [and terminated (B, T)]
# out adds: "reward_loss", "continue_loss", "critic_loss", "actor_loss", "entropy"
opt.zero_grad()
out["loss"].backward()
model.clip_grad_norm() # 1000 for the world model, 100 for the actor and the critic
opt.step()
model.update_target() # EMA of the critic, decay 0.98
z = model.encode(obs) # filtered model states (B, T + 1, deter_dim + stoch * classes)
frames = model.decode(model.rollout(z[:, :1], plan)) # imagined frames (B, H + 1, 3, 64, 64)
plan = CEM(horizon=8, action_dim=2)(model, model.encode(obs[:, :1]), goal_state) # plan on the model state
action = model.act(state) # the actor's mean action; state = model.step(state, action, next_obs)forward sums the world-model, critic and actor losses. Their gradients reach disjoint parameters: imagination runs through the world model with detached weights, so the actor's gradient flows through the dynamics into the actions but never updates the model, and the critic trains on detached states. One backward pass thus matches the reference's three optimizers. Without rewards, only the world model trains.
encode, predict and rollout work on the flattened model state (..., deter_dim + stoch * classes), history = 1. encode filters a clip from the learned initial state; a single frame gives the state after one posterior step. By default the stochastic part is the mode of each categorical, so encodings and rollouts are deterministic; sample=True draws it instead, as in training. step(state, action, obs) advances the filter online, for running the actor in an environment.
Choices and deviations
- Version. The hyperparameters are those of the paper's first version (Table W.1), as in the 2023 reference code:
, horizon 15, actor gradients backpropagated through the dynamics for continuous actions ( actor_grad="dynamics"). The 2024 revision uses, horizon 16, Reinforce for every action space ( actor_grad="reinforce",dyn_coef=1.0,horizon=16) and adds a replay-buffer value loss (scale 0.3), not included here. - Actor. Continuous actions only: a Gaussian with mean
and std in , as in the reference. The baseline is subtracted as a constant. actclips to. - Networks. LayerNorm and SiLU throughout; the GRU is the reference's single LayerNorm-ed linear layer with an update-gate bias of
. The encoder is stride-2 convolutions of kernel 4 with doubling channels down to 4×4, the decoder its transpose; images are centred by and reconstructed with a squared error summed over pixels. The initial state is learned, and the first step of a clip takes a zero action from it; actions entering the RSSM are scaled to at most 1 in magnitude, and imagined continue flags are the continue head's mode, all as in the reference. - Sizes. Defaults are small, for notebooks:
deter_dim=256,stoch=32,classes=16,hidden_dim=256,mlp_layers=2,cnn_depth=16. The paper's presets (first version, Table B.1), all with 32 categoricals of 32 classes:
| preset | GRU units (deter_dim) | CNN multiplier (cnn_depth) | dense units (hidden_dim) | MLP layers | parameters |
|---|---|---|---|---|---|
| XS | 256 | 24 | 256 | 1 | 8M |
| S | 512 | 32 | 512 | 2 | 18M |
| M | 1024 | 48 | 640 | 3 | 37M |
| L | 2048 | 64 | 768 | 4 | 77M |
| XL | 4096 | 96 | 1024 | 5 | 200M |
The 2024 revision trains six sizes from 12M to 400M parameters.
- Optimizer.
param_groupsgives Adam with the paper's rates and epsilons; the reference's later switch to LaProp with adaptive gradient clipping is not included. - Goal planning. The paper acts with the actor only. Planning with
CEMtoward an encoded goal image is an addition of this library.costis the squared distance between the final imagined model state and the goal's, on the whole state by default (cost_on="full");cost_on="stoch"or"deter"compares only the categorical or only the recurrent part. A goal image encoded on its own gets the recurrent state of a first step, which does not depend on the image, so only the stochastic part locates the goal; still, in a 4-epoch tuning run on Two-Rooms the full state planned as well as or better than the stochastic part (same room 100% vs 94%, other room 25% vs 12%, 32 tasks each), and the recurrent part alone was worst (50%, 25%).
Reference
Arguments and methods: DreamerV3 in the API reference.
