Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Support exog features in TimeMixer #1088

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions nbs/models.timemixer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
"source": [
"#| export\n",
"\n",
"import numpy as np\n",
"\n",
"import torch\n",
"import torch.nn as nn\n",
"\n",
Expand Down Expand Up @@ -464,7 +462,6 @@
" down_sampling_window: int = 2,\n",
" down_sampling_method: str = 'avg',\n",
" use_norm: bool = True,\n",
" decoder_input_size_multiplier: float = 0.5,\n",
" loss = MAE(),\n",
" valid_loss = None,\n",
" max_steps: int = 1000,\n",
Expand Down Expand Up @@ -509,10 +506,6 @@
" lr_scheduler_kwargs=lr_scheduler_kwargs,\n",
" **trainer_kwargs)\n",
" \n",
" self.label_len = int(np.ceil(input_size * decoder_input_size_multiplier))\n",
" if (self.label_len >= input_size) or (self.label_len <= 0):\n",
" raise Exception(f'Check decoder_input_size_multiplier={decoder_input_size_multiplier}, range (0,1)')\n",
" \n",
" self.h = h\n",
" self.input_size = input_size\n",
" self.e_layers = e_layers\n",
Expand Down Expand Up @@ -659,9 +652,9 @@
" if self.channel_independence == 1:\n",
" B, T, N = x_enc.size()\n",
" x_mark_dec = x_mark_dec.repeat(N, 1, 1)\n",
" self.x_mark_dec = self.enc_embedding(None, x_mark_dec)\n",
" self.x_mark_dec = self.enc_embedding(None, x_mark_dec) \n",
" else:\n",
" self.x_mark_dec = self.enc_embedding(None, x_mark_dec)\n",
" self.x_mark_dec = self.enc_embedding(x_enc, x_mark_dec) #MODIFIED\n",
"\n",
" x_enc, x_mark_enc = self.__multi_scale_process_inputs(x_enc, x_mark_enc)\n",
"\n",
Expand Down Expand Up @@ -736,8 +729,8 @@
" futr_exog = windows_batch['futr_exog']\n",
"\n",
" if self.futr_exog_size > 0:\n",
" x_mark_enc = futr_exog[:,:self.input_size,:]\n",
" x_mark_dec = futr_exog[:,-(self.label_len+self.h):,:]\n",
" x_mark_enc = futr_exog[:, :, :self.input_size]\n",
" x_mark_dec = futr_exog[:, -(self.h):, :]\n",
marcopeix marked this conversation as resolved.
Show resolved Hide resolved
" else:\n",
" x_mark_enc = None\n",
" x_mark_dec = None\n",
Expand Down Expand Up @@ -878,6 +871,31 @@
"forecasts = fcst.predict(futr_df=Y_test_df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| eval: false\n",
"\n",
"Y_train_df = AirPassengersPanel[AirPassengersPanel.ds<AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 132 train\n",
"Y_test_df = AirPassengersPanel[AirPassengersPanel.ds>=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test\n",
"\n",
"model = TimeMixer(h=12,\n",
" input_size=24,\n",
" n_series=2,\n",
" futr_exog_list=['trend'],\n",
" loss=MSE(),\n",
" valid_loss=MAE(),\n",
" early_stop_patience_steps=3,\n",
" batch_size=32)\n",
"\n",
"fcst = NeuralForecast(models=[model], freq='M')\n",
"fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12)\n",
"forecasts = fcst.predict(futr_df=Y_test_df)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
19 changes: 5 additions & 14 deletions neuralforecast/models/timemixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
'PastDecomposableMixing', 'TimeMixer']

# %% ../../nbs/models.timemixer.ipynb 3
import numpy as np

import torch
import torch.nn as nn

Expand Down Expand Up @@ -386,7 +384,6 @@ def __init__(
down_sampling_window: int = 2,
down_sampling_method: str = "avg",
use_norm: bool = True,
decoder_input_size_multiplier: float = 0.5,
loss=MAE(),
valid_loss=None,
max_steps: int = 1000,
Expand All @@ -404,7 +401,7 @@ def __init__(
optimizer_kwargs=None,
lr_scheduler=None,
lr_scheduler_kwargs=None,
**trainer_kwargs,
**trainer_kwargs
):

super(TimeMixer, self).__init__(
Expand All @@ -431,15 +428,9 @@ def __init__(
optimizer_kwargs=optimizer_kwargs,
lr_scheduler=lr_scheduler,
lr_scheduler_kwargs=lr_scheduler_kwargs,
**trainer_kwargs,
**trainer_kwargs
)

self.label_len = int(np.ceil(input_size * decoder_input_size_multiplier))
if (self.label_len >= input_size) or (self.label_len <= 0):
raise Exception(
f"Check decoder_input_size_multiplier={decoder_input_size_multiplier}, range (0,1)"
)

self.h = h
self.input_size = input_size
self.e_layers = e_layers
Expand Down Expand Up @@ -618,7 +609,7 @@ def forecast(self, x_enc, x_mark_enc, x_mark_dec):
x_mark_dec = x_mark_dec.repeat(N, 1, 1)
self.x_mark_dec = self.enc_embedding(None, x_mark_dec)
else:
self.x_mark_dec = self.enc_embedding(None, x_mark_dec)
self.x_mark_dec = self.enc_embedding(x_enc, x_mark_dec) # MODIFIED

x_enc, x_mark_enc = self.__multi_scale_process_inputs(x_enc, x_mark_enc)

Expand Down Expand Up @@ -704,8 +695,8 @@ def forward(self, windows_batch):
futr_exog = windows_batch["futr_exog"]

if self.futr_exog_size > 0:
x_mark_enc = futr_exog[:, : self.input_size, :]
x_mark_dec = futr_exog[:, -(self.label_len + self.h) :, :]
x_mark_enc = futr_exog[:, :, : self.input_size]
x_mark_dec = futr_exog[:, -(self.h) :, :]
else:
x_mark_enc = None
x_mark_dec = None
Expand Down
Loading