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

Fix/autoformer #523

Merged
merged 5 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ You can preview changes in your local browser before pushing by using the `nbdev
### Building the library
The library is built using the notebooks contained in the `nbs` folder. If you want to make any changes to the library you have to find the relevant notebook, make your changes and then call:
```
nbdev_export
```
nbdev_export
```

### Linters
This project uses a couple of linters to validate different aspects of the code. Before opening a PR, please make sure that it passes all the linting tasks by following the next steps.
Expand Down
6 changes: 3 additions & 3 deletions nbs/examples/Getting_Started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@
" max_steps=100, # Number of steps to train\n",
" n_freq_downsample=[2, 1, 1]) # Downsampling factors for each stack output\n",
" ]\n",
"fcst = NeuralForecast(models=models, freq='M')\n",
"fcst.fit(df=Y_df)"
"nf = NeuralForecast(models=models, freq='M')\n",
"nf.fit(df=Y_df)"
]
},
{
Expand Down Expand Up @@ -243,7 +243,7 @@
}
],
"source": [
"Y_hat_df = fcst.predict()"
"Y_hat_df = nf.predict()"
]
},
{
Expand Down
16 changes: 11 additions & 5 deletions nbs/examples/LongHorizon_Probabilistic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
"outputs": [],
"source": [
"# Fit and predict\n",
"fcst = NeuralForecast(\n",
"nf = NeuralForecast(\n",
" models=models,\n",
" freq='15min')"
]
Expand All @@ -525,8 +525,8 @@
"outputs": [],
"source": [
"%%capture\n",
"fcst_df = fcst.cross_validation(df=Y_df, val_size=val_size,\n",
" test_size=test_size, n_windows=None)"
"Y_hat_df = nf.cross_validation(df=Y_df, val_size=val_size,\n",
" test_size=test_size, n_windows=None)"
]
},
{
Expand All @@ -549,7 +549,7 @@
"metadata": {},
"outputs": [],
"source": [
"Y_hat_df = fcst_df.reset_index(drop=True)\n",
"Y_hat_df = Y_hat_df.reset_index(drop=True)\n",
"Y_hat_df = Y_hat_df[(Y_hat_df['unique_id']=='OT') & (Y_hat_df['cutoff']=='2018-02-11 12:00:00')]\n",
"Y_hat_df = Y_hat_df.drop(columns=['y','cutoff'])"
]
Expand Down Expand Up @@ -596,13 +596,19 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2021). N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting. Accepted at AAAI 2023.](https://arxiv.org/abs/2201.12886)"
"[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2021). NHITS: Neural Hierarchical Interpolation for Time Series Forecasting. Accepted at AAAI 2023.](https://arxiv.org/abs/2201.12886)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand Down
20 changes: 13 additions & 7 deletions nbs/examples/LongHorizon_with_NHITS.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,12 @@
],
"source": [
"%%capture\n",
"fcst = NeuralForecast(\n",
"nf = NeuralForecast(\n",
" models=models,\n",
" freq='15min')\n",
"\n",
"fcst_df = fcst.cross_validation(df=Y_df, val_size=val_size,\n",
" test_size=test_size, n_windows=None)"
"Y_hat_df = nf.cross_validation(df=Y_df, val_size=val_size,\n",
" test_size=test_size, n_windows=None)"
]
},
{
Expand Down Expand Up @@ -478,7 +478,7 @@
}
],
"source": [
"fcst.models[0].results.get_best_result().config"
"nf.models[0].results.get_best_result().config"
]
},
{
Expand All @@ -497,8 +497,8 @@
}
],
"source": [
"y_true = fcst_df.y.values\n",
"y_hat = fcst_df['AutoNHITS'].values\n",
"y_true = Y_hat_df.y.values\n",
"y_hat = Y_hat_df['AutoNHITS'].values\n",
"\n",
"n_series = len(Y_df.unique_id.unique())\n",
"\n",
Expand Down Expand Up @@ -601,13 +601,19 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2021). N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting. Accepted at AAAI 2023.](https://arxiv.org/abs/2201.12886)"
"[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2021). NHITS: Neural Hierarchical Interpolation for Time Series Forecasting. Accepted at AAAI 2023.](https://arxiv.org/abs/2201.12886)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand Down
6 changes: 3 additions & 3 deletions nbs/examples/Transfer_Learning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@
" batch_size = 32,\n",
" scaler_type='standard',\n",
" n_freq_downsample=[12,4,1])]\n",
"fcst = NeuralForecast(models=models, freq='M')\n",
"fcst.fit(df=Y_df)"
"nf = NeuralForecast(models=models, freq='M')\n",
"nf.fit(df=Y_df)"
]
},
{
Expand All @@ -176,7 +176,7 @@
"metadata": {},
"outputs": [],
"source": [
"fcst.save(path='./results/transfer/', model_index=None, overwrite=True, save_dataset=False)"
"nf.save(path='./results/transfer/', model_index=None, overwrite=True, save_dataset=False)"
]
},
{
Expand Down
15 changes: 8 additions & 7 deletions nbs/examples/UncertaintyIntervals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
" NHITS(input_size=7*horizon, h=horizon,\n",
" n_freq_downsample=[24, 12, 1],\n",
" loss=MQLoss(level=levels), max_epochs=200),]\n",
"fcst = NeuralForecast(models=models, freq='H')"
"nf = NeuralForecast(models=models, freq='H')"
]
},
{
Expand All @@ -288,7 +288,7 @@
"outputs": [],
"source": [
"%%capture\n",
"fcst.fit(df=Y_train_df)"
"nf.fit(df=Y_train_df)"
]
},
{
Expand Down Expand Up @@ -469,9 +469,9 @@
}
],
"source": [
"forecasts = fcst.predict()\n",
"forecasts = forecasts.reset_index()\n",
"forecasts.head()"
"Y_hat_df = nf.predict()\n",
"Y_hat_df = Y_hat_df.reset_index()\n",
"Y_hat_df.head()"
]
},
{
Expand All @@ -481,7 +481,7 @@
"metadata": {},
"outputs": [],
"source": [
"Y_test_df = Y_test_df.merge(forecasts, how='left', on=['unique_id', 'ds'])"
"Y_test_df = Y_test_df.merge(Y_hat_df, how='left', on=['unique_id', 'ds'])"
]
},
{
Expand Down Expand Up @@ -561,14 +561,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "cb637d8d-2823-4a03-85f8-221636861d2e",
"metadata": {},
"source": [
"## References\n",
"- [Roger Koenker and Gilbert Basset (1978). Regression Quantiles, Econometrica.](https://www.jstor.org/stable/1913643)<br>\n",
"- [Jeffrey L. Elman (1990). \"Finding Structure in Time\".](https://onlinelibrary.wiley.com/doi/abs/10.1207/s15516709cog1402_1)<br>\n",
"- [Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2022). \"N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting\".](https://arxiv.org/abs/2201.12886)<br>"
"- [Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2022). \"NHITS: Neural Hierarchical Interpolation for Time Series Forecasting\".](https://arxiv.org/abs/2201.12886)<br>"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion nbs/models.autoformer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@
" x_mark_enc = None\n",
" x_mark_dec = None\n",
"\n",
" x_dec = torch.zeros(size=(len(insample_y),self.h,1))\n",
" x_dec = torch.zeros(size=(len(insample_y),self.h,1)).to(insample_y.device)\n",
" x_dec = torch.cat([insample_y[:,-self.label_len:,:], x_dec], dim=1)\n",
"\n",
" # decomp init\n",
Expand Down
2 changes: 1 addition & 1 deletion nbs/models.informer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
" x_mark_enc = None\n",
" x_mark_dec = None\n",
"\n",
" x_dec = torch.zeros(size=(len(insample_y),self.h,1))\n",
" x_dec = torch.zeros(size=(len(insample_y),self.h,1)).to(insample_y.device)\n",
" x_dec = torch.cat([insample_y[:,-self.label_len:,:], x_dec], dim=1) \n",
"\n",
" enc_out = self.enc_embedding(insample_y, x_mark_enc)\n",
Expand Down
12 changes: 7 additions & 5 deletions nbs/models.nhits.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -38,15 +39,16 @@
"\n",
"**References**<br>\n",
"-[Boris N. Oreshkin, Dmitri Carpov, Nicolas Chapados, Yoshua Bengio (2019). \"N-BEATS: Neural basis expansion analysis for interpretable time series forecasting\".](https://arxiv.org/abs/1905.10437)<br>\n",
"-[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2022). \"N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting\". Accepted at the Thirty-Seventh AAAI Conference on Artificial Intelligence.](https://arxiv.org/abs/2201.12886)<br>\n",
"-[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, Max Mergenthaler-Canseco, Artur Dubrawski (2022). \"NHITS: Neural Hierarchical Interpolation for Time Series Forecasting\". Accepted at the Thirty-Seventh AAAI Conference on Artificial Intelligence.](https://arxiv.org/abs/2201.12886)<br>\n",
"-[Zhou, H.; Zhang, S.; Peng, J.; Zhang, S.; Li, J.; Xiong, H.; and Zhang, W. (2020). \"Informer: Beyond Efficient Transformer for Long Sequence Time-Series Forecasting\". Association for the Advancement of Artificial Intelligence Conference 2021 (AAAI 2021).](https://arxiv.org/abs/2012.07436)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"![Figure 1. Neural Hierarchical Interpolation for Time Series (N-HITS).](imgs_models/nhits.png)"
"![Figure 1. Neural Hierarchical Interpolation for Time Series (NHITS).](imgs_models/nhits.png)"
]
},
{
Expand Down Expand Up @@ -162,7 +164,7 @@
"\n",
"class NHITSBlock(nn.Module):\n",
" \"\"\"\n",
" N-HiTS block which takes a basis function as an argument.\n",
" NHITS block which takes a basis function as an argument.\n",
" \"\"\"\n",
" def __init__(self, \n",
" input_size: int,\n",
Expand Down Expand Up @@ -297,7 +299,7 @@
"\n",
" **References:**<br>\n",
" -[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza, \n",
" Max Mergenthaler-Canseco, Artur Dubrawski (2022). \"N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting\".\n",
" Max Mergenthaler-Canseco, Artur Dubrawski (2022). \"NHITS: Neural Hierarchical Interpolation for Time Series Forecasting\".\n",
" Accepted at the Thirty-Seventh AAAI Conference on Artificial Intelligence.](https://arxiv.org/abs/2201.12886)\n",
" \"\"\"\n",
" def __init__(self, \n",
Expand Down Expand Up @@ -523,7 +525,7 @@
" pooling_mode='MaxPool1d')\n",
"model.fit(dataset=dataset)\n",
"y_hat = model.predict(dataset=dataset)\n",
"Y_test_df['N-HiTS'] = y_hat\n",
"Y_test_df['NHITS'] = y_hat\n",
"\n",
"pd.concat([Y_train_df, Y_test_df]).drop('unique_id', axis=1).set_index('ds').plot()"
]
Expand Down
2 changes: 1 addition & 1 deletion neuralforecast/models/autoformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def forward(self, windows_batch):
x_mark_enc = None
x_mark_dec = None

