WFORunner
Source code in wt_ml/tuning/wfo_runner.py
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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 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 474 475 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 523 524 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 631 632 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 718 719 720 721 722 723 724 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 822 823 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 |
|
val_freq: Frequency | None
property
Frequency of WFO periods. If None, custom validation periods is used.
__init__(dataset, model_builder, val_freq=None, min_val_date=None, max_val_date=None, start_date=None, custom_val_periods=None, model_name_prefix='Model')
WFO object that creates periods and runs walk forward optimization.
Either pass in val_freq or custom_val_periods to create wfo periods.
The model_builder is used to create the model used for train_test_model
.
This is separated from WFO, so that it's cleaner and makes it easier during parallelization.
You can use build_model
from wt_ml.networks.model
and create a partial function from it assigning
hyperparameters
and net_combination.
from functools import partial
from wt_ml.networks.model import build_model
model_builder = partial(
_build_model,
hyperparameters=hyperparameters,
net_combination=net_combination,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
EconomicDataset
|
The dataset object. |
required |
model_builder |
ModelBuilder
|
Function that creates the model and has the ModelBuilder signature. |
required |
val_freq |
Frequency | str | None
|
Validation period frequency. Defaults to None. |
None
|
min_val_date |
str | datetime64 | None
|
Minimum validation week to include in periods. If None, ensures that there is at least 1 period to train on. Defaults to None. |
None
|
max_val_date |
str | datetime64 | None
|
Maximum validation week to include in periods. Defaults to None. |
None
|
start_date |
str | datetime64 | None
|
The start date after which we start training. Defaults to None. |
None
|
custom_val_periods |
dict[str, ValidationPeriod] | Sequence[ValidationPeriod] | None
|
Provide custom validation periods instead of generating from |
None
|
model_name_prefix |
str
|
Prefix name given when we build the model. Defaults to "Model". |
'Model'
|
Source code in wt_ml/tuning/wfo_runner.py
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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
calculate_curves_diff(results, return_df=False, level=('brand', 'wholesaler'))
Calculates curve differences between consecutive periods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results |
dict[str, TrainTestOutput]
|
Results of train_test_model for each period. |
required |
return_df |
bool
|
Return the results as a dataframe. |
False
|
level |
tuple[str, ...] | str | None
|
The level at which we want to aggregate.
NOTE: This is not implemented yet. Don't change from defaults.
'country' will aggregate it to all. |
('brand', 'wholesaler')
|
Returns:
Type | Description |
---|---|
dict[str, NDArray] | NDArray | DataFrame
|
dict[str, NDArray] | NDArray: curve diff calculated on each curve and agg on periods. |
Source code in wt_ml/tuning/wfo_runner.py
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 822 823 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 |
|
calculate_metrics(results, mask=None, weights=None, level=('brand', 'wholesaler'), calculate_custom_metrics=False)
Calculate metrics for WFO period results.
Internally calls calculate_metrics
in wt_ml.tuning.train_test_model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results |
dict[str, TrainTestOutput]
|
Results of train_test_model for each period. |
required |
mask |
dict[str, NDArray[bool_]] | None
|
Mask tensor of the same shape as y_true and y_pred indicating which elements to mask out for each period. Default is None. |
None
|
weights |
dict[str, NDArray[float_]] | None | Literal['auto']
|
Weights used for taking weighted
mean on the metrics for each period. 'auto' will pick the |
None
|
level |
tuple[str, ...] | str | None
|
The level at which we want to aggregate.
'country' will aggregate it to all. |
('brand', 'wholesaler')
|
calculate_custom_metrics |
bool
|
Calculate custom metrics as well. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
dict[str, GroupedMetrics | dict[str, GroupedMetrics]]
|
dict[str, GroupedMetrics]: |
Source code in wt_ml/tuning/wfo_runner.py
715 716 717 718 719 720 721 722 723 724 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 |
|
get_periods()
Creates the periods WFO will run on. For each Frequency type, periods are generated. NOTE: the periods are built from date encodings and not from date_index. This can cause mismatch if the dataset was subsetted on time. Ensure the validation periods provided are within the date_index in dataset. When custom_val_periods is set then we return custom_val_periods!
Returns:
Type | Description |
---|---|
dict[str, ValidationPeriod]
|
dict[str, ValidationPeriod]: Key is a unique name for a period. Contains validation range and start date. |
Source code in wt_ml/tuning/wfo_runner.py
467 468 469 470 471 472 473 474 475 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 |
|
metrics_to_df(period_metrics)
Convert period_metrics
to a dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
period_metrics |
dict[str, GroupedMetrics | dict[str, GroupedMetrics]]
|
Metrics calculated from self.calculate_metrics |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Metrics DataFrame. |
Source code in wt_ml/tuning/wfo_runner.py
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 |
|
run(epochs, *, validation_periods=None, parallel=False, save_dir=DEFAULT_SAVE_DIR, include_stitched_period=True, no_return=False, callbacks_builder=None, options=WFOOptions(), **kwargs)
Run Walk forward optimization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epochs |
int
|
Number of epochs to train the model. |
required |
validation_periods |
dict[str, ValidationPeriod] | None
|
WFO periods WFO will run on.
If None, runs on |
None
|
parallel |
bool
|
Run WFO in parallel based on GPU devices. Defaults to False. |
False
|
save_dir |
Path | None
|
Directory to save the model. |
DEFAULT_SAVE_DIR
|
calculate_trackers |
bool
|
Calculate and include trackers in output. Defaults to False. |
required |
include_stitched_period |
bool
|
Include stitched results in output. Defaults to True. |
True
|
retrain |
bool
|
Train WFO from scratch or skipp training for stored models. |
required |
disk_mode |
bool
|
Do not return outputs. Defaults to False.
Should trigger |
required |
no_return |
bool
|
Do not return results. Special case for handling OOM. Defaults to False. |
False
|
callbacks_builder |
Callable[[], CallbacksList] | None
|
Function that returns CallbacksList |
None
|
smoothing_window |
bool
|
Smooth tail weeks data by appending additional weeks. Defaults to False. |
required |
partial_checkpoint_enabled |
bool | Literal['resume'] | Path
|
For non parallel runs, load previous period model weights. If "resume" load existing initial period. If Path is provided load the given Path for first period only. Defaults to True. |
required |
Returns:
Type | Description |
---|---|
dict[str, TrainTestOutput] | None
|
dict[str, TrainTestOutput]: Results of train_test_model for each period. |
Source code in wt_ml/tuning/wfo_runner.py
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 631 632 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 |
|
calculate_metrics(output, val_dates_idx, encodings, mask=None, weights=None, level=('brand', 'wholesaler'), calculate_custom_metrics=False)
Calculate metrics for given output. If output is a dict, we recursively calculate metrics for each key in the dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
ModelOutputType
|
Outputs of the model. |
required |
val_dates_idx |
NDArray[int64]
|
The dates_index indices which is in validation period. |
required |
encodings |
dict[str, dict[str, int]]
|
Encodings dict which will be used to decode the index values. |
required |
mask |
NDArray[bool_] | None
|
Mask tensor of the same shape as y_true and y_pred indicating which elements to mask out. Default is None. |
None
|
weights |
NDArray[float_] | None
|
Weights used for taking weighted mean on the metrics. Defaults to None. |
None
|
level |
tuple[str, ...] | str | None
|
The level at which we want to aggregate it or the metrics correspond to.
'country' will aggregate it to all. |
('brand', 'wholesaler')
|
calculate_custom_metrics |
bool
|
Calculate custom metrics as well. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
GroupedMetrics |
GroupedMetrics | dict[str, GroupedMetrics]
|
|
Source code in wt_ml/tuning/train_test_model.py
426 427 428 429 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 474 475 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 |
|
concat_intermediaries(intermediaries, axis=0, axis_type=Axis.Batch)
Concatenates tf.experimental.ExtensionType objects in the batch_axis.
Make sure that both axis
and axis_type
matches else it leads to unexpected results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
intermediaries |
list[ExtensionType]
|
tf.experimental.ExtensionType type instance. |
required |
axis |
int
|
The axis where we need to concatenate. Defaults to 0 which is assumed to be batch. |
0
|
axis_type(Axis, |
optional
|
The annotated axis where we need to concatenate. Defaults to Axis.Batch. |
required |
Returns:
Type | Description |
---|---|
T
|
tf.experimental.ExtensionType: The concatenated output which is the same type that is passed in. |
Source code in wt_ml/tuning/utils.py
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 |
|
metrics_to_df(metrics_data, index_names=['dataset', 'metric'])
Convert given GroupedMetrics to a dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metrics_data |
GroupedMetrics | dict[str, GroupedMetrics]
|
Calculated GroupedMetrics. |
required |
index_names |
list
|
The index names for the dataframe. Defaults to ["dataset", "metric"]. The GroupedMetrics are flattened, so the index is usually (*any_parent_levels, 'dataset', 'metric'). |
['dataset', 'metric']
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Metrics pandas dataframe. |
Source code in wt_ml/tuning/train_test_model.py
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 |
|