velora.training¶
Methods and classes dedicated to handling agent training.
MovingMetric
¶
Tracks a metric with a moving window for statistics.
Attributes:
Name | Type | Description |
---|---|---|
window |
torch.Tensor
|
a list of values for the statistics |
window_size |
int
|
the window size of the moving statistics |
Source code in velora/training/metrics.py
Python | |
---|---|
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 |
|
latest
property
¶
Gets the latest value.
__init__(window_size, *, device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
window_size
|
int
|
the size of the moving window |
required |
device
|
torch.device
|
the device to perform computations on |
None
|
Source code in velora/training/metrics.py
Python | |
---|---|
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
__len__()
¶
Returns the number of items in the values array.
Source code in velora/training/metrics.py
Python | |
---|---|
185 186 187 |
|
add(value)
¶
Adds a value and updates the window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
torch.Tensor
|
value to add |
required |
Source code in velora/training/metrics.py
Python | |
---|---|
141 142 143 144 145 146 147 148 149 150 151 152 |
|
max()
¶
Calculates the maximum value of a set of values or the current window.
Returns:
Name | Type | Description |
---|---|---|
max |
torch.Tensor
|
the maximum value. |
Source code in velora/training/metrics.py
Python | |
---|---|
176 177 178 179 180 181 182 183 |
|
mean()
¶
Calculates the mean of values or the current window.
Returns:
Name | Type | Description |
---|---|---|
avg |
torch.Tensor
|
the calculated mean. |
Source code in velora/training/metrics.py
Python | |
---|---|
154 155 156 157 158 159 160 161 |
|
std()
¶
Calculates the standard deviation of values or the current window.
Returns:
Name | Type | Description |
---|---|---|
std |
torch.Tensor
|
the calculated standard deviation. |
Source code in velora/training/metrics.py
Python | |
---|---|
163 164 165 166 167 168 169 170 171 172 173 174 |
|
StepStorage
¶
A storage container for step metrics.
Useful for calculating the episodic average values to store in MetricStorage
.
Attributes:
Name | Type | Description |
---|---|---|
critic_losses |
torch.Tensor
|
a tensor of agent Critic loss values |
actor_losses |
torch.Tensor
|
a tensor of agent Actor loss values |
entropy_losses |
torch.Tensor
|
a tensor of agent Entropy loss values |
Source code in velora/training/metrics.py
Python | |
---|---|
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 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 |
|
__init__(capacity, *, device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
capacity
|
int
|
storage capacity for each tensor |
required |
device
|
torch.device
|
the device to perform computations on |
None
|
Source code in velora/training/metrics.py
Python | |
---|---|
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
actor_avg(ep_length)
¶
Computes the actor loss average. Useful for computing episodic averages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ep_length
|
int
|
size of the episode |
required |
Returns:
Name | Type | Description |
---|---|---|
avg |
torch.Tensor
|
actor loss step average |
Source code in velora/training/metrics.py
Python | |
---|---|
54 55 56 57 58 59 60 61 62 63 64 |
|
add(critic, actor, entropy)
¶
Adds one of each metric into storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
critic
|
torch.Tensor
|
critic loss |
required |
actor
|
torch.Tensor
|
actor loss |
required |
entropy
|
torch.Tensor
|
entropy loss |
required |
Source code in velora/training/metrics.py
Python | |
---|---|
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
critic_avg(ep_length)
¶
Computes the critic loss average. Useful for computing episodic averages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ep_length
|
int
|
size of the episode |
required |
Returns:
Name | Type | Description |
---|---|---|
avg |
torch.Tensor
|
critic loss step average |
Source code in velora/training/metrics.py
Python | |
---|---|
42 43 44 45 46 47 48 49 50 51 52 |
|
empty()
¶
Empty storage.
Source code in velora/training/metrics.py
Python | |
---|---|
100 101 102 103 104 105 106 107 |
|
entropy_avg(ep_length)
¶
Computes the entropy loss average. Useful for computing episodic averages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ep_length
|
int
|
size of the episode |
required |
Returns:
Name | Type | Description |
---|---|---|
avg |
torch.Tensor
|
entropy loss step average |
Source code in velora/training/metrics.py
Python | |
---|---|
66 67 68 69 70 71 72 73 74 75 76 |
|
TrainHandler
¶
Bases: TrainHandlerBase
A context manager for handling an agents training state. Compatible with single environments.
Source code in velora/training/handler.py
Python | |
---|---|
193 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 235 236 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 |
|
metrics
property
¶
Training metric class instance.
Returns:
Name | Type | Description |
---|---|---|
metrics |
TrainMetrics
|
current training metric state. |
__enter__()
¶
Setup the training context, initializing the environment.
Returns:
Name | Type | Description |
---|---|---|
self |
Self
|
the initialized context. |
Source code in velora/training/handler.py
Python | |
---|---|
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
__init__(agent, n_episodes, max_steps, log_freq, window_size, callbacks)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
RLModuleAgent
|
the agent being trained |
required |
n_episodes
|
int
|
the total number of training episodes |
required |
max_steps
|
int
|
maximum number of steps in an episode |
required |
log_freq
|
int
|
metric logging frequency (in episodes) |
required |
window_size
|
int
|
episode window size rate |
required |
callbacks
|
List[TrainCallback] | None
|
a list of training callbacks.
If |
required |
Source code in velora/training/handler.py
Python | |
---|---|
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
log(idx, log_type)
¶
Performs logging
callback event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
the current training step or episode index |
required |
log_type
|
str
|
the type of logging method |
required |
Source code in velora/training/handler.py
Python | |
---|---|
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
step(current_step)
¶
Performs step
callback event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_step
|
int
|
the current training timestep index |
required |
Source code in velora/training/handler.py
Python | |
---|---|
278 279 280 281 282 283 284 285 286 |
|
TrainMetrics
¶
Bases: TrainMetricsBase
A utility class for working with and storing episodic training metrics for monitoring an agents training performance.
Source code in velora/training/metrics.py
Python | |
---|---|
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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|
__init__(session, window_size, n_episodes, max_steps, *, device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
sqlmodel.Session
|
current metric database session |
required |
window_size
|
int
|
moving average window size |
required |
n_episodes
|
int
|
total number of training episodes |
required |
max_steps
|
int
|
maximum number of steps per episode |
required |
device
|
torch.device
|
the device to perform computations on |
None
|
Source code in velora/training/metrics.py
Python | |
---|---|
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
add_episode(ep_idx, reward, ep_length)
¶
Add episode metrics to the metric database and reset step accumulators.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ep_idx
|
int
|
the current episode index |
required |
reward
|
torch.Tensor
|
episode reward |
required |
ep_length
|
torch.Tensor
|
number of steps after episode done |
required |
Source code in velora/training/metrics.py
Python | |
---|---|
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 |
|
add_step(critic, actor, entropy)
¶
Add timestep metrics to local storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
critic
|
torch.Tensor
|
critic step loss |
required |
actor
|
torch.Tensor
|
actor step loss |
required |
entropy
|
torch.Tensor
|
entropy step loss |
required |
Source code in velora/training/metrics.py
Python | |
---|---|
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
info(current_ep)
¶
Outputs basic information to the console.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_ep
|
int
|
the current episode index |
required |
Source code in velora/training/metrics.py
Python | |
---|---|
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|