Data containers — rl_mind.data¶
Transitions, episodes, rollouts and the replay buffer.
Two ways of storing interaction data, depending on the algorithm:
Transitionsis a flat batch of \((s, a, r, s', \textrm{terminated})\) tuples, used by off-policy algorithms (DQN, DDPG, TD3, SAC) through aReplayBuffer;Episodeis one full episode with time-indexed tensors, used by episodic algorithms (REINFORCE);Rolloutis a fixed-length[T, B]segment of interaction, used by on-policy algorithms (A2C, PPO), withminibatchesto iterate over its (flattened) steps.
Off-policy data — a flat batch of transitions, and the buffer they are kept in:
- class rl_mind.data.Transitions(obs: Tensor, action: ActionT, reward: Tensor, next_obs: Tensor, terminated: Tensor)[source]¶
Bases:
TensorStruct,Generic[ActionT]A batch of
Ntransitions \((s, a, r, s', \textrm{terminated})\)- Parameters:
obs – The observations \(s\), shape
[N, obs_dim]action – The actions \(a\) (an
Action, tensors of shape[N, ...])reward – The rewards \(r\), shape
[N]next_obs – The next observations \(s'\), shape
[N, obs_dim]terminated – Whether \(s'\) is a terminal state, shape
[N](bool). When the episode was truncated (time limit), this isFalse: the value of \(s'\) must be bootstrapped.
- class rl_mind.data.ReplayBuffer(capacity: int)[source]¶
-
A fixed-capacity buffer of transitions (a “ring buffer”: when full, new transitions overwrite the oldest ones)
Used by the off-policy notebooks:
addthe transitions returned by arl_mind.collectors.TransitionCollector, thensampleminibatches from the whole (shuffled) history.- capacity¶
The maximum number of transitions the buffer holds
- __len__() int[source]¶
The number of transitions currently stored
- Returns:
A number between 0 and
capacity
- add(transitions: Transitions[ActionT]) None[source]¶
Add a batch of transitions to the buffer
- Parameters:
transitions – The transitions to store (a batch of any size; the storage is allocated from the first batch, so every later batch must have the same fields, shapes and dtypes)
- sample(batch_size: int) Transitions[ActionT][source]¶
Sample a batch of transitions uniformly at random (with replacement)
- Parameters:
batch_size – The number of transitions to draw
- Returns:
A
Transitionsof batch sizebatch_size- Raises:
AssertionError – If the buffer is still empty
Episodic and on-policy data:
- class rl_mind.data.Episode(obs: Tensor, action: ActionT, reward: Tensor, final_obs: Tensor, terminated: bool)[source]¶
-
One full episode of length
T- Parameters:
obs – The observations \(s_0, \ldots, s_{T-1}\), shape
[T, obs_dim]action – The actions \(a_0, \ldots, a_{T-1}\) (tensors
[T, ...])reward – The rewards \(r_1, \ldots, r_T\) where \(r_{t+1}\) is received after performing \(a_t\) in \(s_t\); shape
[T]final_obs – The last observation \(s_T\), shape
[obs_dim](see also theall_obsproperty, which appends it toobs)terminated – True if \(s_T\) is a terminal state; False if the episode was truncated (e.g. time limit), in which case the value of \(s_T\) must be bootstrapped.
- __len__() int[source]¶
The number of steps
Tof the episode- Returns:
The number of actions taken (
obs,actionandrewardall haveTentries;all_obshasT+1)
- property all_obs¶
The whole observation sequence \(s_0, \ldots, s_T\), shape
[T+1, ...]obsonly holds theTobservations on which an action was taken;all_obsappends the final one. Henceall_obs[t+1]is the successor ofall_obs[t]:all_obs[1:]are the successors ofobs, andcritic(all_obs)gives theT+1values \(V(s_0), \ldots, V(s_T)\).
- class rl_mind.data.Rollout(obs: Tensor, action: ActionT, reward: Tensor, next_obs: Tensor, terminated: Tensor, truncated: Tensor)[source]¶
Bases:
TensorStruct,Generic[ActionT]A fixed-length segment of interaction with
Bparallel environmentsAll the tensors are time-indexed: shape
[T, B, ...]. A rollout may span several episodes: episode boundaries are marked byterminated/truncated, andnext_obs[t]is always the true successor state ofobs[t](the final observation when the episode ends at stept, whileobs[t+1]is then the first observation of the next episode).- Parameters:
obs – The observations \(s_t\), shape
[T, B, obs_dim]action – The actions \(a_t\) (an
Action, tensors[T, B, ...])reward – The rewards \(r_{t+1}\), shape
[T, B]next_obs – The successor states \(s_{t+1}\), shape
[T, B, obs_dim]terminated – Whether \(s_{t+1}\) is a terminal state, shape
[T, B]truncated – Whether the episode was truncated at this step, shape
[T, B]
- flatten() Rollout[ActionT][source]¶
Merge the time and environment dimensions:
[T, B, ...] -> [T*B, ...]The time structure is lost, so this is done after the quantities that need it (returns, GAE advantages) have been computed; the flat result is what
minibatchesiterates over.- Returns:
The same rollout with a single leading dimension of size
T * B
- rl_mind.data.minibatches(data: StructT, batch_size: int, shuffle: bool = True) Iterator[StructT][source]¶
Iterate over minibatches of a batch of data
The data is partitioned: each sample belongs to exactly one minibatch (the last one may be smaller). With
shuffle, the partition is random.- Parameters:
data – Any
TensorStruct(e.g.Transitions, or aRolloutthat wasflatten`ed), of batch size `Nbatch_size – The size of the minibatches
shuffle – Whether to shuffle the samples before partitioning them (a different partition at every pass)
- Returns:
An iterator over
ceil(N / batch_size)structures of the same type asdata