mirror of
https://github.com/wassname/pytorch-transformer-ts.git
synced 2026-07-28 11:24:48 +08:00
376 KiB
376 KiB
In [1]:
%matplotlib inline
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
from itertools import isliceIn [2]:
from gluonts.evaluation import make_evaluation_predictions, Evaluator
from gluonts.dataset.repository.datasets import get_dataset
from estimator import ETSformerEstimator/home/kashif/.env/pytorch/lib/python3.8/site-packages/xgboost/compat.py:36: FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead. from pandas import MultiIndex, Int64Index
In [3]:
dataset = get_dataset("electricity")In [14]:
estimator = ETSformerEstimator(
freq=dataset.metadata.freq,
prediction_length=dataset.metadata.prediction_length,
context_length=dataset.metadata.prediction_length*2,
#
num_feat_static_cat=1,
cardinality=[321],
embedding_dimension=[3],
# attention hyper-params
nhead=2,
# training params
batch_size=128,
num_batches_per_epoch=100,
trainer_kwargs=dict(max_epochs=50, accelerator='gpu', gpus=1),
)In [15]:
predictor = estimator.train(
training_data=dataset.train,
num_workers=8,
shuffle_buffer_length=1024
)/home/kashif/.env/pytorch/lib/python3.8/site-packages/pytorch_lightning/utilities/parsing.py:244: UserWarning: Attribute 'model' is an instance of `nn.Module` and is already saved during checkpointing. It is recommended to ignore them using `self.save_hyperparameters(ignore=['model'])`.
rank_zero_warn(
/home/kashif/.env/pytorch/lib/python3.8/site-packages/pytorch_lightning/utilities/parsing.py:244: UserWarning: Attribute 'loss' is an instance of `nn.Module` and is already saved during checkpointing. It is recommended to ignore them using `self.save_hyperparameters(ignore=['loss'])`.
rank_zero_warn(
GPU available: True, used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/.env/pytorch/lib/python3.8/site-packages/pytorch_lightning/trainer/configuration_validator.py:133: UserWarning: You defined a `validation_step` but have no `val_dataloader`. Skipping val loop.
rank_zero_warn("You defined a `validation_step` but have no `val_dataloader`. Skipping val loop.")
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
| Name | Type | Params
------------------------------------------------
0 | model | ETSformerModel | 52.2 K
1 | loss | NegativeLogLikelihood | 0
------------------------------------------------
52.2 K Trainable params
0 Non-trainable params
52.2 K Total params
0.209 Total estimated model params size (MB)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base = start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
return _shift_timestamp_helper(ts, ts.freq, offset)
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
..., i0 : i0 + length * start.freq.n : start.freq.n
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.
self._freq_base is None or self._freq_base == start.freq.base
Training: 0it [00:00, ?it/s]
Epoch 0, global step 100: 'train_loss' reached 6.83650 (best 6.83650), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=0-step=100.ckpt' as top 1 Epoch 1, global step 200: 'train_loss' reached 6.16099 (best 6.16099), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=1-step=200.ckpt' as top 1 Epoch 2, global step 300: 'train_loss' reached 6.03977 (best 6.03977), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=2-step=300.ckpt' as top 1 Epoch 3, global step 400: 'train_loss' reached 5.96518 (best 5.96518), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=3-step=400.ckpt' as top 1 Epoch 4, global step 500: 'train_loss' reached 5.92151 (best 5.92151), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=4-step=500.ckpt' as top 1 Epoch 5, global step 600: 'train_loss' reached 5.88383 (best 5.88383), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=5-step=600.ckpt' as top 1 Epoch 6, global step 700: 'train_loss' reached 5.84047 (best 5.84047), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=6-step=700.ckpt' as top 1 Epoch 7, global step 800: 'train_loss' was not in top 1 Epoch 8, global step 900: 'train_loss' was not in top 1 Epoch 9, global step 1000: 'train_loss' reached 5.83049 (best 5.83049), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=9-step=1000.ckpt' as top 1 Epoch 10, global step 1100: 'train_loss' reached 5.80265 (best 5.80265), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=10-step=1100.ckpt' as top 1 Epoch 11, global step 1200: 'train_loss' was not in top 1 Epoch 12, global step 1300: 'train_loss' reached 5.78531 (best 5.78531), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=12-step=1300.ckpt' as top 1 Epoch 13, global step 1400: 'train_loss' reached 5.77739 (best 5.77739), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=13-step=1400.ckpt' as top 1 Epoch 14, global step 1500: 'train_loss' was not in top 1 Epoch 15, global step 1600: 'train_loss' reached 5.73936 (best 5.73936), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=15-step=1600.ckpt' as top 1 Epoch 16, global step 1700: 'train_loss' was not in top 1 Epoch 17, global step 1800: 'train_loss' was not in top 1 Epoch 18, global step 1900: 'train_loss' was not in top 1 Epoch 19, global step 2000: 'train_loss' reached 5.71629 (best 5.71629), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=19-step=2000.ckpt' as top 1 Epoch 20, global step 2100: 'train_loss' was not in top 1 Epoch 21, global step 2200: 'train_loss' was not in top 1 Epoch 22, global step 2300: 'train_loss' was not in top 1 Epoch 23, global step 2400: 'train_loss' reached 5.70743 (best 5.70743), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=23-step=2400.ckpt' as top 1 Epoch 24, global step 2500: 'train_loss' was not in top 1 Epoch 25, global step 2600: 'train_loss' was not in top 1 Epoch 26, global step 2700: 'train_loss' reached 5.70683 (best 5.70683), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=26-step=2700.ckpt' as top 1 Epoch 27, global step 2800: 'train_loss' reached 5.68871 (best 5.68871), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=27-step=2800.ckpt' as top 1 Epoch 28, global step 2900: 'train_loss' reached 5.68410 (best 5.68410), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=28-step=2900.ckpt' as top 1 Epoch 29, global step 3000: 'train_loss' was not in top 1 Epoch 30, global step 3100: 'train_loss' reached 5.67481 (best 5.67481), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=30-step=3100.ckpt' as top 1 Epoch 31, global step 3200: 'train_loss' reached 5.65723 (best 5.65723), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=31-step=3200.ckpt' as top 1 Epoch 32, global step 3300: 'train_loss' was not in top 1 Epoch 33, global step 3400: 'train_loss' was not in top 1 Epoch 34, global step 3500: 'train_loss' was not in top 1 Epoch 35, global step 3600: 'train_loss' was not in top 1 Epoch 36, global step 3700: 'train_loss' was not in top 1 Epoch 37, global step 3800: 'train_loss' was not in top 1 Epoch 38, global step 3900: 'train_loss' was not in top 1 Epoch 39, global step 4000: 'train_loss' was not in top 1 Epoch 40, global step 4100: 'train_loss' was not in top 1 Epoch 41, global step 4200: 'train_loss' was not in top 1 Epoch 42, global step 4300: 'train_loss' was not in top 1 Epoch 43, global step 4400: 'train_loss' was not in top 1 Epoch 44, global step 4500: 'train_loss' was not in top 1 Epoch 45, global step 4600: 'train_loss' reached 5.65159 (best 5.65159), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/etsformer/lightning_logs/version_3/checkpoints/epoch=45-step=4600.ckpt' as top 1 Epoch 46, global step 4700: 'train_loss' was not in top 1 Epoch 47, global step 4800: 'train_loss' was not in top 1 Epoch 48, global step 4900: 'train_loss' was not in top 1 Epoch 49, global step 5000: 'train_loss' was not in top 1
In [16]:
forecast_it, ts_it = make_evaluation_predictions(
dataset=dataset.test,
predictor=predictor
)In [17]:
forecasts = list(forecast_it)/home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:343: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. self._freq_base = start.freq.base /home/kashif/gluon-ts-PR/src/gluonts/transform/split.py:36: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. return _shift_timestamp_helper(ts, ts.freq, offset) /home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:384: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. ..., i0 : i0 + length * start.freq.n : start.freq.n /home/kashif/gluon-ts-PR/src/gluonts/transform/feature.py:340: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. self._freq_base is None or self._freq_base == start.freq.base
In [18]:
tss = list(ts_it)In [19]:
evaluator = Evaluator()In [20]:
agg_metrics, ts_metrics = evaluator(iter(tss), iter(forecasts))Running evaluation: 2247it [00:00, 4924.54it/s]/home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/gluon-ts-PR/src/gluonts/evaluation/_base.py:306: FutureWarning: Timestamp.freq is deprecated and will be removed in a future version. date_before_forecast = forecast.index[0] - forecast.index[0].freq /home/kashif/.env/pytorch/lib/python3.8/site-packages/pandas/core/construction.py:781: UserWarning: Warning: converting a masked element to nan. subarr = np.array(arr, dtype=dtype, copy=copy)
In [21]:
agg_metricsOut [21]:
{'MSE': 2405696.9162869295,
'abs_error': 10426922.300764084,
'abs_target_sum': 128632956.0,
'abs_target_mean': 2385.272140631954,
'seasonal_error': 189.49338196116761,
'MASE': 0.9046918475206022,
'MAPE': 0.13628862122431049,
'sMAPE': 0.12878806848051888,
'MSIS': 8.437463235791425,
'QuantileLoss[0.1]': 3981099.9491350194,
'Coverage[0.1]': 0.07975448746476783,
'QuantileLoss[0.2]': 6389192.250024781,
'Coverage[0.2]': 0.13746105919003115,
'QuantileLoss[0.3]': 8211433.885405607,
'Coverage[0.3]': 0.1896417445482866,
'QuantileLoss[0.4]': 9542885.989697104,
'Coverage[0.4]': 0.2402462542649458,
'QuantileLoss[0.5]': 10426922.319201306,
'Coverage[0.5]': 0.2942256341789052,
'QuantileLoss[0.6]': 10963723.530763008,
'Coverage[0.6]': 0.35145008159026847,
'QuantileLoss[0.7]': 10917687.92765279,
'Coverage[0.7]': 0.42515947188844383,
'QuantileLoss[0.8]': 10156566.021311926,
'Coverage[0.8]': 0.5211763833259161,
'QuantileLoss[0.9]': 8273664.033297701,
'Coverage[0.9]': 0.658989764129951,
'RMSE': 1551.0309204806103,
'NRMSE': 0.6502532327693562,
'ND': 0.08105949381093353,
'wQuantileLoss[0.1]': 0.030949300031129032,
'wQuantileLoss[0.2]': 0.04966994811209019,
'wQuantileLoss[0.3]': 0.0638361594162977,
'wQuantileLoss[0.4]': 0.07418694467145033,
'wQuantileLoss[0.5]': 0.08105949395426554,
'wQuantileLoss[0.6]': 0.08523261745429381,
'wQuantileLoss[0.7]': 0.08487473402735757,
'wQuantileLoss[0.8]': 0.07895772854129175,
'wQuantileLoss[0.9]': 0.06431994016601547,
'mean_absolute_QuantileLoss': 8762575.100721026,
'mean_wQuantileLoss': 0.0681207629304657,
'MAE_Coverage': 0.1779883466020538,
'OWA': nan}In [22]:
plt.figure(figsize=(20, 15))
date_formater = mdates.DateFormatter('%b, %d')
plt.rcParams.update({'font.size': 15})
for idx, (forecast, ts) in islice(enumerate(zip(forecasts, tss)), 9):
ax = plt.subplot(3, 3, idx+1)
plt.plot(ts[-4 * dataset.metadata.prediction_length:], label="target", )
forecast.plot( color='g')
plt.xticks(rotation=60)
plt.title(forecast.item_id)
ax.xaxis.set_major_formatter(date_formater)
plt.gcf().tight_layout()
plt.legend()
plt.show()In [ ]: