Skip to content
class

DreamerV3Model

extendsPretrainedModel
DreamerV3Model(config: DreamerV3Config)

DreamerV2's architecture with distributional heads and a slow critic.

Parameters

Frozen configuration.

Notes

Reference: Hafner, Pasukonis, Ba, and Norouzi, "Mastering Diverse Domains through World Models", Nature 640 (2025), 647-653 (arXiv:2301.04104).

Two architectural differences from DreamerV2 are visible here.

The reward and value heads are _TwoHotHeads — they predict a distribution over exponentially spaced bins and are read back through symexp, which is what decouples the gradients from the reward's magnitude.

The critic is duplicated, but the copy plays the opposite role to DreamerV2's. There the frozen copy produced the targets; here the returns bootstrap from the live critic and the slow copy appears only as a regulariser pulling the live one toward its own moving average. The copy is therefore updated every step by a small fraction rather than replaced wholesale on a schedule.

Examples

>>> import lucid
>>> from lucid.models import dreamer_v3_12m
>>> model = dreamer_v3_12m(action_dim=2, cnn_depth=2, stoch_size=3, discrete=4,
...     deter_size=8, hidden_size=8, actor_hidden=8, value_hidden=8,
...     reward_hidden=8, num_bins=5)
>>> _, posteriors = model.observe(lucid.randn((1, 3, 3, 64, 64)),
...                               lucid.randn((1, 3, 2)))
>>> posteriors.is_discrete, model.act(posteriors, sample=False).shape
(True, (1, 3, 2))

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: DreamerV3Config)

Instance methods

9
fn

act

→Tensor
act(state: RSSMState, sample: bool = True)

Propose actions — (B, T, action_dim) or (B, action_dim).

Parameters

stateRSSMState
A sequence or a single step; the rank is preserved.
samplebool= True, keyword-only
Draw, or take the policy's mode.

Returns

Tensor

One-hot when discrete. Otherwise the mode lies inside (-1, 1) and a draw is a Gaussian around it that is not clipped, as the released policy's is not — the environment boundary clips it (lucid.utils.rollout.LatentPolicy does).

Examples

>>> import lucid
>>> from lucid.models import dreamer_v3_12m
>>> kwargs = dict(action_dim=2, cnn_depth=2, stoch_size=3, discrete=4,
...               deter_size=8, hidden_size=8, actor_hidden=8,
...               value_hidden=8, reward_hidden=8, num_bins=5)
>>> model = dreamer_v3_12m(**kwargs)
>>> obs, act = lucid.randn((1, 3, 3, 64, 64)), lucid.randn((1, 3, 2))
>>> _, posteriors = model.observe(obs, act)
>>> last = posteriors.map(lambda t: t[:, -1])
>>> model.act(posteriors).shape, model.act(last).shape
((1, 3, 2), (1, 2))
>>> bool((model.act(last, sample=False).abs() < 1).all())  # the mode is squashed
True
A discrete action space picks one alternative instead, as a one-hot:
>>> game = dreamer_v3_12m(action_space="discrete", **kwargs)
>>> _, beliefs = game.observe(obs, act)
>>> choice = game.act(beliefs.map(lambda t: t[:, -1]))
>>> choice.shape, choice.sum(dim=-1).tolist()
((1, 2), [1.0])
fn

decode

→Tensor
decode(state: RSSMState)

Reconstruct frames from a state — (B, T, C, 64, 64).

Notes

Frames are not passed through symlog. The transform is for quantities whose range differs between domains; a pixel is always in the same interval, and the reference applies it to vector observations and to reward and value only.

fn

encode

→Tensor
encode(observations: Tensor)

Embed a frame sequence — (B, T, C, 64, 64) -> (B, T, embed_size).

fn

forward

→DreamerV3Output
forward(observations: Tensor, actions: Tensor)

Override in subclasses to define the computation.

fn

imagine

→RSSMState
imagine(state: RSSMState, horizon: int, sample: bool | None = None)

Roll the dynamics forward under the actor's own policy.

Parameters

stateRSSMState
Flat starting beliefs, (N, ·).
horizonint
Steps to imagine.
sample(bool or None, optional, keyword - only)= None
Draw both latent and action, take their modes, or follow mean_only.

Returns

RSSMState

The imagined states including the start, (N, horizon + 1, ·).

Notes

The state the actor reads is detached. Unlike the earlier families this costs nothing: DreamerV3's actor is trained purely by the score function, so no gradient was ever going to travel back through the dynamics from the return.

Examples

