๐พMemory
Last updated
Was this helpful?
Was this helpful?
class Memory:
def update(self, state, action, reward, next_state, done, terminated, truncated):
"""Store a transition."""
pass
def sample(self) -> dict:
"""Sample a batch for training. Returns dict with keys:
- states: (batch_size, state_dim)
- actions: (batch_size, action_dim)
- rewards: (batch_size,)
- next_states: (batch_size, state_dim)
- dones: (batch_size,)
- terminateds: (batch_size,) # v5: true episode end
- truncateds: (batch_size,) # v5: time limit reached
"""
pass
def __len__(self) -> int:
"""Current number of stored transitions."""
pass# In the training loop
agent.memory.update(state, action, reward, next_state, done, terminated, truncated)
if agent.memory.to_train:
loss = agent.algorithm.train()
agent.memory.to_train = False # Reset flag{
"agent": {
"memory": {
"name": "Replay", // Memory class name
"batch_size": 32, // Samples per training batch
"max_size": 10000, // Maximum buffer capacity
"use_cer": true // Combined Experience Replay
}
}
}{
"memory": {
"name": "Replay",
"use_cer": true // Guarantees latest transition is sampled
}
}batch = memory.sample()
# batch = {
# 'states': tensor of shape (batch_size, *state_shape),
# 'actions': tensor of shape (batch_size, *action_shape),
# 'rewards': tensor of shape (batch_size,),
# 'next_states': tensor of shape (batch_size, *state_shape),
# 'dones': tensor of shape (batch_size,),
# 'terminateds': tensor of shape (batch_size,),
# 'truncateds': tensor of shape (batch_size,),
# }