Pytorchtsは、GluontsをバックエンドAPIとして利用し、時間系列のデータセットを読み込み、変換、バックテストするために、最先端のPytorch時系列モデルを提供するPytorchの確率的時系列予測フレームワークです。
$ pip3 install pytorchts
ここでは、Gluonts Readmeを介してAPIの変更を強調します。
import matplotlib . pyplot as plt
import pandas as pd
import torch
from gluonts . dataset . common import ListDataset
from gluonts . dataset . util import to_pandas
from pts . model . deepar import DeepAREstimator
from pts import Trainerこの簡単な例は、いくつかのデータでモデルをトレーニングする方法を示しており、それを使用して予測を行います。最初のステップとして、いくつかのデータを収集する必要があります。この例では、AMZNティッカーシンボルに言及したツイートのボリュームを使用します。
url = "https://raw.githubusercontent.com/numenta/NAB/master/data/realTweets/Twitter_volume_AMZN.csv"
df = pd . read_csv ( url , header = 0 , index_col = 0 , parse_dates = True )最初の100のデータポイントは次のようになります。
df [: 100 ]. plot ( linewidth = 2 )
plt . grid ( which = 'both' )
plt . show ()
これで、モデルがトレーニングできるトレーニングデータセットを準備できます。データセットは、本質的に辞書の反復可能なコレクションです。各辞書は、おそらく関連する機能を備えた時系列を表します。この例では、最初のデータポイントのタイムスタンプである"start"フィールドで指定されたエントリと、時系列データを含む"target"フィールドのみが1つしかありません。トレーニングには、2015年4月5日の真夜中までデータを使用します。
training_data = ListDataset (
[{ "start" : df . index [ 0 ], "target" : df . value [: "2015-04-05 00:00:00" ]}],
freq = "5min"
)予測モデルは予測オブジェクトです。予測因子を取得する1つの方法は、特派員の推定器をトレーニングすることです。推定器をインスタンス化するには、処理する時系列の頻度と、予測する時間ステップの数を指定する必要があります。この例では、5分間のデータを使用しているため、 req="5min"使用しています。次の時間をprediction_length=12するためにモデルをトレーニングします。モデルへの入力は、各時点でサイズinput_size=43のベクトルになります。また、 epoch=10のdeviceで特定のトレーニングで最小限のトレーニングオプションを指定します。
device = torch . device ( "cuda" if torch . cuda . is_available () else "cpu" )
estimator = DeepAREstimator ( freq = "5min" ,
prediction_length = 12 ,
input_size = 19 ,
trainer = Trainer ( epochs = 10 ,
device = device ))
predictor = estimator . train ( training_data = training_data , num_workers = 4 ) 45it [00:01, 37.60it/s, avg_epoch_loss=4.64, epoch=0]
48it [00:01, 39.56it/s, avg_epoch_loss=4.2, epoch=1]
45it [00:01, 38.11it/s, avg_epoch_loss=4.1, epoch=2]
43it [00:01, 36.29it/s, avg_epoch_loss=4.05, epoch=3]
44it [00:01, 35.98it/s, avg_epoch_loss=4.03, epoch=4]
48it [00:01, 39.48it/s, avg_epoch_loss=4.01, epoch=5]
48it [00:01, 38.65it/s, avg_epoch_loss=4, epoch=6]
46it [00:01, 37.12it/s, avg_epoch_loss=3.99, epoch=7]
48it [00:01, 38.86it/s, avg_epoch_loss=3.98, epoch=8]
48it [00:01, 39.49it/s, avg_epoch_loss=3.97, epoch=9]
トレーニング中、進捗に関する有用な情報が表示されます。利用可能なオプションの完全な概要を取得するには、 DeepAREstimator (または他の推定器)およびTrainerのソースコードを参照してください。
予測を行う準備ができました。2015年4月15日の真夜中に続く時間を予測します。
test_data = ListDataset (
[{ "start" : df . index [ 0 ], "target" : df . value [: "2015-04-15 00:00:00" ]}],
freq = "5min"
) for test_entry , forecast in zip ( test_data , predictor . predict ( test_data )):
to_pandas ( test_entry )[ - 60 :]. plot ( linewidth = 2 )
forecast . plot ( color = 'g' , prediction_intervals = [ 50.0 , 90.0 ])
plt . grid ( which = 'both' )
予測は確率分布の観点から表示されることに注意してください。網掛け部分は、それぞれ中央値(濃い緑色の線)を中心とする50%と90%の予測間隔を表しています。
pip install -e .
pytest test
このリポジトリを引用するには:
@software{pytorchgithub,
author = {Kashif Rasul},
title = {{P}yTorch{TS}},
url = {https://github.com/zalandoresearch/pytorch-ts},
version = {0.6.x},
year = {2021},
}このフレームワークを使用して、次のモデルを実装しました。
@INPROCEEDINGS{rasul2020tempflow,
author = {Kashif Rasul and Abdul-Saboor Sheikh and Ingmar Schuster and Urs Bergmann and Roland Vollgraf},
title = {{M}ultivariate {P}robabilistic {T}ime {S}eries {F}orecasting via {C}onditioned {N}ormalizing {F}lows},
year = {2021},
url = {https://openreview.net/forum?id=WiGQBFuVRv},
booktitle = {International Conference on Learning Representations 2021},
}@InProceedings{pmlr-v139-rasul21a,
title = {{A}utoregressive {D}enoising {D}iffusion {M}odels for {M}ultivariate {P}robabilistic {T}ime {S}eries {F}orecasting},
author = {Rasul, Kashif and Seward, Calvin and Schuster, Ingmar and Vollgraf, Roland},
booktitle = {Proceedings of the 38th International Conference on Machine Learning},
pages = {8857--8868},
year = {2021},
editor = {Meila, Marina and Zhang, Tong},
volume = {139},
series = {Proceedings of Machine Learning Research},
month = {18--24 Jul},
publisher = {PMLR},
pdf = {http://proceedings.mlr.press/v139/rasul21a/rasul21a.pdf},
url = {http://proceedings.mlr.press/v139/rasul21a.html},
}@misc{gouttes2021probabilistic,
title={{P}robabilistic {T}ime {S}eries {F}orecasting with {I}mplicit {Q}uantile {N}etworks},
author={Adèle Gouttes and Kashif Rasul and Mateusz Koren and Johannes Stephan and Tofigh Naghibi},
year={2021},
eprint={2107.03743},
archivePrefix={arXiv},
primaryClass={cs.LG}
}