>>> import lucid
>>> from lucid.models import dreamer_v3_12m
>>> model = dreamer_v3_12m(action_dim=2, cnn_depth=2, stoch_size=3,
...     discrete=4, deter_size=8, hidden_size=8, actor_hidden=8,
...     value_hidden=8, reward_hidden=8, num_bins=5)
>>> _, posteriors = model.observe(lucid.randn((1, 3, 3, 64, 64)),
...                               lucid.randn((1, 3, 2)))
Every filtered step becomes an imagination start:
>>> start = posteriors.map(lambda t: t.reshape(3, *t.shape[2:]))
>>> states, actions = model.imagine(start, horizon=5)
>>> states.stoch.shape, states.logits.shape, actions.shape
((3, 6, 12), (3, 6, 3, 4), (3, 5, 2))
Reward along the trajectory is decoded from its bins in reward
units — one per state, the start included:
>>> model.predict_reward(states).shape
(3, 6)
fn

observe

→priors, posteriors : RSSMState
observe(observations: Tensor, actions: Tensor, state: RSSMState | None = None, sample: bool | None = None)

Filter a trajectory into posterior states.

Parameters

observationsTensor
Frames, (B, T, C, 64, 64).
actionsTensor
Actions taken into each step, (B, T, action_dim).
stateRSSMState or None= None
Starting belief; None starts from a uniform prior.
sample(bool or None, optional, keyword - only)= None
Draw the latent, take its mode, or follow mean_only.

Returns

priors, posteriors : RSSMState

(B, T, ·) each, carrying categorical logits already mixed with unimix.

Examples

>>> import lucid
>>> from lucid.models import dreamer_v3_12m
>>> model = dreamer_v3_12m(action_dim=2, cnn_depth=2, stoch_size=3,
...     discrete=4, deter_size=8, hidden_size=8, actor_hidden=8,
...     value_hidden=8, reward_hidden=8, num_bins=5)
>>> obs, act = lucid.randn((1, 3, 3, 64, 64)), lucid.randn((1, 3, 2))
>>> priors, posteriors = model.observe(obs, act)
>>> posteriors.stoch.shape, posteriors.logits.shape
((1, 3, 12), (1, 3, 3, 4))
unimix is already folded in: every class keeps at least
unimix / discrete of the mass, so no outcome is ever ruled out:
>>> floor = model.config.unimix / model.config.discrete
>>> bool((lucid.softmax(posteriors.logits, dim=-1) >= floor).all())
True
fn

predict_pcont

→Tensor
predict_pcont(state: RSSMState)

Predict the discount at a state — logits, (B, T).

Parameters

stateRSSMState
The states to score.

Returns

Tensor

Bernoulli logits; apply sigmoid for the probability.

Raises

ValueError
If the model was configured without pcont.

Examples

>>> import lucid
>>> from lucid.models import dreamer_v3_12m
>>> model = dreamer_v3_12m(action_dim=2, cnn_depth=2, stoch_size=3,
...     discrete=4, deter_size=8, hidden_size=8, actor_hidden=8,
...     value_hidden=8, reward_hidden=8, num_bins=5)
>>> _, posteriors = model.observe(lucid.randn((1, 3, 3, 64, 64)),
...                               lucid.randn((1, 3, 2)))
>>> logits = model.predict_pcont(posteriors)
>>> logits.shape
(1, 3)
The head is on by default in this family; sigmoid gives the
probability of continuing that imagination discounts by:
>>> model.config.pcont
True
>>> keep = lucid.sigmoid(logits)
>>> bool(((keep > 0) & (keep < 1)).all())
True
fn

predict_reward

→Tensor
predict_reward(state: RSSMState)

Predict reward from a state — (B, T), in reward units.

fn

predict_value

→Tensor
predict_value(state: RSSMState, slow: bool = False)

Estimate a state's value — (B, T).

Parameters

stateRSSMState
The states to score.
slowbool= False, keyword-only
Read the slow copy instead of the learning critic. Note this is not where the returns come from — they bootstrap from the learner; the slow copy only regularises it.

Returns

Tensor

Value estimates, in return units.

Examples

>>> import lucid
>>> from lucid.models import dreamer_v3_12m
>>> model = dreamer_v3_12m(action_dim=2, cnn_depth=2, stoch_size=3,
...     discrete=4, deter_size=8, hidden_size=8, actor_hidden=8,
...     value_hidden=8, reward_hidden=8, num_bins=5)
>>> _, posteriors = model.observe(lucid.randn((1, 3, 3, 64, 64)),
...                               lucid.randn((1, 3, 2)))
>>> value = model.predict_value(posteriors)
>>> value.shape
(1, 3)
Both critics are zero-initialised, so an untrained critic asserts no
return for the actor to chase, and the slow copy agrees with it:
>>> slow = model.predict_value(posteriors, slow=True)
>>> float(value.abs().max().item()), float(slow.abs().max().item())
(0.0, 0.0)