N-beats model¶
In [1]:
Copied!
#!pip install deepts_forecasting
#!pip install deepts_forecasting
Import libraries¶
In [ ]:
Copied!
import numpy as np
import torch
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning.callbacks import EarlyStopping, LearningRateMonitor
from pytorch_lightning.loggers import TensorBoardLogger
from deepts_forecasting.utils.data import TimeSeriesDataSet
from deepts_forecasting.utils.data.encoders import TorchNormalizer
from deepts_forecasting.datasets import AirPassengersDataset
from deepts_forecasting.models.nbeats.nbeats import NBEATSModel
import numpy as np
import torch
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning.callbacks import EarlyStopping, LearningRateMonitor
from pytorch_lightning.loggers import TensorBoardLogger
from deepts_forecasting.utils.data import TimeSeriesDataSet
from deepts_forecasting.utils.data.encoders import TorchNormalizer
from deepts_forecasting.datasets import AirPassengersDataset
from deepts_forecasting.models.nbeats.nbeats import NBEATSModel
D:\Anaconda3\envs\DeepTS_Forecasting\lib\site-packages\tqdm\auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
Dataset¶
In [4]:
Copied!
data = AirPassengersDataset().load()
data['year'] = data['Month'].dt.year
data['month'] = data['Month'].dt.month
data['group'] = '0'
data['time_idx'] = np.arange(len(data))
data['Passengers'] = data['Passengers'].astype(float)
data['month'] = data['month'].astype('str')
data.head()
data = AirPassengersDataset().load()
data['year'] = data['Month'].dt.year
data['month'] = data['Month'].dt.month
data['group'] = '0'
data['time_idx'] = np.arange(len(data))
data['Passengers'] = data['Passengers'].astype(float)
data['month'] = data['month'].astype('str')
data.head()
Out[4]:
Month | Passengers | year | month | group | time_idx | |
---|---|---|---|---|---|---|
0 | 1949-01-01 | 112.0 | 1949 | 1 | 0 | 0 |
1 | 1949-02-01 | 118.0 | 1949 | 2 | 0 | 1 |
2 | 1949-03-01 | 132.0 | 1949 | 3 | 0 | 2 |
3 | 1949-04-01 | 129.0 | 1949 | 4 | 0 | 3 |
4 | 1949-05-01 | 121.0 | 1949 | 5 | 0 | 4 |
Split train/test sets¶
In [11]:
Copied!
max_encoder_length = 18
max_prediction_length = 12
training_cutoff = data["time_idx"].max() - max_encoder_length - max_prediction_length
training = TimeSeriesDataSet(
data[lambda x: x.time_idx <= training_cutoff],
max_encoder_length= max_encoder_length,
min_encoder_length=max_encoder_length,
max_prediction_length=max_prediction_length,
min_prediction_length=max_prediction_length,
time_idx="time_idx",
target="Passengers",
group_ids=["group"],
static_categoricals=[],
static_reals=[],
time_varying_known_categoricals=['month'],
time_varying_known_reals=[],
time_varying_unknown_reals=["Passengers"],
time_varying_unknown_categoricals=[],
target_normalizer=TorchNormalizer(method="standard",
transformation=None),
)
training.get_parameters()
validation = TimeSeriesDataSet.from_dataset(training,
data[lambda x: x.time_idx > training_cutoff])
batch_size = 16
train_dataloader = DataLoader(training, batch_size=batch_size, shuffle=False, drop_last=False)
val_dataloader = DataLoader(validation, batch_size=batch_size, shuffle=False, drop_last=False)
max_encoder_length = 18
max_prediction_length = 12
training_cutoff = data["time_idx"].max() - max_encoder_length - max_prediction_length
training = TimeSeriesDataSet(
data[lambda x: x.time_idx <= training_cutoff],
max_encoder_length= max_encoder_length,
min_encoder_length=max_encoder_length,
max_prediction_length=max_prediction_length,
min_prediction_length=max_prediction_length,
time_idx="time_idx",
target="Passengers",
group_ids=["group"],
static_categoricals=[],
static_reals=[],
time_varying_known_categoricals=['month'],
time_varying_known_reals=[],
time_varying_unknown_reals=["Passengers"],
time_varying_unknown_categoricals=[],
target_normalizer=TorchNormalizer(method="standard",
transformation=None),
)
training.get_parameters()
validation = TimeSeriesDataSet.from_dataset(training,
data[lambda x: x.time_idx > training_cutoff])
batch_size = 16
train_dataloader = DataLoader(training, batch_size=batch_size, shuffle=False, drop_last=False)
val_dataloader = DataLoader(validation, batch_size=batch_size, shuffle=False, drop_last=False)
Define model¶
In [12]:
Copied!
pl.seed_everything(123)
# create PyTorch Lighning Trainer with early stopping
early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-4,
patience=60, verbose=False, mode="min")
lr_logger = LearningRateMonitor()
trainer = pl.Trainer(
max_epochs=300,
gpus=0, # run on CPU, if on multiple GPUs, use accelerator="ddp"
gradient_clip_val=0.1,
limit_train_batches=30, # 30 batches per epoch
callbacks=[lr_logger, early_stop_callback],
logger=TensorBoardLogger("lightning_logs")
)
model = NBEATSModel.from_dataset(training,
nr_params=1,
num_blocks=2,
num_layers=3,
stack_types=['trend', 'seasonality'],
layer_widths=[64, 64],
expansion_coefficient_dim=3,
)
model.summarize
pl.seed_everything(123)
# create PyTorch Lighning Trainer with early stopping
early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-4,
patience=60, verbose=False, mode="min")
lr_logger = LearningRateMonitor()
trainer = pl.Trainer(
max_epochs=300,
gpus=0, # run on CPU, if on multiple GPUs, use accelerator="ddp"
gradient_clip_val=0.1,
limit_train_batches=30, # 30 batches per epoch
callbacks=[lr_logger, early_stop_callback],
logger=TensorBoardLogger("lightning_logs")
)
model = NBEATSModel.from_dataset(training,
nr_params=1,
num_blocks=2,
num_layers=3,
stack_types=['trend', 'seasonality'],
layer_widths=[64, 64],
expansion_coefficient_dim=3,
)
model.summarize
Global seed set to 123 GPU available: False, used: False TPU available: False, using: 0 TPU cores IPU available: False, using: 0 IPUs
Out[12]:
<bound method LightningModule.summarize of NBEATSModel( (loss): MSELoss() (logging_metrics): ModuleList() (stacks): ModuleList( (0): _Stack( (blocks): ModuleList( (0): _Block( (relu): ReLU() (fc_stack): ModuleList( (0): Linear(in_features=18, out_features=64, bias=True) (1): Linear(in_features=64, out_features=64, bias=True) (2): Linear(in_features=64, out_features=64, bias=True) ) (backcast_linear_layer): Linear(in_features=64, out_features=3, bias=True) (forecast_linear_layer): Linear(in_features=64, out_features=3, bias=True) (backcast_g): TrendBasis() (forecast_g): TrendBasis() ) (1): _Block( (relu): ReLU() (fc_stack): ModuleList( (0): Linear(in_features=18, out_features=64, bias=True) (1): Linear(in_features=64, out_features=64, bias=True) (2): Linear(in_features=64, out_features=64, bias=True) ) (backcast_linear_layer): Linear(in_features=64, out_features=3, bias=True) (forecast_linear_layer): Linear(in_features=64, out_features=3, bias=True) (backcast_g): TrendBasis() (forecast_g): TrendBasis() ) ) ) (1): _Stack( (blocks): ModuleList( (0): _Block( (relu): ReLU() (fc_stack): ModuleList( (0): Linear(in_features=18, out_features=64, bias=True) (1): Linear(in_features=64, out_features=64, bias=True) (2): Linear(in_features=64, out_features=64, bias=True) ) (backcast_linear_layer): Linear(in_features=64, out_features=17, bias=True) (forecast_linear_layer): Linear(in_features=64, out_features=11, bias=True) (backcast_g): SeasonalityBasis() (forecast_g): SeasonalityBasis() ) (1): _Block( (relu): ReLU() (fc_stack): ModuleList( (0): Linear(in_features=18, out_features=64, bias=True) (1): Linear(in_features=64, out_features=64, bias=True) (2): Linear(in_features=64, out_features=64, bias=True) ) (backcast_linear_layer): Linear(in_features=64, out_features=17, bias=True) (forecast_linear_layer): Linear(in_features=64, out_features=11, bias=True) (backcast_g): SeasonalityBasis() (forecast_g): SeasonalityBasis() ) ) ) ) )>
In [13]:
Copied!
model.hparams
model.hparams
Out[13]:
"exogenous_dim": 0 "expansion_coefficient_dim": 3 "input_length": 18 "layer_widths": [64, 64] "learning_rate": 0.001 "log_interval": -1 "log_val_interval": None "logging_metrics": ModuleList() "loss": MSELoss() "monotone_constaints": {} "nr_params": 1 "num_blocks": 2 "num_layers": 3 "output_transformer": TorchNormalizer() "prediction_length": 12 "reals": ['Passengers'] "stack_types": ['trend', 'seasonality'] "time_varying_known_reals": []
Train model with early stopping¶
In [14]:
Copied!
trainer.fit(
model, train_dataloader=train_dataloader, val_dataloaders=val_dataloader,
)
# (given that we use early stopping, this is not necessarily the last epoch)
best_model_path = trainer.checkpoint_callback.best_model_path
best_model = NBEATSModel.load_from_checkpoint(best_model_path)
# calcualte mean absolute error on validation set
actuals = torch.cat([model.transform_output(prediction=y, target_scale=x['target_scale'])
for x, y in iter(val_dataloader)])
predictions, x_index = best_model.predict(val_dataloader)
mae = (actuals - predictions).abs().mean()
# print('predictions shape is:', predictions.shape)
# print('actuals shape is:', actuals.shape)
print(torch.cat([actuals, predictions]))
print('MAE is:', mae)
trainer.fit(
model, train_dataloader=train_dataloader, val_dataloaders=val_dataloader,
)
# (given that we use early stopping, this is not necessarily the last epoch)
best_model_path = trainer.checkpoint_callback.best_model_path
best_model = NBEATSModel.load_from_checkpoint(best_model_path)
# calcualte mean absolute error on validation set
actuals = torch.cat([model.transform_output(prediction=y, target_scale=x['target_scale'])
for x, y in iter(val_dataloader)])
predictions, x_index = best_model.predict(val_dataloader)
mae = (actuals - predictions).abs().mean()
# print('predictions shape is:', predictions.shape)
# print('actuals shape is:', actuals.shape)
print(torch.cat([actuals, predictions]))
print('MAE is:', mae)
| Name | Type | Params ----------------------------------------------- 0 | loss | MSELoss | 0 1 | logging_metrics | ModuleList | 0 2 | stacks | ModuleList | 21.8 K ----------------------------------------------- 21.3 K Trainable params 528 Non-trainable params 21.8 K Total params 0.087 Total estimated model params size (MB)
Global seed set to 123
Epoch 99: 100%|██████████| 7/7 [00:00<00:00, 63.34it/s, loss=0.00676, v_num=5, val_loss=0.112, train_loss=0.00607] tensor([[[417.0000], [391.0000], [419.0000], [461.0000], [472.0000], [535.0000], [622.0000], [606.0000], [508.0000], [461.0000], [390.0000], [432.0000]], [[391.0681], [359.8405], [416.3703], [432.7150], [443.1371], [498.9146], [584.6387], [625.6229], [526.2440], [434.5593], [365.1976], [382.0431]]], dtype=torch.float64) MAE is: tensor(27.4486, dtype=torch.float64)