Skip to content
class

SequenceReplay

SequenceReplay(capacity: int = 1000000)

Whole episodes in, contiguous chunks out.

Parameters

capacityint= 1_000_000
Budget in transitions, not episodes. Frames dominate the memory of a pixel-based agent — at 64x64x3 in float32 a single observation is 48 KiB, so a million of them is not a number to set carelessly — and counting episodes would let the true cost drift with their length. Oldest episodes are dropped first.

Attributes

episodeslist of Episode
Stored trajectories, oldest first.

Notes

Episodes shorter than the requested chunk length are skipped rather than padded. Padding would put frames into the sequence that the dynamics never produced, and the model has no way to tell them from real ones.

Sampling picks an episode uniformly and then a start position within it uniformly, which is what the world-model papers' released code does. Note the consequence: a step in a short episode is more likely to be drawn than a step in a long one. Sampling uniformly over all valid start positions instead would remove that bias, and is a deliberate non-choice here — matching the reference matters more than correcting it.

Examples

>>> import lucid
>>> from lucid.utils.rollout import Episode, SequenceReplay
>>> replay = SequenceReplay()
>>> replay.add(Episode(lucid.zeros((20, 3, 8, 8)), lucid.zeros((20, 2)),
...                    lucid.zeros((20,)), lucid.ones((20,))))
>>> batch = replay.sample(4, 5)
>>> batch.observations.shape, batch.actions.shape
((4, 5, 3, 8, 8), (4, 5, 2))

Used by 1

Constructors

1
dunder

__init__

→None
__init__(capacity: int = 1000000)

Properties

1
prop

steps

→int
steps: int

Total transitions held.

Instance methods

2
fn

add

→None
add(episode: Episode)

Store an episode, evicting the oldest until the budget is met.

Parameters

episodeEpisode
The trajectory to store.

Raises

ValueError
If the episode's fields disagree on length, or it is empty.
fn

sample

→Episode
sample(batch_size: int, length: int)

Draw contiguous chunks.

Parameters

batch_sizeint
Number of chunks.
lengthint
Steps per chunk. Each one is a window into a single episode.

Returns

Episode

Batched, with a leading batch axis — observations is (batch_size, length, C, H, W) and the rest follow. It is the same NamedTuple because the fields mean the same thing; only the rank differs.

Raises

ValueError
If no stored episode is at least length steps long.

Dunder methods

1
dunder

__len__

→int
__len__()

Number of stored episodes.