This part of the project documentation focuses on
an information-oriented approach. Use it as a
reference for the technical implementation of the
mlpForecaster
project code.
MLPForecast
MLPForecast(
hparams: dict,
exp_name: str = "Tanesco",
file_name: str = None,
seed: int = 42,
root_dir: str = "../",
trial=None,
metric: str = "val_mae",
max_epochs: int = 10,
wandb: bool = False,
model_type: str = "MLPF",
gradient_clip_val: float = 10.0,
rich_progress_bar: bool = True,
)
Bases: PytorchForecast
MLP Forecasting class for managing training, evaluation, and prediction.
Attributes:
-
hparams
(dict
) –Hyperparameters for the MLP model.
-
model
(MLPForecastModel
) –PyTorch model.
-
train_df
(DataFrame
) –Training DataFrame.
-
validation_df
(DataFrame
) –Validation DataFrame.
Parameters:
-
hparams
(dict
) –Hyperparameters for the MLP model.
-
exp_name
(str
, default:'Tanesco'
) –Experiment name. Defaults to "Tanesco".
-
file_name
(str
, default:None
) –Name of the file for logging and saving checkpoints. Defaults to None.
-
seed
(int
, default:42
) –Random seed for reproducibility. Defaults to 42.
-
root_dir
(str
, default:'../'
) –Root directory for the project. Defaults to "../".
-
trial
(trial
, default:None
) –Optuna trial object for hyperparameter optimization. Defaults to None.
-
metric
(str
, default:'val_mae'
) –Metric to monitor during training. Defaults to "val_mae".
-
max_epochs
(int
, default:10
) –Maximum number of epochs for training. Defaults to 10.
-
wandb
(bool
, default:False
) –Whether to use Weights and Biases for logging. Defaults to False.
-
model_type
(str
, default:'MLPF'
) –Type of the model. Defaults to "MLPF".
-
gradient_clip_val
(float
, default:10.0
) –Value for gradient clipping. Defaults to 10.0.
-
rich_progress_bar
(bool
, default:True
) –Whether to use rich progress bar. Defaults to True.
Source code in mlpforecast/forecaster/mlp.py
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 |
|
auto_tune
auto_tune(
train_df,
val_df,
num_trial=10,
reduction_factor=3,
patience=2,
)
Perform hyperparameter tuning using Optuna.
Parameters:
-
train_df
(DataFrame
) –Training DataFrame.
-
val_df
(DataFrame
) –Validation DataFrame.
-
num_trial
(int
, default:10
) –Number of trials for hyperparameter optimization. Defaults to 10.
-
reduction_factor
(int
, default:3
) –Reduction factor for Hyperband pruner. Defaults to 3.
-
patience
(int
, default:2
) –Patience for the Patient pruner. Defaults to
Source code in mlpforecast/forecaster/mlp.py
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 |
|
create_results_df
create_results_df(
time_stamp,
ground_truth,
predictions,
target_series,
date_column,
)
Creates a DataFrame with the timestamp index and populates it with ground truth and forecasted values.
Parameters:
-
time_stamp
(array
) –The timestamp index.
-
ground_truth
(array
) –The ground truth values.
-
predictions
(array
) –The forecasted values.
-
target_series
(list
) –A list of target series names.
-
date_column
(str
) –The name of the date column.
Returns:
-
results_df
(DataFrame
) –A DataFrame containing the ground truth and forecasted values, indexed by timestamp.
Source code in mlpforecast/forecaster/common.py
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 |
|
evaluate_point_forecast
evaluate_point_forecast(ground_truth, pred, time_stamp)
Evaluates the point forecast.
Parameters:
-
ground_truth
(array
) –The ground truth values.
-
pred
(array
) –The forecasted values.
-
time_stamp
(array
) –The timestamp index.
Returns:
-
dict
–A dictionary containing the evaluation metrics.
Source code in mlpforecast/forecaster/common.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
fit
fit(
train_df,
val_df=None,
train_ratio=0.8,
drop_last=True,
num_worker=1,
batch_size=64,
pin_memory=True,
)
Fit the model using the provided training DataFrame.
Parameters:
-
train_df
(DataFrame
) –The training data.
-
val_df
(DataFrame
, default:None
) –The validation data. If None, will split train_df based on train_ratio.
-
train_ratio
(float
, default:0.8
) –Proportion of data to use for training. Default is 0.80.
-
drop_last
(bool
, default:True
) –Whether to drop the last incomplete batch. Default is True.
-
num_worker
(int
, default:1
) –Number of workers for data loading. Default is 1.
-
batch_size
(int
, default:64
) –Size of each batch for training. Default is 64.
-
pin_memory
(bool
, default:True
) –If True, the data loader will copy Tensors into CUDA pinned memory. Default is True.
Returns:
-
float
–The training wall time or cost metric based on the training configuration.
Raises:
-
ValueError
–If the model instance is not initialized.
Source code in mlpforecast/forecaster/common.py
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
get_search_params
get_search_params(trial: Trial) -> dict
Define the search space for hyperparameter optimization using Optuna.
Parameters:
-
trial
(Trial
) –An Optuna trial object to suggest parameters.
Returns:
-
dict
(dict
) –A dictionary containing suggested hyperparameters.
Source code in mlpforecast/forecaster/mlp.py
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 |
|
load_and_prepare_data
load_and_prepare_data(
test_df: DataFrame, daily_feature: str
)
Loads the checkpoint and prepares the ground truth data.
Parameters:
-
test_df
(DataFrame
) –The test DataFrame containing the input features for prediction.
-
daily_feature
(str
) –The daily feature to use in the model.
Returns:
-
ground_truth
(DataFrame
) –A DataFrame containing the ground truth data.
Source code in mlpforecast/forecaster/common.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
load_checkpoint
load_checkpoint()
Load the latest checkpoint for the model.
This method retrieves the path of the latest checkpoint and loads the model from it.
Source code in mlpforecast/forecaster/mlp.py
76 77 78 79 80 81 82 83 84 |
|
perform_prediction
perform_prediction(test_df: DataFrame)
Performs the model prediction.
Parameters:
-
test_df
(DataFrame
) –The test DataFrame containing the input features for prediction.
Returns:
-
dict
–A dictionary containing the forecasted values.
Source code in mlpforecast/forecaster/common.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
predict
predict(
test_df=None, covariate_df=None, daily_feature=True
)
Perform prediction on the test DataFrame and return a DataFrame with ground truth and forecasted values.
Parameters:
-
test_df
(DataFrame
, default:None
) –The test DataFrame containing the input features for prediction.
-
daily_feature
(bool
, default:True
) –Flag indicating whether daily features are used in the model. Default is True.
Returns:
-
results_df
(DataFrame
) –A DataFrame containing the ground truth and forecasted values, indexed by timestamp.
Source code in mlpforecast/forecaster/common.py
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 |
|
PytorchForecast
PytorchForecast(
exp_name="Tanesco",
file_name=None,
seed=42,
root_dir="../",
trial=None,
metric="val_mae",
max_epochs=10,
wandb=False,
model_type="MLPF",
rich_progress_bar=False,
gradient_clip_val=10,
)
PytorchForecast class for setting up and managing the training process of a PyTorch model using PyTorch Lightning.
Attributes:
-
exp_name
(str
) –The name of the experiment.
-
file_name
(str
) –The name of the file to save the logs and model checkpoints.
-
seed
(int
) –The seed for random number generation to ensure reproducibility.
-
root_dir
(str
) –The root directory for saving logs and checkpoints.
-
trial
(Trial
) –Optuna trial for hyperparameter optimization.
-
metric
(str
) –The metric to monitor for early stopping and model checkpointing.
-
max_epochs
(int
) –The maximum number of training epochs.
-
wandb
(bool
) –Flag to use Wandb logger instead of TensorBoard logger.
-
rich_progress_bar
(bool
) –Flag to use rich progress bar for training visualization.
Parameters:
-
exp_name
(str
, default:'Tanesco'
) –The name of the experiment. Defaults to "Tanesco".
-
file_name
(str
, default:None
) –The name of the file to save the logs and model checkpoints. Defaults to None.
-
seed
(int
, default:42
) –The seed for random number generation. Defaults to 42.
-
root_dir
(str
, default:'../'
) –The root directory for saving logs and checkpoints. Defaults to "../".
-
trial
(Trial
, default:None
) –Optuna trial for hyperparameter optimization. Defaults to None.
-
metric
(str
, default:'val_mae'
) –The metric to monitor for early stopping and model checkpointing. Defaults to "val_mae".
-
max_epochs
(int
, default:10
) –The maximum number of training epochs. Defaults to 10.
-
wandb
(bool
, default:False
) –Flag to use Wandb logger instead of TensorBoard logger. Defaults to False.
-
rich_progress_bar
(bool
, default:False
) –Flag to use rich progress bar for training visualization. Defaults to True.
Source code in mlpforecast/forecaster/common.py
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 |
|
create_results_df
create_results_df(
time_stamp,
ground_truth,
predictions,
target_series,
date_column,
)
Creates a DataFrame with the timestamp index and populates it with ground truth and forecasted values.
Parameters:
-
time_stamp
(array
) –The timestamp index.
-
ground_truth
(array
) –The ground truth values.
-
predictions
(array
) –The forecasted values.
-
target_series
(list
) –A list of target series names.
-
date_column
(str
) –The name of the date column.
Returns:
-
results_df
(DataFrame
) –A DataFrame containing the ground truth and forecasted values, indexed by timestamp.
Source code in mlpforecast/forecaster/common.py
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 |
|
evaluate_point_forecast
evaluate_point_forecast(ground_truth, pred, time_stamp)
Evaluates the point forecast.
Parameters:
-
ground_truth
(array
) –The ground truth values.
-
pred
(array
) –The forecasted values.
-
time_stamp
(array
) –The timestamp index.
Returns:
-
dict
–A dictionary containing the evaluation metrics.
Source code in mlpforecast/forecaster/common.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
fit
fit(
train_df,
val_df=None,
train_ratio=0.8,
drop_last=True,
num_worker=1,
batch_size=64,
pin_memory=True,
)
Fit the model using the provided training DataFrame.
Parameters:
-
train_df
(DataFrame
) –The training data.
-
val_df
(DataFrame
, default:None
) –The validation data. If None, will split train_df based on train_ratio.
-
train_ratio
(float
, default:0.8
) –Proportion of data to use for training. Default is 0.80.
-
drop_last
(bool
, default:True
) –Whether to drop the last incomplete batch. Default is True.
-
num_worker
(int
, default:1
) –Number of workers for data loading. Default is 1.
-
batch_size
(int
, default:64
) –Size of each batch for training. Default is 64.
-
pin_memory
(bool
, default:True
) –If True, the data loader will copy Tensors into CUDA pinned memory. Default is True.
Returns:
-
float
–The training wall time or cost metric based on the training configuration.
Raises:
-
ValueError
–If the model instance is not initialized.
Source code in mlpforecast/forecaster/common.py
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
load_and_prepare_data
load_and_prepare_data(
test_df: DataFrame, daily_feature: str
)
Loads the checkpoint and prepares the ground truth data.
Parameters:
-
test_df
(DataFrame
) –The test DataFrame containing the input features for prediction.
-
daily_feature
(str
) –The daily feature to use in the model.
Returns:
-
ground_truth
(DataFrame
) –A DataFrame containing the ground truth data.
Source code in mlpforecast/forecaster/common.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
perform_prediction
perform_prediction(test_df: DataFrame)
Performs the model prediction.
Parameters:
-
test_df
(DataFrame
) –The test DataFrame containing the input features for prediction.
Returns:
-
dict
–A dictionary containing the forecasted values.
Source code in mlpforecast/forecaster/common.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
predict
predict(
test_df=None, covariate_df=None, daily_feature=True
)
Perform prediction on the test DataFrame and return a DataFrame with ground truth and forecasted values.
Parameters:
-
test_df
(DataFrame
, default:None
) –The test DataFrame containing the input features for prediction.
-
daily_feature
(bool
, default:True
) –Flag indicating whether daily features are used in the model. Default is True.
Returns:
-
results_df
(DataFrame
) –A DataFrame containing the ground truth and forecasted values, indexed by timestamp.
Source code in mlpforecast/forecaster/common.py
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 |
|
format_target
format_target(
targets,
input_window_size,
forecast_horizon,
daily_feature=True,
)
Format the target data for training the model.
Parameters:
-
targets
(DataFrame
) –Target data.
-
input_window_size
(int
) –Input window size.
-
forecast_horizon
(int
) –Forecast horizon.
-
daily_feature
(bool
, default:True
) –Whether to use daily features. Defaults to True.
Returns:
-
ndarray
–Formatted target data.
Source code in mlpforecast/forecaster/utils.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
get_latest_checkpoint
get_latest_checkpoint(checkpoint_path)
Get the path of the latest checkpoint file.
Parameters:
-
checkpoint_path
(str
) –Path to the directory containing the checkpoint files.
Returns:
-
latest_file
(str
) –Path of the latest checkpoint file.
Source code in mlpforecast/forecaster/utils.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|