Training
ginjax.ml.training
¤
MultiImageDataset
¤
Bases: Dataset
A basic dataset for multi images which assumes that we already have the X and Y in memory. The getitem for this class expects a list of integer indices for the entire batch at once, which means the sampler of the data loader should be a batch sampler.
Source code in ginjax/ml/training.py
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 | |
Mapper
¤
Functor for map_and_loss in train, map_loss_in_batches, etc, where arguments can be provided beforehand. In this case, it is useful for smse vs relative error, and whether to learn the residual or not.
Source code in ginjax/ml/training.py
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
__init__(losses: list[geom.Losses], residual: bool = False, reduce: str | None = 'mean', eps: float = 0) -> None
¤
Docstring for init
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
losses
|
list[Losses]
|
a list of losses, must be at least 1 |
required |
residual
|
bool
|
Whether the network should learn the residual, defaults to False |
False
|
reduce
|
str | None
|
How to reduce the batch dimension, defaults to 'mean' but can also be None |
'mean'
|
eps
|
float
|
epsilon value to use for nrmse and lr_rel, avoid dividing by 0 |
0
|
Source code in ginjax/ml/training.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 | |
map(model: models.MultiImageModule, multi_image_x: geom.MultiImage, aux_data: eqx.nn.State | None = None) -> tuple[geom.MultiImage, eqx.nn.State | None]
¤
The map function using the model and the input data.
Source code in ginjax/ml/training.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 | |
__call__(model: models.MultiImageModule, multi_image_x: geom.MultiImage, multi_image_y: geom.MultiImage, aux_data: eqx.nn.State | None = None) -> tuple[jax.Array, eqx.nn.State | None]
¤
Equivalent of the map_and_loss function.
Source code in ginjax/ml/training.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
save(filename: str | pathlib.Path, model: models.MultiImageModule) -> None
¤
Save an equinox model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
the file to save the model to |
required |
model
|
MultiImageModule
|
the model to save |
required |
Source code in ginjax/ml/training.py
32 33 34 35 36 37 38 39 40 41 42 | |
save_plus(filename: str | pathlib.Path, model: models.MultiImageModule, further_args: dict = {}) -> None
¤
New version of save, allows you to save any serializable args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
the file to save the model to |
required |
model
|
MultiImageModule
|
the model to save |
required |
further_args
|
dict
|
more values to save, as a dictionary |
{}
|
Source code in ginjax/ml/training.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
load(filename: str | pathlib.Path, model: models.MultiImageModule) -> models.MultiImageModule
¤
Load an equinox model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
the file to load the model from |
required |
model
|
MultiImageModule
|
the type of model we are loading, the parameter values will be set to the loaded ones |
required |
Returns:
| Type | Description |
|---|---|
MultiImageModule
|
the loaded model |
Source code in ginjax/ml/training.py
63 64 65 66 67 68 69 70 71 72 73 74 75 | |
load_plus(filename: str | pathlib.Path, model: models.MultiImageModule) -> tuple[models.MultiImageModule, dict]
¤
Load an equinox model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
the file to load the model from |
required |
model
|
MultiImageModule
|
the type of model we are loading, the parameter values will be set to the loaded ones |
required |
Returns:
| Type | Description |
|---|---|
tuple[MultiImageModule, dict]
|
the loaded model |
Source code in ginjax/ml/training.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
get_batches(multi_images: Union[Sequence[geom.MultiImage], geom.MultiImage], batch_size: int, rand_key: Optional[ArrayLike], devices: Optional[list[jax.Device]] = None) -> list[list[geom.MultiImage]]
¤
Given a set of MultiImages, construct random batches of those MultiImages. The most common use case is for MultiImagess to be a tuple (X,Y) so that the batches have the inputs and outputs. In this case, it will return a list of length 2 where the first element is a list of the batches of the input data and the second element is the same batches of the output data. Automatically reshapes the batches to use with pmap based on the number of gpus found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
multi_images
|
Union[Sequence[MultiImage], MultiImage]
|
MultiImages which all get simultaneously batched |
required |
batch_size
|
int
|
length of the batch |
required |
rand_key
|
Optional[ArrayLike]
|
key for the randomness. If None, the order won't be random |
required |
devices
|
Optional[list[Device]]
|
gpu/cpu devices to use, if None (default) then sets this to jax.devices() |
None
|
Returns:
| Type | Description |
|---|---|
list[list[MultiImage]]
|
list of lists of batches (which are MultiImages) |
Source code in ginjax/ml/training.py
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 | |
autoregressive_step(input: geom.MultiImage, output: geom.MultiImage, past_steps: int, constant_fields_dict: dict[tuple[tuple[bool, ...], int], int] = {}, future_steps: int = 1) -> geom.MultiImage
¤
Given the input MultiImage, the next step of the model, update the input to be fed into the model next. MultiImages should have shape (channels,spatial,tensor). Channels are c*past_steps + constant_fields where c is some positive integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
MultiImage
|
the input to the model |
required |
output
|
MultiImage
|
the model output at this step, assumed to be a single time step |
required |
past_steps
|
int
|
the number of past time steps that are fed into the model |
required |
constant_fields_dict
|
dict[tuple[tuple[bool, ...], int], int]
|
a map {key:n_constant_fields} for fields that don't depend on timestep |
{}
|
future_steps
|
int
|
number of future steps that the model outputs, currently must be 1 |
1
|
Returns:
| Type | Description |
|---|---|
MultiImage
|
the new input |
Source code in ginjax/ml/training.py
186 187 188 189 190 191 192 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 | |
autoregressive_map(model: models.MultiImageModule, x: geom.MultiImage, aux_data: Optional[eqx.nn.State] = None, past_steps: int = 1, autoregressive_steps: int = 1, constant_fields: dict[tuple[tuple[bool, ...], int], int] = {}) -> tuple[geom.MultiImage, Optional[eqx.nn.State]]
¤
Given a model, perform an autoregressive step n times, and return the output steps in a single MultiImage. Currently the model must output a single time step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
MultiImageModule
|
model that operates on MultiImages |
required |
x
|
MultiImage
|
the input MultiImage to map |
required |
aux_data
|
Optional[State]
|
auxilliary data to pass to the network |
None
|
past_steps
|
int
|
the number of past steps input to the autoregressive map |
1
|
autoregressive_steps
|
int
|
how many times to loop through the autoregression |
1
|
constant_fields
|
dict[tuple[tuple[bool, ...], int], int]
|
data structure which explains which fields are constant fields |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[MultiImage, Optional[State]]
|
the output map with number of steps equal to future steps, and the aux_data |
Source code in ginjax/ml/training.py
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 | |
evaluate(model: models.MultiImageModule, map_and_loss: Union[Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, Optional[eqx.nn.State]], tuple[jax.Array, Optional[eqx.nn.State]]], Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, Optional[eqx.nn.State]], tuple[jax.Array, Optional[eqx.nn.State], geom.MultiImage]]], x: geom.MultiImage, y: geom.MultiImage, aux_data: Optional[eqx.nn.State] = None, return_map: bool = False) -> Union[jax.Array, tuple[jax.Array, geom.MultiImage]]
¤
Runs map_and_loss for the entire x, y, splitting into batches if the MultiImage is larger than the batch_size. This is helpful to run a whole validation/test set through map and loss when you need to split those over batches for memory reasons. Automatically pmaps over multiple gpus, so the number of gpus must evenly divide batch_size as well as as any remainder of the MultiImage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
MultiImageModule
|
the model to run through map_and_loss |
required |
map_and_loss
|
Union[Callable[[MultiImageModule, MultiImage, MultiImage, Optional[State]], tuple[Array, Optional[State]]], Callable[[MultiImageModule, MultiImage, MultiImage, Optional[State]], tuple[Array, Optional[State], MultiImage]]]
|
function that takes in model, X_batch, Y_batch, and aux_data if has_aux is true, and returns the loss, and aux_data if has_aux is true. |
required |
x
|
MultiImage
|
input data |
required |
y
|
MultiImage
|
target output data |
required |
aux_data
|
Optional[State]
|
auxilliary data, such as batch stats. Passed to the function is has_aux is True. |
None
|
return_map
|
bool
|
whether to also return the map of x |
False
|
Returns:
| Type | Description |
|---|---|
Union[Array, tuple[Array, MultiImage]]
|
Average loss over the entire MultiImage |
Source code in ginjax/ml/training.py
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 | |
loss_reducer(ls: list[jax.Array]) -> jax.Array
¤
A reducer for map_loss_in_batches that takes the batch mean of the loss
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ls
|
list[Array]
|
list of losses |
required |
Returns:
| Type | Description |
|---|---|
Array
|
the mean of the losses |
Source code in ginjax/ml/training.py
326 327 328 329 330 331 332 333 334 335 336 | |
multi_image_reducer(ls: list[geom.MultiImage]) -> geom.MultiImage
¤
If map data returns the mapped MultiImages, merge them togther
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ls
|
list[MultiImage]
|
list of MultiImages |
required |
Returns:
| Type | Description |
|---|---|
MultiImage
|
a single concatenated MultiImage |
Source code in ginjax/ml/training.py
339 340 341 342 343 344 345 346 347 348 349 | |
map_loss_in_batches_dl(map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, eqx.nn.State | None], tuple[jax.Array, eqx.nn.State | None]], model: models.MultiImageModule, dataloader: DataLoader, aux_data: eqx.nn.State | None = None, reduce: str | None = 'mean') -> jax.Array
¤
Runs map_and_loss for the entire x, y, splitting into batches if the MultiImage is larger than the batch_size. This is helpful to run a whole validation/test set through map and loss when you need to split those over batches for memory reasons. Automatically pmaps over multiple gpus, so the number of gpus must evenly divide batch_size as well as as any remainder of the MultiImage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_and_loss
|
Callable[[MultiImageModule, MultiImage, MultiImage, State | None], tuple[Array, State | None]]
|
function that takes in model, X_batch, Y_batch, and aux_data and returns the loss and aux_data |
required |
model
|
MultiImageModule
|
the model to run through map_and_loss |
required |
dataloader
|
DataLoader
|
the dataloader for input and output multi image data |
required |
aux_data
|
State | None
|
auxilliary data, such as batch stats. Passed to the function is has_aux is True. |
None
|
reduce
|
str | None
|
how to reduce between batches, defaults to mean |
'mean'
|
Returns:
| Type | Description |
|---|---|
Array
|
Average loss over the entire BatchMultiImage |
Source code in ginjax/ml/training.py
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 | |
map_loss_in_batches(map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, Optional[eqx.nn.State]], tuple[jax.Array, Optional[eqx.nn.State]]], model: models.MultiImageModule, x: geom.MultiImage, y: geom.MultiImage, batch_size: int, rand_key: Optional[ArrayLike], devices: Optional[list[jax.Device]] = None, aux_data: Optional[eqx.nn.State] = None, reduce: str | None = 'mean') -> jax.Array
¤
Runs map_and_loss for the entire x, y, splitting into batches if the MultiImage is larger than the batch_size. This is helpful to run a whole validation/test set through map and loss when you need to split those over batches for memory reasons. Automatically pmaps over multiple gpus, so the number of gpus must evenly divide batch_size as well as as any remainder of the MultiImage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_and_loss
|
Callable[[MultiImageModule, MultiImage, MultiImage, Optional[State]], tuple[Array, Optional[State]]]
|
function that takes in model, X_batch, Y_batch, and aux_data and returns the loss and aux_data |
required |
model
|
MultiImageModule
|
the model to run through map_and_loss |
required |
x
|
MultiImage
|
input data |
required |
y
|
MultiImage
|
target output data |
required |
batch_size
|
int
|
effective batch_size, must be divisible by number of gpus |
required |
rand_key
|
Optional[ArrayLike]
|
rand key passed to get_batches, on None order won't be randomized |
required |
devices
|
Optional[list[Device]]
|
the gpus that the code will run on |
None
|
aux_data
|
Optional[State]
|
auxilliary data, such as batch stats. Passed to the function is has_aux is True. |
None
|
reduce
|
str | None
|
how to reduce between batches, defaults to mean |
'mean'
|
Returns:
| Type | Description |
|---|---|
Array
|
Average loss over the entire BatchMultiImage |
Source code in ginjax/ml/training.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
map_plus_loss_in_batches(map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, Optional[eqx.nn.State]], tuple[jax.Array, Optional[eqx.nn.State], geom.MultiImage]], model: models.MultiImageModule, x: geom.MultiImage, y: geom.MultiImage, batch_size: int, rand_key: Optional[ArrayLike], devices: Optional[list[jax.Device]] = None, aux_data: Optional[eqx.nn.State] = None) -> tuple[jax.Array, geom.MultiImage]
¤
This is like map_loss_in_batches, but it returns the mapped images in additon to just the loss.
Runs map_and_loss for the entire x, y, splitting into batches if the MultiImage is larger than
the batch_size. This is helpful to run a whole validation/test set through map and loss when you need
to split those over batches for memory reasons. Automatically pmaps over multiple gpus, so the number
of gpus must evenly divide batch_size as well as as any remainder of the MultiImage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_and_loss
|
Callable[[MultiImageModule, MultiImage, MultiImage, Optional[State]], tuple[Array, Optional[State], MultiImage]]
|
function that takes in model, X_batch, Y_batch, and aux_data and returns the loss and aux_data |
required |
model
|
MultiImageModule
|
the model to run through map_and_loss |
required |
x
|
MultiImage
|
input data |
required |
y
|
MultiImage
|
target output data |
required |
batch_size
|
int
|
effective batch_size, must be divisible by number of gpus |
required |
rand_key
|
Optional[ArrayLike]
|
rand key passed to get_batches, on none the order will not be randomized |
required |
devices
|
Optional[list[Device]]
|
the gpus that the code will run on |
None
|
aux_data
|
Optional[State]
|
auxilliary data, such as batch stats. Passed to the function is has_aux is True. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Array, MultiImage]
|
Average loss over the entire MultiImage, and the mapped entire MultiImage |
Source code in ginjax/ml/training.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
train_step(map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, Optional[eqx.nn.State]], tuple[jax.Array, Optional[eqx.nn.State]]], model: models.MultiImageModule, optim: optax.GradientTransformation, opt_state: Any, x: geom.MultiImage, y: geom.MultiImage, aux_data: Optional[eqx.nn.State] = None) -> tuple[models.MultiImageModule, Any, jax.Array, Optional[eqx.nn.State]]
¤
Perform one step and gradient update of the model. Uses filter_pmap to use multiple gpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_and_loss
|
Callable[[MultiImageModule, MultiImage, MultiImage, Optional[State]], tuple[Array, Optional[State]]]
|
map and loss function where the input is a model pytree, x, y, and aux_data, and returns a float loss and aux_data |
required |
model
|
MultiImageModule
|
the model |
required |
optim
|
GradientTransformation
|
the optimizer |
required |
opt_state
|
Any
|
|
required |
x
|
MultiImage
|
input data |
required |
y
|
MultiImage
|
target data |
required |
aux_data
|
Optional[State]
|
auxilliary data for stateful layers |
None
|
Returns:
| Type | Description |
|---|---|
tuple[MultiImageModule, Any, Array, Optional[State]]
|
model, opt_state, loss_value, aux_data |
Source code in ginjax/ml/training.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
train_dl(train_dataloader: DataLoader, map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, eqx.nn.State | None], tuple[jax.Array, eqx.nn.State | None]], model: models.MultiImageModule, stop_condition: StopCondition, optimizer: optax.GradientTransformation, val_dataloader: DataLoader | None = None, val_map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, eqx.nn.State | None], tuple[jax.Array, eqx.nn.State | None]] | None = None, save_model: str | None = None, aux_data: eqx.nn.State | None = None, is_wandb: bool = False) -> tuple[models.MultiImageModule, eqx.nn.State | None, ArrayLike | None, ArrayLike | None, float]
¤
Method to train the model. It uses stochastic gradient descent (SGD) with the optimizer to learn the parameters the minimize the map_and_loss function. The model is returned. This function automatically pmaps over the available gpus, so batch_size should be divisible by the number of gpus. If you only want to train on a single GPU, the script should be run with CUDA_VISIBLE_DEVICES=# for whatever gpu number. This version uses pytorch datasets and dataloaders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_dataloader
|
DataLoader
|
dataloader for train input and target data. Each is a MultiImage by k of (images, channels, (N,)D, (D,)k) |
required |
map_and_loss
|
Callable[[MultiImageModule, MultiImage, MultiImage, State | None], tuple[Array, State | None]]
|
function that takes in model, X_batch, Y_batch, and aux_data and returns the loss and aux_data. |
required |
model
|
MultiImageModule
|
Model pytree |
required |
stop_condition
|
StopCondition
|
when to stop the training process, currently only 1 condition at a time |
required |
batch_size
|
the size of each mini-batch in SGD |
required | |
optimizer
|
GradientTransformation
|
optimizer |
required |
val_dataloader
|
DataLoader | None
|
dataloader for val input and target data. Each is a MultiImage by k of (images, channels, (N,)D, (D,)k) |
None
|
save_model
|
str | None
|
if string, save model every 10 epochs, defaults to None |
None
|
aux_data
|
State | None
|
initial aux data passed in to map_and_loss when has_aux is true. |
None
|
is_wandb
|
bool
|
whether wandb experiment tracking has been initiated and should be logged to |
False
|
Returns:
| Type | Description |
|---|---|
tuple[MultiImageModule, State | None, ArrayLike | None, ArrayLike | None, float]
|
A tuple of best model in inference mode, aux_data, epoch loss, val loss, and train_time |
Source code in ginjax/ml/training.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
train(X: geom.MultiImage, Y: geom.MultiImage, map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, eqx.nn.State | None], tuple[jax.Array, eqx.nn.State | None]], model: models.MultiImageModule, rand_key: jax.Array, stop_condition: StopCondition, batch_size: int, optimizer: optax.GradientTransformation, validation_X: geom.MultiImage | None = None, validation_Y: geom.MultiImage | None = None, val_map_and_loss: Callable[[models.MultiImageModule, geom.MultiImage, geom.MultiImage, eqx.nn.State | None], tuple[jax.Array, eqx.nn.State | None]] | None = None, save_model: str | None = None, devices: list[jax.Device] | None = None, aux_data: eqx.nn.State | None = None, is_wandb: bool = False) -> tuple[models.MultiImageModule, eqx.nn.State | None, ArrayLike | None, ArrayLike | None, float]
¤
Method to train the model. It uses stochastic gradient descent (SGD) with the optimizer to learn the parameters the minimize the map_and_loss function. The model is returned. This function automatically pmaps over the available gpus, so batch_size should be divisible by the number of gpus. If you only want to train on a single GPU, the script should be run with CUDA_VISIBLE_DEVICES=# for whatever gpu number. Use train_dl if you would like to pass pytorch dataloaders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
MultiImage
|
The X input data as a MultiImage by k of (images, channels, (N,)D, (D,)k) |
required |
Y
|
MultiImage
|
The Y target data as a MultiImage by k of (images, channels, (N,)D, (D,)k) |
required |
map_and_loss
|
Callable[[MultiImageModule, MultiImage, MultiImage, State | None], tuple[Array, State | None]]
|
function that takes in model, X_batch, Y_batch, and aux_data and returns the loss and aux_data. |
required |
model
|
MultiImageModule
|
Model pytree |
required |
rand_key
|
Array
|
key for randomness |
required |
stop_condition
|
StopCondition
|
when to stop the training process, currently only 1 condition at a time |
required |
batch_size
|
int
|
the size of each mini-batch in SGD |
required |
optimizer
|
GradientTransformation
|
optimizer |
required |
validation_X
|
MultiImage | None
|
input data for a validation data set as a MultiImage by k of (images, channels, (N,)D, (D,)k) |
None
|
validation_Y
|
MultiImage | None
|
target data for a validation data set as a MultiImage by k of (images, channels, (N,)D, (D,)k) |
None
|
save_model
|
str | None
|
if string, save model every 10 epochs, defaults to None |
None
|
aux_data
|
State | None
|
initial aux data passed in to map_and_loss when has_aux is true. |
None
|
devices
|
list[Device] | None
|
gpu/cpu devices to use, if None (default) then it will use jax.devices() |
None
|
is_wandb
|
bool
|
whether wandb experiment tracking has been initiated and should be logged to |
False
|
Returns:
| Type | Description |
|---|---|
tuple[MultiImageModule, State | None, ArrayLike | None, ArrayLike | None, float]
|
A tuple of best model in inference mode, aux_data, epoch loss, val loss, and train_time |
Source code in ginjax/ml/training.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
benchmark(get_data: Callable, models: list[tuple[str, Callable, dict]], rand_key: ArrayLike, benchmark: str, benchmark_range: Sequence, benchmark_type: str = BENCHMARK_DATA, num_trials: int = 1, num_results: int = 1, is_wandb: bool = False, wandb_project: str = '', wandb_entity: str = '', args: dict = {}) -> np.ndarray
¤
Method to benchmark multiple models as a particular benchmark over the specified range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
get_data
|
Callable
|
function that takes as its first argument the benchmark_value, and a rand_key as its second argument. It returns the data which later gets passed to model. |
required |
models
|
list[tuple[str, Callable, dict]]
|
the elements of the tuple are (str) model_name, (func) model, and a dict of keyword arguments to pass to model. Model is a function that takes data, a rand_key, the model_name, and remaining keyword arguments and returns either a single float score or an iterable of length num_results of float scores. |
required |
rand_key
|
ArrayLike
|
key for randomness |
required |
benchmark
|
str
|
the type of benchmarking to do |
required |
benchmark_range
|
Sequence
|
iterable of the benchmark values to range over |
required |
benchmark_type
|
str
|
one of { BENCHMARK_DATA, BENCHMARK_MODEL, BENCHMARK_NONE } |
BENCHMARK_DATA
|
num_trials
|
int
|
number of trials to run |
1
|
num_results
|
int
|
the number of results that will come out of the model function. If num_results is greater than 1, it should be indexed by range(num_results) |
1
|
is_wandb
|
bool
|
whether wandb experiment tracking is enabled |
False
|
wandb_project
|
str
|
the string name of the wandb project |
''
|
wandb_entity
|
str
|
the wandb user |
''
|
args
|
dict
|
args to add the the wandb config |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
an np.array of shape (trials, benchmark_range, models, num_results) with the results all filled in |
Source code in ginjax/ml/training.py
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 | |
benchmark_lr(get_data: Callable, models: list[tuple[str, Callable, dict]], rand_key: ArrayLike, lr_range: Sequence[float], num_trials: int = 1, num_results: int = 1, is_wandb: bool = False, wandb_project: str = '', wandb_entity: str = '', args: dict = {}) -> np.ndarray
¤
The most common usecase of the benchmark function is benchmarking over a learning rate range. If the lr_range has no values, instead this defaults to no benchmarking, just over the model list and number of trials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
get_data
|
Callable
|
function that takes as its first argument the benchmark_value, and a rand_key as its second argument. It returns the data which later gets passed to model. |
required |
models
|
list[tuple[str, Callable, dict]]
|
the elements of the tuple are (str) model_name, (func) model, and a dict of keyword arguments to pass to model. Model is a function that takes data, a rand_key, the model_name, and remaining keyword arguments and returns either a single float score or an iterable of length num_results of float scores. |
required |
rand_key
|
ArrayLike
|
key for randomness |
required |
num_trials
|
int
|
number of trials to run |
1
|
num_results
|
int
|
the number of results that will come out of the model function. If num_results is greater than 1, it should be indexed by range(num_results) |
1
|
is_wandb
|
bool
|
whether wandb experiment tracking is enabled |
False
|
wandb_project
|
str
|
the string name of the wandb project |
''
|
wandb_entity
|
str
|
the wandb user |
''
|
args
|
dict
|
args to add the the wandb config |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
an np.array of shape (trials, lr_range, models, num_results) with the results all filled in |
Source code in ginjax/ml/training.py
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 | |