xwm.data¶
Batch streams and a synthetic controllable world: a sprite the actions move, plus distractors that move on their own. Enough to train and plan on CPU.
Data: batch streams and a synthetic controllable world.
Modules:
| Name | Description |
|---|---|
batching |
Turning arrays into batch streams. |
synthetic |
A small controllable world, for tests and runnable examples. |
Classes:
| Name | Description |
|---|---|
SpriteState |
World state: a controlled agent plus |
SpriteWorld |
A 2-D world with one action-controlled sprite and some uncontrolled ones. |
Functions:
| Name | Description |
|---|---|
clip_windows |
Cut |
iter_batches |
Iterate mini-batches over a dict of equally-long arrays. |
random_actions |
|
sprite_images |
Generate a still-image dataset: |
sprite_sequences |
Generate an action-labelled video dataset. |
SpriteState
¶
Bases: NamedTuple
World state: a controlled agent plus n_distractors random walkers.
SpriteWorld
¶
SpriteWorld(size: int = 32, *, n_distractors: int = 2, radius: float = 0.1, dt: float = 1.0, damping: float = 0.5, action_scale: float = 0.15, distractor_speed: float = 0.05)
Bases: Module
A 2-D world with one action-controlled sprite and some uncontrolled ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
rendered resolution (square). |
32
|
n_distractors
|
int
|
uncontrollable sprites performing a random walk. |
2
|
radius
|
float
|
sprite radius in normalised units. |
0.1
|
dt
|
float
|
integration step. |
1.0
|
damping
|
float
|
velocity decay per step; |
0.5
|
action_scale
|
float
|
acceleration applied per unit of action. |
0.15
|
distractor_speed
|
float
|
random-walk step size for the distractors. |
0.05
|
The defaults make one step of full action displace the agent by about 1.5 radii. That matters for evaluation: with slower dynamics, a single step barely changes the image, "predict no change" becomes a near-optimal one-step baseline, and a latent dynamics model looks worthless at short horizons for reasons that have nothing to do with the model.
Methods:
| Name | Description |
|---|---|
step |
Advance the world. |
render |
Render one state to |
rollout |
Run |
observe |
Just the frames from |
Source code in xwm/data/synthetic.py
step
¶
step(state: SpriteState, action: Array, *, key: PRNGKey | None = None) -> SpriteState
Advance the world. action is a 2-D acceleration in [-1, 1]^2.
Positions reflect off the walls, which keeps trajectories bounded without the discontinuity of wrapping.
Source code in xwm/data/synthetic.py
render
¶
render(state: SpriteState) -> Array
Render one state to (3, size, size) in [0, 1].
The agent occupies the red channel and the distractors the green one, so a probe can tell trivially whether a representation kept the controllable content, the uncontrollable content, or both.
Source code in xwm/data/synthetic.py
rollout
¶
rollout(key: PRNGKey, actions: Array) -> tuple[Array, SpriteState]
Run (T, 2) actions from a random start.
Returns (frames, states) with frames of shape
(T + 1, 3, size, size) -- one more frame than actions, since the
initial observation precedes the first action.
Source code in xwm/data/synthetic.py
clip_windows
¶
Cut (T, ...) frames into overlapping clips of window frames.
Returns (n_windows, window, ...). Video encoders take fixed-length
clips, so this is how a long sequence is fed to one.
Source code in xwm/data/batching.py
iter_batches
¶
iter_batches(data: dict[str, Array], batch_size: int, *, key: PRNGKey | None = None, shuffle: bool = True, drop_last: bool = True, epochs: int | None = 1) -> Iterator[Batch]
Iterate mini-batches over a dict of equally-long arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Array]
|
field name -> array with a common leading axis. |
required |
epochs
|
int | None
|
passes over the data; |
1
|
Source code in xwm/data/batching.py
random_actions
¶
random_actions(key: PRNGKey, n_sequences: int, length: int, action_dim: int = 2, *, smoothness: float = 0.7) -> Array
(n_sequences, length, action_dim) smoothly correlated random actions.
White-noise actions make an almost unlearnable dataset -- the agent jitters
in place and no action has visible consequences. Temporally correlated
actions (an AR(1) process, smoothness being the correlation) produce
trajectories that actually go somewhere.
Source code in xwm/data/synthetic.py
sprite_images
¶
sprite_images(key: PRNGKey, n_images: int, *, world: SpriteWorld | None = None) -> dict[str, Array]
Generate a still-image dataset: (n, 3, size, size) plus positions.
Source code in xwm/data/synthetic.py
sprite_sequences
¶
sprite_sequences(key: PRNGKey, n_sequences: int, length: int, *, world: SpriteWorld | None = None, smoothness: float = 0.7) -> dict[str, Array]
Generate an action-labelled video dataset.
Returns a dict with:
video:(n, length, 3, size, size)action:(n, length - 1, 2)--action[i, t]joins framestandt + 1, the conventionxwm.action.ActionWorldModelexpects.position:(n, length, 2)ground-truth agent position, for probes.