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.
MLPBlock
MLPBlock(
in_size=1,
latent_dim=32,
features_start=16,
expansion_factor=1,
residual=False,
num_layers=4,
context_size=96,
activation=nn.ReLU(),
bn=True,
)
Bases: Module
Multi-Layer Perceptron (MLP) block with configurable layers and options.
Attributes:
-
mlp_network
(ModuleList
) –List of layers in the MLP network.
-
in_size
(int
) –Size of the input after flattening.
-
context_size
(int
) –Size of the context.
-
residual
(bool
) –If True, adds residual connections.
Parameters:
-
in_size
(int
, default:1
) –Size of the input. Defaults to 1.
-
latent_dim
(int
, default:32
) –Dimensionality of the latent space. Defaults to 32.
-
features_start
(int
, default:16
) –Number of features in the initial layer. Defaults to 16.
-
num_layers
(int
, default:4
) –Number of layers in the MLP. Defaults to 4.
-
context_size
(int
, default:96
) –Size of the context. Defaults to 96.
-
activation
(Module
, default:ReLU()
) –Activation function. Defaults to ReLU().
-
bn
(bool
, default:True
) –If True, adds batch normalization. Defaults to True.
Source code in mlpforecast/net/layers.py
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
forward
forward(x)
Forward pass of the MLP block.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
Tensor
–Output tensor after passing through the MLP block.
Source code in mlpforecast/net/layers.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
MLPForecastNetwork
MLPForecastNetwork(
n_target_series: int,
n_unknown_features: int,
n_known_calendar_features: int,
n_known_continuous_features: int,
embedding_size: int = 28,
embedding_type: str = None,
combination_type: str = "attn-comb",
expansion_factor: int = 2,
residual: bool = False,
hidden_size: int = 256,
num_layers: int = 2,
forecast_horizon: int = 48,
input_window_size: int = 96,
activation_function: str = "SiLU",
out_activation_function: str = "Identity",
dropout_rate: float = 0.25,
alpha: float = 0.1,
num_attention_heads: int = 4,
)
Bases: Module
Multilayer Perceptron (MLP) Forecast Network for time series forecasting.
Attributes:
-
n_out
(int
) –Number of target series.
-
n_unknown
(int
) –Number of unknown time-varying features.
-
n_covariates
(int
) –Number of known time-varying features.
-
n_channels
(int
) –Number of channels in the input.
-
input_window_size
(int
) –Size of the input window.
-
forecast_horizon
(int
) –Number of future time steps to forecast.
-
out_activation
(Module
) –Output activation function.
-
activation
(Module
) –Activation function.
-
encoder
(PastFutureEncoder
) –Encoder module.
-
horizon
(PastFutureEncoder
) –Horizon encoder module.
-
combination_type
(str
) –Type of combination to use.
-
alpha
(float
) –Alpha parameter for the loss.
-
attention
(MultiheadAttention
) –Multi-head attention module.
-
gate
(Linear
) –Linear layer for weighted combination.
-
decoder
(Sequential
) –Decoder module.
-
mu
(Linear
) –Linear layer for output.
Parameters:
-
n_target_series
(int
) –Number of target series.
-
n_unknown_features
(int
) –Number of unknown time-varying features.
-
n_known_calendar_features
(int
) –Number of known categorical time-varying features.
-
n_known_continuous_features
(int
) –Number of known continuous time-varying features.
-
embedding_size
(int
, default:28
) –Dimensionality of the embedding space. Defaults to 28.
-
embedding_type
(str
, default:None
) –Type of embedding to use. Defaults to None. Options: 'PosEmb', 'RotaryEmb', 'CombinedEmb'.
-
combination_type
(str
, default:'attn-comb'
) –Type of combination to use.Defaults to 'attn-comb'. Options: 'attn-comb', 'weighted-comb', 'addition-comb'.
-
expansion_factor
(int
, default:2
) –Expansion factor for the encoder. Defaults to 2.
-
residual
(bool
, default:False
) –Whether to use residual connections in the encoder. Defaults to False.
-
hidden_size
(int
, default:256
) –Dimensionality of the hidden layers. Defaults to 256.
-
num_layers
(int
, default:2
) –Number of layers in the MLP. Defaults to 2.
-
forecast_horizon
(int
, default:48
) –Number of future time steps to forecast. Defaults to 48.
-
input_window_size
(int
, default:96
) –Size of the input window. Defaults to 96.
-
activation_function
(str
, default:'SiLU'
) –Activation function. Defaults to 'SiLU'.
-
out_activation_function
(str
, default:'Identity'
) –Output activation function. Defaults to 'Identity'.
-
dropout_rate
(float
, default:0.25
) –Dropout probability. Defaults to 0.25.
-
alpha
(float
, default:0.1
) –Alpha parameter for the loss. Defaults to 0.1.
-
num_attention_heads
(int
, default:4
) –Number of heads in the multi-head attention. Defaults to 4.
Source code in mlpforecast/net/layers.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 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 |
|
forecast
forecast(x: Tensor) -> dict
Generates forecasts for the input sequences.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
dict
(dict
) –Dictionary containing the forecast predictions.
Source code in mlpforecast/net/layers.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
forward
forward(x: Tensor) -> Tensor
Forward pass of the MLPForecastNetwork.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
Tensor
–torch.Tensor: Output tensor after processing through the network.
Source code in mlpforecast/net/layers.py
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 |
|
step
Training step for the MLPForecastNetwork.
Parameters:
-
batch
(tuple
) –Tuple containing input and target tensors.
-
metric_fn
(callable
) –Metric function to evaluate.
Returns:
-
tuple
(tuple
) –Tuple containing the loss and computed metric.
Source code in mlpforecast/net/layers.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
|
PastFutureEncoder
PastFutureEncoder(
embedding_size: int = 28,
embedding_type: str = None,
latent_size: int = 64,
num_layers: int = 2,
residual: bool = False,
expansion_factor: int = 2,
context_size: int = 96,
activation: Module = nn.ReLU(),
dropout_rate: float = 0.25,
n_channels: int = 1,
)
Bases: Module
Encoder module for the PastFutureNetwork.
Attributes:
-
encoder
(MLPBlock
) –MLP block for the encoder.
-
norm
(LayerNorm
) –Layer normalization.
-
dropout
(Dropout
) –Dropout layer.
-
embedding
(Module
) –Embedding layer.
-
embedding_type
(str
) –Type of embedding to use.
-
rotary_embedding
(RotaryEmbedding
) –Rotary positional embedding.
-
pos_embedding
(PosEmbedding
) –Positional embedding.
Parameters:
-
embedding_size
(int
, default:28
) –Dimensionality of the embedding space. Defaults to 28.
-
embedding_type
(str
, default:None
) –Type of embedding to use. Defaults to None.
-
latent_size
(int
, default:64
) –Dimensionality of the latent space. Defaults to 64.
-
num_layers
(int
, default:2
) –Number of layers in the encoder. Defaults to 2.
-
residual
(bool
, default:False
) –Whether to use residual connections in the encoder. Defaults to False.
-
expansion_factor
(int
, default:2
) –Expansion factor for the encoder. Defaults to 2.
-
context_size
(int
, default:96
) –Size of the context. Defaults to 96.
-
activation
(Module
, default:ReLU()
) –Activation function. Defaults to nn.ReLU().
-
dropout_rate
(float
, default:0.25
) –Dropout probability. Defaults to 0.25.
-
n_channels
(int
, default:1
) –Number of channels in the input. Defaults
Source code in mlpforecast/net/layers.py
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
forward
forward(x: Tensor) -> Tensor
Forward pass of the PastFutureEncoder module.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
Tensor
–torch.Tensor: Output tensor after processing through the encoder.
Source code in mlpforecast/net/layers.py
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 |
|
FeedForward
FeedForward(
dim,
expansion_factor=2,
dropout=0.0,
activation=nn.GELU(),
bn=True,
)
Creates a feedforward block composed of linear layers, activation function, and dropout.
Parameters:
-
dim
(int
) –Dimensionality of the input.
-
expansion_factor
(int
, default:2
) –Expansion factor for the intermediate hidden layer. Defaults to 2.
-
dropout
(float
, default:0.0
) –Dropout probability. Defaults to 0.0 (no dropout).
-
activation
(Module
, default:GELU()
) –Activation function. Defaults to GELU().
-
bn
(bool
, default:True
) –If True, adds batch normalization. Defaults to True.
Returns:
-
Sequential
–Feedforward block.
Source code in mlpforecast/net/layers.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
create_linear
create_linear(in_channels, out_channels, bn=False)
Creates a linear layer with optional batch normalization.
Parameters:
-
in_channels
(int
) –Number of input channels.
-
out_channels
(int
) –Number of output channels.
-
bn
(bool
, default:False
) –If True, adds batch normalization. Defaults to False.
Returns:
-
Module
–Linear layer with optional batch normalization.
Source code in mlpforecast/net/layers.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 |
|
PosEmbedding
PosEmbedding(n_channels, d_model, window_size)
Bases: Module
Positional Embedding module that combines convolutional and sinusoidal embeddings.
Attributes:
-
emb
(Conv1DLayer
) –Convolutional positional embedding module.
-
d_model
(int
) –Dimension of the model.
Parameters:
-
n_channels
(int
) –Number of input channels.
-
d_model
(int
) –Dimension of the model.
-
window_size
(int
) –Size of the input window
Source code in mlpforecast/net/embending.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
forward
forward(x)
Forward pass of the PosEmbedding module.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
Tensor
–Output tensor after applying positional embedding.
Source code in mlpforecast/net/embending.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
Rotary
Rotary(dim, base=10000)
Bases: Module
Rotary positional embedding module.
Attributes:
-
seq_len_cached
(int
) –Cached sequence length.
-
cos_cached
(Tensor
) –Cached cosine values.
-
sin_cached
(Tensor
) –Cached sine values.
Parameters:
-
dim
(int
) –Dimension of the input embeddings.
-
base
(int
, default:10000
) –Base value for frequency calculation. Defaults to 10000.
Source code in mlpforecast/net/embending.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
forward
forward(inputs, seq_dim=1)
Forward pass of the rotary positional embedding module.
Parameters:
-
inputs
(Tensor
) –Input tensor.
-
seq_dim
(int
, default:1
) –Dimension representing the sequence length. Defaults to 1.
Returns:
-
Tensor
–Rotary positional embeddings.
Source code in mlpforecast/net/embending.py
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 |
|
RotaryEmbedding
RotaryEmbedding(d_model)
Bases: Module
Rotary Embedding module.
Attributes:
-
emb
(Rotary
) –Rotary positional embedding module.
Parameters:
-
d_model
(int
) –Dimension of the model.
Source code in mlpforecast/net/embending.py
182 183 184 185 186 187 188 189 190 191 |
|
forward
forward(x)
Forward pass of the RotaryEmbedding module.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
Tensor
–Output tensor after applying rotary embedding.
Source code in mlpforecast/net/embending.py
194 195 196 197 198 199 200 201 202 203 204 205 |
|
Conv1DLayer
Conv1DLayer(in_channels, out_channels, bias=True)
Creates a 1D convolutional layer with specified input and output channels.
Parameters:
-
in_channels
(int
) –Number of input channels.
-
out_channels
(int
) –Number of output channels.
-
bias
(bool
, default:True
) –If True, adds a learnable bias to the output. Default is True.
Returns:
-
Module
–1D convolutional layer.
Source code in mlpforecast/net/embending.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
rotate_half
rotate_half(x)
Rotate the input tensor along the last dimension by half.
Parameters:
-
x
(Tensor
) –Input tensor.
Returns:
-
Tensor
–Rotated tensor.
Source code in mlpforecast/net/embending.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
sinusoids
sinusoids(length, channels, max_timescale=10000)
Returns sinusoids for positional embedding.
Parameters:
-
length
(int
) –Length of the sequence.
-
channels
(int
) –Number of channels in the positional embeddings. It should be an even number.
-
max_timescale
(int
, default:10000
) –Maximum timescale for the sinusoids. Defaults to 10000.
Returns:
-
Tensor
–Sinusoidal positional embeddings.
Source code in mlpforecast/net/embending.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|