x_dec = torch.zeros(size=(len(insample_y), self.h, 1))
x_dec = torch.zeros(size=(len(insample_y), self.h, 1)).to(insample_y.device)
x_dec = torch.cat([insample_y[:, -self.label_len :, :], x_dec], dim=1)

# decomp init
Expand Down
2 changes: 1 addition & 1 deletion neuralforecast/models/informer.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def forward(self, windows_batch):
x_mark_enc = None
x_mark_dec = None

x_dec = torch.zeros(size=(len(insample_y), self.h, 1))
x_dec = torch.zeros(size=(len(insample_y), self.h, 1)).to(insample_y.device)
x_dec = torch.cat([insample_y[:, -self.label_len :, :], x_dec], dim=1)

enc_out = self.enc_embedding(insample_y, x_mark_enc)
Expand Down
4 changes: 2 additions & 2 deletions neuralforecast/models/nhits.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def forward(self, theta: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:

class NHITSBlock(nn.Module):
"""
N-HiTS block which takes a basis function as an argument.
NHITS block which takes a basis function as an argument.
"""

def __init__(
Expand Down Expand Up @@ -216,7 +216,7 @@ class NHITS(BaseWindows):

**References:**<br>
-[Cristian Challu, Kin G. Olivares, Boris N. Oreshkin, Federico Garza,
Max Mergenthaler-Canseco, Artur Dubrawski (2022). "N-HiTS: Neural Hierarchical Interpolation for Time Series Forecasting".
Max Mergenthaler-Canseco, Artur Dubrawski (2022). "NHITS: Neural Hierarchical Interpolation for Time Series Forecasting".
Accepted at the Thirty-Seventh AAAI Conference on Artificial Intelligence.](https://arxiv.org/abs/2201.12886)
"""

Expand Down