bordax.data
bordax.data.collectors
Collector
Bases: ABC
Abstract base class for data collectors.
A collector interacts with the environment for a fixed number of steps and returns the resulting transitions, optionally storing them in a replay buffer.
Source code in bordax/data/collectors.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
__call__(key, env, obs, env_state, replay_buffer, agent, ts)
abstractmethod
Collect transitions from the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
JAX random key. |
required |
env
|
EnvAdapter
|
The environment to interact with. |
required |
obs
|
EnvObs
|
Current observation batch. |
required |
env_state
|
EnvState
|
Current environment state batch. |
required |
replay_buffer
|
Any
|
Existing replay buffer (on-policy: ignored; off-policy: transitions are appended to it). |
required |
agent
|
Agent
|
Agent used to select actions. |
required |
ts
|
TrainingState
|
Current training state (provides parameters). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Any, EnvState]
|
Tuple of |
Any
|
|
Tuple[Tuple[Any, EnvState], Any]
|
|
Source code in bordax/data/collectors.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
EpsGreedyCollector
Bases: Collector
Collects transitions using an epsilon-greedy policy (for DQN).
At each step, with probability epsilon a random action is taken;
otherwise the greedy action from the Q-network is used. Collected
transitions are added to the replay buffer one at a time.
Source code in bordax/data/collectors.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
__init__(epsilon_schedule, rollout_length=1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon_schedule
|
Callable[[int], float]
|
Callable |
required |
rollout_length
|
int
|
Number of environment steps collected per call. Typically 1 for standard DQN. |
1
|
Source code in bordax/data/collectors.py
245 246 247 248 249 250 251 252 253 254 | |
OnPolicyCollector
Bases: Collector
Collects full rollouts for on-policy algorithms (e.g. PPO).
For jittable environments the rollout is gathered inside
jax.lax.scan, keeping everything on-device. For non-jittable
environments a Python loop is used instead, with a final device
transfer. GAE advantages and value targets are computed after
collection.
Source code in bordax/data/collectors.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
__init__(rollout_length=1024, gamma=0.99, _lambda=0.99)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rollout_length
|
int
|
Number of environment steps per rollout. |
1024
|
gamma
|
float
|
Discount factor used in GAE computation. |
0.99
|
_lambda
|
float
|
GAE lambda parameter controlling the bias-variance tradeoff. |
0.99
|
Source code in bordax/data/collectors.py
63 64 65 66 67 68 69 70 71 72 73 74 | |
compute_gae(traj_batch, last_value, values, gamma, gae_lambda)
Compute Generalised Advantage Estimates (GAE) for a rollout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj_batch
|
Dict of trajectory arrays with shape
|
required | |
last_value
|
Value estimate for the observation after the last step,
shape |
required | |
values
|
Value estimates for all observations in the rollout,
shape |
required | |
gamma
|
Discount factor. |
required | |
gae_lambda
|
GAE lambda parameter. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple of |
|
|
|
Source code in bordax/data/collectors.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
bordax.data.batchbuilders
BatchBuilder
Bases: ABC
Abstract base class for batch builders.
A batch builder transforms a raw buffer (trajectory dict or replay
buffer) into the format expected by the updater. Batch builders can
be chained via ComposedBatchBuilder.
Source code in bordax/data/batchbuilders.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
__call__(key, buffer)
abstractmethod
Transform a buffer into a training batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
JAX random key (for shuffling or sampling). |
required |
buffer
|
Any
|
Raw data — a trajectory dict (on-policy) or a
|
required |
Returns:
| Type | Description |
|---|---|
Tuple[PRNGKey, Mapping[str, ndarray]]
|
A batch dict of JAX arrays ready for the updater. |
Source code in bordax/data/batchbuilders.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
ComposedBatchBuilder
Bases: BatchBuilder
Apply a sequence of batch builders in order.
Each builder's output is passed as input to the next. The full composed call is JIT-compiled. Typical PPO usage::
ComposedBatchBuilder((
FullBufferBatch(rollout_length, num_envs),
MiniBatch(num_minibatches),
NormalizeAdvantagesTargets(),
))
Source code in bordax/data/batchbuilders.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__init__(batch_builders)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_builders
|
Sequence[BatchBuilder]
|
Ordered sequence of batch builders to apply. |
required |
Source code in bordax/data/batchbuilders.py
148 149 150 151 152 153 | |
FullBufferBatch
Bases: BatchBuilder
Flatten and shuffle an entire on-policy rollout into a single batch.
Merges the time and environment dimensions, then applies a random
permutation. Typically the first stage in a ComposedBatchBuilder
for PPO, followed by MiniBatch.
Source code in bordax/data/batchbuilders.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
__init__(buffer_size, num_env)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer_size
|
Number of timesteps in the rollout. |
required | |
num_env
|
Number of parallel environments. |
required |
Source code in bordax/data/batchbuilders.py
41 42 43 44 45 46 47 48 | |
MiniBatch
Bases: BatchBuilder
Split a flat batch into equal-sized minibatches.
Reshapes the leading dimension into (num_minibatches, minibatch_size).
The resulting array is iterated over by the updater's SGD loop.
Source code in bordax/data/batchbuilders.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
__init__(num_minibatches)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_minibatches
|
int
|
Number of minibatches to split the batch into. The batch size must be divisible by this value. |
required |
Source code in bordax/data/batchbuilders.py
77 78 79 80 81 82 83 | |
NormalizeAdvantagesTargets
Bases: BatchBuilder
Normalizes advantages (and optionally value targets) per minibatch.
Source code in bordax/data/batchbuilders.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
__init__(eps=1e-08, normalize_targets=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Small constant added to the standard deviation for numerical stability. |
1e-08
|
normalize_targets
|
bool
|
If |
True
|
Source code in bordax/data/batchbuilders.py
99 100 101 102 103 104 105 106 107 108 | |
UniformReplayBatch
Bases: BatchBuilder
Sample a batch uniformly from a ReplayBuffer.
Source code in bordax/data/batchbuilders.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
__call__(key, buffer)
Sample transitions from replay buffer and convert to JAX arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
PRNG key (unused, but kept for interface consistency) |
required |
buffer
|
Any
|
ReplayBuffer instance |
required |
Returns:
| Type | Description |
|---|---|
Mapping[str, ndarray]
|
Dictionary of JAX arrays with keys: obs, action, reward, next_obs, done |
Source code in bordax/data/batchbuilders.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
__init__(batch_size)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of transitions to sample per update. |
required |
Source code in bordax/data/batchbuilders.py
168 169 170 171 172 173 | |
bordax.data.buffer
ReplayBuffer
A simple ring buffer for storing and sampling transitions for off-policy RL. This implementation is based on NumPy and is not designed to be JAX-jittable.
Source code in bordax/data/buffer.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
__init__(capacity, obs_shape, action_shape)
Initializes the replay buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
The maximum number of transitions to store. |
required |
obs_shape
|
Tuple[int, ...]
|
The shape of a single observation. |
required |
action_shape
|
Tuple[int, ...]
|
The shape of a single action. |
required |
Source code in bordax/data/buffer.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
add(rollout)
Adds a batch of transitions to the buffer. The input arrays in the rollout dictionary are expected to have the same leading dimension. Required keys: 'obs', 'action', 'reward', 'next_obs', 'done'.
Source code in bordax/data/buffer.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
sample(batch_size)
Samples a batch of transitions from the buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
The number of transitions to sample. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, ndarray]
|
A dictionary containing the sampled transitions. |
Source code in bordax/data/buffer.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |