Files
pytorch-transformer-ts/autoformer/autoformer.ipynb
T
2022-04-06 13:24:40 +02:00

372 KiB

In [1]:
%matplotlib inline
from matplotlib import pyplot as plt
import matplotlib.dates as mdates

from itertools import islice
In [2]:
from gluonts.evaluation import make_evaluation_predictions, Evaluator
from gluonts.dataset.repository.datasets import get_dataset

from estimator import AutoformerEstimator
/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 [13]:
estimator = AutoformerEstimator(
    freq=dataset.metadata.freq,
    prediction_length=dataset.metadata.prediction_length,
    num_feat_static_cat=1,
    cardinality=[321],
    embedding_dimension=[3],
    dim_feedforward=32,
    num_encoder_layers=2,
    num_decoder_layers=2,
    n_heads=2,
    activation="relu",

    batch_size=128,
    num_batches_per_epoch=100,
    trainer_kwargs=dict(max_epochs=50, accelerator='gpu', gpus=1),
)
In [14]:
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/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/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: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/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
/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/.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]
/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

  | Name  | Type                  | Params
------------------------------------------------
0 | model | AutoformerModel       | 98.4 K
1 | loss  | NegativeLogLikelihood | 0     
------------------------------------------------
98.4 K    Trainable params
0         Non-trainable params
98.4 K    Total params
0.393     Total estimated model params size (MB)
/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: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: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.68778 (best 6.68778), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=0-step=100.ckpt' as top 1
Epoch 1, global step 200: 'train_loss' reached 6.31477 (best 6.31477), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=1-step=200.ckpt' as top 1
Epoch 2, global step 300: 'train_loss' reached 6.16719 (best 6.16719), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=2-step=300.ckpt' as top 1
Epoch 3, global step 400: 'train_loss' reached 6.09789 (best 6.09789), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=3-step=400.ckpt' as top 1
Epoch 4, global step 500: 'train_loss' reached 6.04187 (best 6.04187), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=4-step=500.ckpt' as top 1
Epoch 5, global step 600: 'train_loss' reached 5.95030 (best 5.95030), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=5-step=600.ckpt' as top 1
Epoch 6, global step 700: 'train_loss' reached 5.91891 (best 5.91891), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/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' reached 5.91078 (best 5.91078), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=8-step=900.ckpt' as top 1
Epoch 9, global step 1000: 'train_loss' reached 5.85885 (best 5.85885), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=9-step=1000.ckpt' as top 1
Epoch 10, global step 1100: 'train_loss' reached 5.83621 (best 5.83621), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/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.80791 (best 5.80791), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=12-step=1300.ckpt' as top 1
Epoch 13, global step 1400: 'train_loss' was not in top 1
Epoch 14, global step 1500: 'train_loss' reached 5.79026 (best 5.79026), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=14-step=1500.ckpt' as top 1
Epoch 15, global step 1600: 'train_loss' was not in top 1
Epoch 16, global step 1700: 'train_loss' was not in top 1
Epoch 17, global step 1800: 'train_loss' reached 5.76943 (best 5.76943), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=17-step=1800.ckpt' as top 1
Epoch 18, global step 1900: 'train_loss' reached 5.73974 (best 5.73974), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=18-step=1900.ckpt' as top 1
Epoch 19, global step 2000: 'train_loss' was not in top 1
Epoch 20, global step 2100: 'train_loss' reached 5.71776 (best 5.71776), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=20-step=2100.ckpt' as 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' was not in 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.70524 (best 5.70524), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=26-step=2700.ckpt' as top 1
Epoch 27, global step 2800: 'train_loss' was not in top 1
Epoch 28, global step 2900: 'train_loss' was not in top 1
Epoch 29, global step 3000: 'train_loss' was not in top 1
Epoch 30, global step 3100: 'train_loss' was not in top 1
Epoch 31, global step 3200: 'train_loss' was not in top 1
Epoch 32, global step 3300: 'train_loss' reached 5.67971 (best 5.67971), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=32-step=3300.ckpt' as top 1
Epoch 33, global step 3400: 'train_loss' reached 5.67721 (best 5.67721), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=33-step=3400.ckpt' as 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' reached 5.67696 (best 5.67696), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=40-step=4100.ckpt' as 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' reached 5.66506 (best 5.66506), saving model to '/mnt/scratch/kashif/pytorch-transformer-ts/autoformer/lightning_logs/version_9/checkpoints/epoch=43-step=4400.ckpt' as top 1
Epoch 44, global step 4500: 'train_loss' was not in top 1
Epoch 45, global step 4600: 'train_loss' was not in 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 [15]:
forecast_it, ts_it = make_evaluation_predictions(
    dataset=dataset.test, 
    predictor=predictor
)
In [16]:
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 [17]:
tss = list(ts_it)
In [18]:
evaluator = Evaluator()
In [19]:
agg_metrics, ts_metrics = evaluator(iter(tss), iter(forecasts))
Running evaluation: 2247it [00:00, 4312.72it/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 [20]:
agg_metrics
Out [20]:
{'MSE': 1912270.1601813699,
 'abs_error': 9499909.78665924,
 'abs_target_sum': 128632956.0,
 'abs_target_mean': 2385.272140631954,
 'seasonal_error': 189.49338196116761,
 'MASE': 0.9230363743475488,
 'MAPE': 0.13762913075625383,
 'sMAPE': 0.1225657482189261,
 'MSIS': 6.837486088818375,
 'QuantileLoss[0.1]': 4183971.882330881,
 'Coverage[0.1]': 0.13139741878059635,
 'QuantileLoss[0.2]': 6507467.195659928,
 'Coverage[0.2]': 0.24473371903278449,
 'QuantileLoss[0.3]': 8081948.245087599,
 'Coverage[0.3]': 0.34848316273549923,
 'QuantileLoss[0.4]': 9055948.958718367,
 'Coverage[0.4]': 0.4462987687286753,
 'QuantileLoss[0.5]': 9499909.849783681,
 'Coverage[0.5]': 0.5364560154279779,
 'QuantileLoss[0.6]': 9417650.74509728,
 'Coverage[0.6]': 0.6173972704346535,
 'QuantileLoss[0.7]': 8747053.378532637,
 'Coverage[0.7]': 0.7036418928942294,
 'QuantileLoss[0.8]': 7436005.54045527,
 'Coverage[0.8]': 0.7901090342679128,
 'QuantileLoss[0.9]': 5067467.9464677805,
 'Coverage[0.9]': 0.8804146269099541,
 'RMSE': 1382.8485673353282,
 'NRMSE': 0.5797445682524747,
 'ND': 0.07385284519667915,
 'wQuantileLoss[0.1]': 0.03252643811070377,
 'wQuantileLoss[0.2]': 0.05058942434363343,
 'wQuantileLoss[0.3]': 0.06282953059935589,
 'wQuantileLoss[0.4]': 0.0704014681798836,
 'wQuantileLoss[0.5]': 0.07385284568741218,
 'wQuantileLoss[0.6]': 0.0732133586753404,
 'wQuantileLoss[0.7]': 0.06800009616923237,
 'wQuantileLoss[0.8]': 0.057807934853454424,
 'wQuantileLoss[0.9]': 0.03939478733947294,
 'mean_absolute_QuantileLoss': 7555269.304681491,
 'mean_wQuantileLoss': 0.05873509821760989,
 'MAE_Coverage': 0.028653842984061054,
 'OWA': nan}
In [21]:
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 [ ]: