bordax.algorithms
bordax.algorithms.base
Algorithm
Bases: NamedTuple
A training algorithm composed of a collector, batch builder, and updater.
Attributes:
| Name | Type | Description |
|---|---|---|
collector |
Collector
|
Generates transitions by interacting with the environment. |
batch_builder |
BatchBuilder
|
Transforms collected data into training batches. |
updater |
Updater
|
Applies gradient updates to the network parameters. |
Source code in bordax/algorithms/base.py
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 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 | |
collect(key, env, obs, env_state, replay_buffer, agent, ts)
Collect experience from the environment.
Delegates to self.collector. For on-policy algorithms the
returned buffer contains the freshly collected rollout; for
off-policy algorithms transitions are appended to the existing
replay buffer which is returned.
Returns:
| Type | Description |
|---|---|
|
Tuple of |
Source code in bordax/algorithms/base.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
init_training_state(agent, key, sample_obs, env)
Initialise the training state for a given agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
The agent whose parameters are initialised. |
required |
key
|
PRNGKey
|
JAX random key. |
required |
sample_obs
|
Any
|
A sample observation used to infer network input shapes. |
required |
env
|
EnvAdapter
|
The training environment (used by some updaters). |
required |
Returns:
| Type | Description |
|---|---|
TrainingState
|
A |
Source code in bordax/algorithms/base.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
train_step(env, agent, key, ts, replay_buffer, obs, env_state)
Run one full training iteration: collect → batch → update.
This method is JIT-compiled by the Trainer when the environment
is jittable and the algorithm is on-policy.
Returns:
| Type | Description |
|---|---|
|
Tuple of |
Source code in bordax/algorithms/base.py
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 | |
update(agent, batch, ts, key)
JIT-compiled parameter update step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent providing loss function access. |
required |
batch
|
Any
|
Training batch produced by the batch builder. |
required |
ts
|
TrainingState
|
Current training state. |
required |
key
|
PRNGKey
|
JAX random key. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple of |
Source code in bordax/algorithms/base.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
dqn_algo(epsilon_schedule=lambda t: 0.1, rollout_length=1, batch_size=32, gamma=0.99, lr=0.0001, target_update_freq=1000, applied_loss=optax.squared_error, **kwargs)
Create a DQN algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon_schedule
|
Callable[[int], float]
|
Callable |
lambda t: 0.1
|
rollout_length
|
int
|
Number of environment steps collected per update. Typically 1 for standard DQN. |
1
|
batch_size
|
int
|
Number of transitions sampled from the replay buffer per update. |
32
|
gamma
|
float
|
Discount factor for Bellman targets. |
0.99
|
lr
|
float
|
Adam learning rate for the Q-network. |
0.0001
|
target_update_freq
|
int
|
Number of training steps between target network hard updates. |
1000
|
applied_loss
|
Callable
|
Element-wise loss applied to TD errors (e.g.
|
squared_error
|
Returns:
| Type | Description |
|---|---|
|
A configured |
Source code in bordax/algorithms/base.py
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 235 236 237 238 239 240 241 242 243 244 245 246 | |
ppo_algo(rollout_length=1024, gamma=0.99, _lambda=0.85, lr=0.001, clip_schedule=lambda _: 0.2, vf_schedule=lambda _: 0.5, ent_schedule=lambda _: 0.01, num_minibatches=16, num_sgd_steps=1, num_envs=1, grad_clip=0.5, **kwargs)
Create a PPO algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rollout_length
|
int
|
Number of environment steps collected per epoch
per environment. Must be divisible by |
1024
|
gamma
|
float
|
Discount factor for returns. |
0.99
|
_lambda
|
float
|
GAE lambda for advantage estimation. |
0.85
|
lr
|
float
|
Adam learning rate. |
0.001
|
clip_schedule
|
Callable |
lambda _: 0.2
|
|
vf_schedule
|
Callable |
lambda _: 0.5
|
|
ent_schedule
|
Callable |
lambda _: 0.01
|
|
num_minibatches
|
Number of minibatches to split each rollout into. |
16
|
|
num_sgd_steps
|
Number of SGD passes over the data per epoch. |
1
|
|
num_envs
|
int
|
Number of parallel environments (used for batch reshaping). |
1
|
grad_clip
|
float
|
Global gradient norm clipping threshold. |
0.5
|
Returns:
| Type | Description |
|---|---|
|
A configured |
Source code in bordax/algorithms/base.py
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
bordax.algorithms.losses
bordax.algorithms.utils
make_algo(algo_name, algo_config={})
Create an algorithm by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algo_name
|
str
|
Algorithm identifier. Supported values:
|
required |
algo_config
|
dict
|
Dict of hyperparameters forwarded to the algorithm
factory function. See |
{}
|
Returns:
| Type | Description |
|---|---|
Algorithm
|
A configured |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in bordax/algorithms/utils.py
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 | |