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.
MLPForecastModel
MLPForecastModel(
data_pipeline=None,
target_series: list[str] | str = ["NetLoad"],
unknown_features: list[str] = [],
calendar_variables: list[str] = [],
known_calendar_features: list[str] = [],
known_continuous_features: list[str] = [],
input_window_size: int = 96,
forecast_horizon: int = 48,
embedding_size: int = 28,
embedding_type: str = None,
combination_type: str = "addition-comb",
hidden_size: int = 64,
num_layers: int = 2,
expansion_factor: int = 2,
residual: bool = False,
activation_function: str = "ReLU",
out_activation_function: str = "Identity",
dropout_rate: float = 0.25,
alpha: float = 0.1,
num_attention_heads: int = 4,
metric: str = "mae",
learning_rate: float = 0.001,
weight_decay: float = 1e-06,
prob_decay_1: float = 0.75,
prob_decay_2: float = 0.9,
gamma: float = 0.01,
max_epochs: int = 10,
)
Bases: BaseForecastModel
MLP Forecast Model for time series point forecasting.
Attributes:
-
n_out
(int
) –Number of output series.
-
n_channels
(int
) –Number of input channels.
-
model
(object
) –Model object.
-
hparams
(dict
) –Hyperparameters for the model.
Parameters:
-
data_pipeline
(object
, default:None
) –Data pipeline object containing the series and features. Defaults to None.
-
embedding_size
(int
, default:28
) –Dimensionality of the embedding space. Defaults to 28.
-
embedding_type
(str
, default:None
) –Type of embedding to use.\ Options: 'PosEmb', 'RotaryEmb', 'CombinedEmb'. Defaults to None.
-
combination_type
(str
, default:'addition-comb'
) –Type of combination to use. Options: \ 'attn-comb', 'weighted-comb', 'addition-comb'. Defaults to 'attn-comb'.
-
hidden_size
(int
, default:64
) –Dimensionality of the hidden layers. Defaults to 64.
-
num_layers
(int
, default:2
) –Number of layers in the MLP. Defaults to 2.
-
expansion_factor
(int
, default:2
) –Factor to expand the size of layers. Defaults to 2.
-
residual
(bool
, default:False
) –Whether to use residual connections. Defaults to False.
-
activation_function
(str
, default:'ReLU'
) –\ Activation function to use in the hidden layers. Defaults to "ReLU".
-
out_activation_function
(str
, default:'Identity'
) –Activation function to use in the output layer. \ Defaults to "Identity".
-
dropout_rate
(float
, default:0.25
) –Dropout rate for regularization. Defaults to 0.25.
-
alpha
(float
, default:0.1
) –Alpha parameter for the loss function. Defaults to 0.1.
-
num_attention_heads
(int
, default:4
) –Number of attention heads. Defaults to 4.
-
metric
(str
, default:'mae'
) –Metric to evaluate the model. Defaults to "mae".
-
learning_rate
(float
, default:0.001
) –Learning rate for the optimizer. Defaults to 1e-3.
-
weight_decay
(float
, default:1e-06
) –Weight decay for the optimizer. Defaults to 1e-6.
-
prob_decay_1
(float
, default:0.75
) –First probability decay rate. Defaults to 0.75.
-
prob_decay_2
(float
, default:0.9
) –Second probability decay rate. Defaults to 0.9.
-
gamma
(float
, default:0.01
) –Gamma parameter. Defaults to 0.01.
-
max_epochs
(int
, default:10
) –Maximum number of epochs for training. Defaults to 10.
Example
kwargs = { 'data_pipeline': data_pipeline, 'embedding_size': 20, 'embedding_type': None, 'combination_type': 'Add', 'hidden_size': 64, 'num_layers': 2, 'activation_function': 'ReLU', 'out_activation_function': 'ReLU', 'dropout_rate': 0.25, 'alpha': 0.25, 'num_attention_heads': 4, 'metric': 'smape', 'learning_rate': 1e-4, 'weight_decay': 1e-6, 'prob_decay_1': 0.75, 'prob_decay_2': 0.9, 'gamma': 0.01, 'max_epochs': 50 }
model = MLPForecastModel(**kwargs)
Source code in mlpforecast/model/deterministic.py
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 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 |
|
configure_optimizers
configure_optimizers()
Configure optimizers and learning rate schedulers.
Returns:
-
tuple
–A tuple containing the optimizer and the scheduler.
Source code in mlpforecast/model/deterministic.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
forecast
forecast(x)
Generate forecast for the given input.
Parameters:
-
x
(tensor
) –Input data for forecasting.
Returns:
-
tensor
–Forecasted values.
Source code in mlpforecast/model/deterministic.py
135 136 137 138 139 140 141 142 143 144 145 |
|
forward
forward(x)
Forward pass of the model.
Parameters:
-
x
(tensor
) –Input data.
Source code in mlpforecast/model/deterministic.py
148 149 150 151 152 153 154 155 |
|
on_load_checkpoint
on_load_checkpoint(checkpoint)
Load the data pipeline from a file.
Parameters:
-
checkpoint
(dict
) –Checkpoint dictionary.
Source code in mlpforecast/model/base_model.py
79 80 81 82 83 84 85 86 |
|
on_save_checkpoint
on_save_checkpoint(checkpoint)
Save the data pipeline to a file and add the file path to the checkpoint dictionary.
Parameters:
-
checkpoint
(dict
) –Checkpoint dictionary.
Source code in mlpforecast/model/base_model.py
66 67 68 69 70 71 72 73 74 75 76 77 |
|
training_step
training_step(batch, batch_idx)
Perform a single training step.
Parameters:
Returns:
-
tensor
–The loss value for the batch.
Source code in mlpforecast/model/deterministic.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
validation_step
validation_step(batch, batch_idx)
Perform a single validation step.
Parameters:
Returns:
-
tensor
–The loss value for the batch.
Source code in mlpforecast/model/deterministic.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
BaseForecastModel
BaseForecastModel(data_pipeline=None, metric='mae')
Bases: LightningModule
Base class for all forecasting models.
Attributes:
-
model
(Module
) –PyTorch model.
-
data_pipeline
(Pipeline
) –Data pipeline.
-
tra_metric_fcn
(Metric
) –Training metric function.
-
val_metric_fcn
(Metric
) –Validation metric function.
-
size
(float
) –Model size in MB.
-
checkpoint_path
(str
) –Path to save checkpoints.
Parameters:
-
data_pipeline
(Pipeline
, default:None
) –Data pipeline.
-
metric
(str
, default:'mae'
) –Metric to use for evaluation. Options: 'mae', 'mse', 'smape'.
Source code in mlpforecast/model/base_model.py
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 |
|
on_load_checkpoint
on_load_checkpoint(checkpoint)
Load the data pipeline from a file.
Parameters:
-
checkpoint
(dict
) –Checkpoint dictionary.
Source code in mlpforecast/model/base_model.py
79 80 81 82 83 84 85 86 |
|
on_save_checkpoint
on_save_checkpoint(checkpoint)
Save the data pipeline to a file and add the file path to the checkpoint dictionary.
Parameters:
-
checkpoint
(dict
) –Checkpoint dictionary.
Source code in mlpforecast/model/base_model.py
66 67 68 69 70 71 72 73 74 75 76 77 |
|