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

TemporalNorm with ReVIN learnable parameters #768

Merged
merged 6 commits into from
Oct 4, 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
61 changes: 56 additions & 5 deletions nbs/common.base_recurrent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@
" self.early_stop_patience_steps = early_stop_patience_steps\n",
" self.val_check_steps = val_check_steps\n",
"\n",
" # Scaler\n",
" self.scaler = TemporalNorm(scaler_type=scaler_type, dim=-1) # Time dimension is -1.\n",
"\n",
" # Variables\n",
" self.futr_exog_list = futr_exog_list if futr_exog_list is not None else []\n",
" self.hist_exog_list = hist_exog_list if hist_exog_list is not None else []\n",
" self.stat_exog_list = stat_exog_list if stat_exog_list is not None else []\n",
"\n",
" # Scaler\n",
" self.scaler = TemporalNorm(scaler_type=scaler_type, dim=-1, # Time dimension is -1.\n",
" num_features=1+len(self.hist_exog_list)+len(self.futr_exog_list)) \n",
"\n",
" # Fit arguments\n",
" self.val_size = 0\n",
" self.test_size = 0\n",
Expand Down Expand Up @@ -219,13 +220,17 @@
" 'interval': 'step'}\n",
" return {'optimizer': optimizer, 'lr_scheduler': scheduler}\n",
"\n",
" def _normalization(self, batch, val_size=0, test_size=0):\n",
" def _get_temporal_data_cols(self, temporal_cols):\n",
" temporal_data_cols = ['y'] + list(set(temporal_cols.tolist()) &\\\n",
" set(self.hist_exog_list + self.futr_exog_list))\n",
" return temporal_data_cols\n",
"\n",
" def _normalization(self, batch, val_size=0, test_size=0):\n",
" temporal = batch['temporal'] # B, C, T\n",
" temporal_cols = batch['temporal_cols'].copy()\n",
"\n",
" # Separate data and mask\n",
" temporal_data_cols = temporal_cols.drop('available_mask').tolist()\n",
" temporal_data_cols = self._get_temporal_data_cols(temporal_cols=temporal_cols)\n",
" temporal_data = temporal[:, temporal_cols.get_indexer(temporal_data_cols), :]\n",
" temporal_mask = temporal[:, temporal_cols.get_loc('available_mask'), :].clone()\n",
"\n",
Expand Down Expand Up @@ -679,6 +684,52 @@
"show_doc(BaseRecurrent.predict, title_level=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"# add h=0,1 unit test for _parse_windows \n",
"from neuralforecast.losses.pytorch import MAE\n",
"from neuralforecast.utils import AirPassengersDF\n",
"from neuralforecast.tsdataset import TimeSeriesDataset, TimeSeriesDataModule\n",
"\n",
"# Declare batch\n",
"AirPassengersDF['x'] = np.array(len(AirPassengersDF))\n",
"AirPassengersDF['x2'] = np.array(len(AirPassengersDF)) * 2\n",
"dataset, indices, dates, ds = TimeSeriesDataset.from_df(df=AirPassengersDF)\n",
"data = TimeSeriesDataModule(dataset=dataset, batch_size=1, drop_last=True)\n",
"\n",
"train_loader = data.train_dataloader()\n",
"batch = next(iter(train_loader))\n",
"\n",
"# Test that hist_exog_list and futr_exog_list correctly filter data that is sent to scaler.\n",
"baserecurrent = BaseRecurrent(h=12,\n",
" input_size=117,\n",
" hist_exog_list=['x', 'x2'],\n",
" futr_exog_list=['x'],\n",
" loss=MAE(),\n",
" valid_loss=MAE(),\n",
" learning_rate=0.001,\n",
" max_steps=1,\n",
" val_check_steps=0,\n",
" batch_size=1,\n",
" valid_batch_size=1,\n",
" windows_batch_size=10,\n",
" inference_input_size=2,\n",
" start_padding_enabled=True)\n",
"\n",
"windows = baserecurrent._create_windows(batch, step='train')\n",
"\n",
"temporal_cols = windows['temporal_cols'].copy() # B, L+H, C\n",
"temporal_data_cols = baserecurrent._get_temporal_data_cols(temporal_cols=temporal_cols)\n",
"\n",
"test_eq(set(temporal_data_cols), set(['y', 'x', 'x2']))\n",
"test_eq(windows['temporal'].shape, torch.Size([1,len(['y', 'x', 'x2', 'available_mask']),117,12+1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
61 changes: 56 additions & 5 deletions nbs/common.base_windows.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,16 @@
" self.windows_batch_size = windows_batch_size\n",
" self.step_size = step_size\n",
"\n",
" # Scaler\n",
" self.scaler = TemporalNorm(scaler_type=scaler_type, dim=1) # Time dimension is 1.\n",
"\n",
" # Variables\n",
" self.futr_exog_list = futr_exog_list if futr_exog_list is not None else []\n",
" self.hist_exog_list = hist_exog_list if hist_exog_list is not None else []\n",
" self.stat_exog_list = stat_exog_list if stat_exog_list is not None else []\n",
" self.exclude_insample_y = exclude_insample_y\n",
"\n",
" # Scaler\n",
" self.scaler = TemporalNorm(scaler_type=scaler_type, dim=1, # Time dimension is 1.\n",
" num_features=1+len(self.hist_exog_list)+len(self.futr_exog_list))\n",
"\n",
" # Fit arguments\n",
" self.val_size = 0\n",
" self.test_size = 0\n",
Expand Down Expand Up @@ -353,6 +354,11 @@
" return windows_batch\n",
" else:\n",
" raise ValueError(f'Unknown step {step}')\n",
"\n",
" def _get_temporal_data_cols(self, temporal_cols):\n",
" temporal_data_cols = ['y'] + list(set(temporal_cols.tolist()) &\\\n",
" set(self.hist_exog_list + self.futr_exog_list))\n",
" return temporal_data_cols\n",
" \n",
" def _normalization(self, windows):\n",
" # windows are already filtered by train/validation/test\n",
Expand All @@ -361,7 +367,8 @@
" temporal_cols = windows['temporal_cols'].copy() # B, L+H, C\n",
"\n",
" # To avoid leakage uses only the lags\n",
" temporal_data_cols = temporal_cols.drop('available_mask').tolist()\n",
" #temporal_data_cols = temporal_cols.drop('available_mask').tolist()\n",
" temporal_data_cols = self._get_temporal_data_cols(temporal_cols=temporal_cols)\n",
" temporal_data = temporal[:, :, temporal_cols.get_indexer(temporal_data_cols)]\n",
" temporal_mask = temporal[:, :, temporal_cols.get_loc('available_mask')].clone()\n",
" if self.h > 0:\n",
Expand Down Expand Up @@ -822,6 +829,7 @@
"\n",
"# Declare batch\n",
"AirPassengersDF['x'] = np.array(len(AirPassengersDF))\n",
"AirPassengersDF['x2'] = np.array(len(AirPassengersDF)) * 2\n",
"dataset, indices, dates, ds = TimeSeriesDataset.from_df(df=AirPassengersDF)\n",
"data = TimeSeriesDataModule(dataset=dataset, batch_size=1, drop_last=True)\n",
"\n",
Expand Down Expand Up @@ -902,8 +910,51 @@
"windows = basewindows._create_windows(batch, step='predict')\n",
"windows = basewindows._normalization(windows=windows)\n",
"insample_y, insample_mask, outsample_y, outsample_mask, \\\n",
" hist_exog, futr_exog, stat_exog = basewindows._parse_windows(batch, windows)\n"
" hist_exog, futr_exog, stat_exog = basewindows._parse_windows(batch, windows)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54d2e850",
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"\n",
"# Test that hist_exog_list and futr_exog_list correctly filter data.\n",
"# that is sent to scaler.\n",
"basewindows = BaseWindows(h=12,\n",
" input_size=500,\n",
" hist_exog_list=['x', 'x2'],\n",
" futr_exog_list=['x'],\n",
" loss=MAE(),\n",
" valid_loss=MAE(),\n",
" learning_rate=0.001,\n",
" max_steps=1,\n",
" val_check_steps=0,\n",
" batch_size=1,\n",
" valid_batch_size=1,\n",
" windows_batch_size=10,\n",
" inference_windows_batch_size=2,\n",
" start_padding_enabled=True)\n",
"\n",
"windows = basewindows._create_windows(batch, step='train')\n",
"\n",
"temporal_cols = windows['temporal_cols'].copy() # B, L+H, C\n",
"temporal_data_cols = basewindows._get_temporal_data_cols(temporal_cols=temporal_cols)\n",
"\n",
"test_eq(set(temporal_data_cols), set(['y', 'x', 'x2']))\n",
"test_eq(windows['temporal'].shape, torch.Size([10,500+12,len(['y', 'x', 'x2', 'available_mask'])]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf493ff9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading
Loading