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 |
|
index_flatten(index)
Flattens a given index to avoid pesky inner nested tuples.
Source code in wt_ml/tuning/train_test_model.py
533 534 535 |
|
load_model_result(test_output, val_dates_idx)
Convert dict output into mocked Model Intermediaries using a Namespace.
Source code in wt_ml/tuning/train_test_model.py
576 577 578 579 580 581 582 583 584 585 |
|
mask_smoothing_weeks(batch, num_head_weeks=0, num_tail_weeks=0)
Post processing step for batch data during iteration to mask out tail weeks.
Source code in wt_ml/tuning/train_test_model.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
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 |
|
train_test_model(val_period, full_dataset, model, epochs, start_date=None, verbosity=1, calculate_trackers=False, calculate_time=False, checkpoint_freq=None, callbacks_builder=None, save_dir=None, smoothing_window=False, delete_existing_checkpoints=False, **kwargs)
Runs model on train Time period and then validates on train and test Time period.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val_period |
tuple[str, str, ...] | NDArray[datetime64]
|
The inclusive validation period. |
required |
full_dataset |
EconomicDataset
|
The dataset object. We will subset train & test periods from this. |
required |
model |
TrainableModule
|
The TrainableModule model. |
required |
epochs |
int
|
Number of epochs to train the model. |
required |
start_date |
str | datetime64 | None
|
The start date after which we start training. Defaults to None which is the first date. |
None
|
verbosity |
int
|
Verbosity level. Defaults to 1. |
1
|
calculate_trackers |
bool
|
Calculate and include trackers in output. Defaults to False. |
False
|
calculate_time |
bool
|
Calculate model training time. Defaults to False. |
False
|
checkpoint_freq |
int | Sequence[int] | None
|
Freq at which checkpoints are created. |
None
|
callbacks_builder |
Callable[[], CallbacksList] | None
|
Function that returns CallbacksList which will be appended with LearningCurveCallback |
None
|
save_dir |
Path | None
|
Directory to save the learning curve. |
None
|
smoothing_window |
bool
|
Whether to smooth the validation period. Defaults to False. |
False
|
delete_existing_checkpoints |
bool
|
Delete previous checkpoints. Defaults to False. |
False
|
**kwargs |
Keyword arguments passed into |
{}
|
Returns:
Name | Type | Description |
---|---|---|
TrainTestOutput |
TrainTestOutput | Tuple[TrainTestOutput, float]
|
Returns the full model intermediaries and validation date idx. |
float |
optional
|
Returns the model training time. |
Source code in wt_ml/tuning/train_test_model.py
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 302 303 304 305 306 307 308 |
|