The WorldModel is the primary interface for training a world model that predicts future states, rewards, and terminality. It uses a NearestNeighborTokenizer for state representation and a BlockCausalTransformer for temporal modeling.
Training
Call forward() with state tensors (or token IDs), and optionally rewards, actions, or is_terminal labels. The method returns the total loss and a breakdown of individual losses (state, reward, terminal).
Sampling
Call sample() to generate trajectories. You can provide a prompt (initial states), time_steps to sample, and optionally actions or rewards to condition the generation. It supports custom filter_fn (like min_p_filter) and temperature control.
Key Configuration Parameters
image_size, patch_size, channels: Define the input video dimensions.num_actions: If $>0$, enables action conditioning.reward_num_bins: If $>0$, enables reward prediction using HLGaussLoss.tokenizer: A NearestNeighborTokenizer instance or a config dict.transformer: A BlockCausalTransformer instance or a config dict.
from improving_transformers_world_model.world_model import WorldModel
model = WorldModel(
image_size = 64,
patch_size = 4,
channels = 3,
num_actions = 6,
reward_num_bins = 10,
tokenizer = dict(
dim = 128,
distance_threshold = 0.01
),
transformer = dict(
dim = 256,
depth = 8,
block_size = 256, # patches_per_image
heads = 8
)
)
# Training step example
# state: [batch, channels, time, height, width]
loss, breakdown = model(state, rewards=rewards, actions=actions, is_terminal=is_terminal)
# Sampling example
# prompt: [batch, channels, prompt_time, height, width]
sampled_states, sampled_rewards, sampled_dones = model.sample(
prompt=prompt,
time_steps=50,
actions=actions,
temperature=1.5
)