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 Ticker 기호를 언급하는 트윗의 양을 사용합니다.
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" 필드로 지정된 하나의 항목 만 있습니다. 교육을 위해 2015 년 4 월 5 일 자정까지 데이터를 사용합니다.
training_data = ListDataset (
[{ "start" : df . index [ 0 ], "target" : df . value [: "2015-04-05 00:00:00" ]}],
freq = "5min"
) 예측 모델은 예측 변수 객체입니다. 예측 변수를 얻는 한 가지 방법은 특파원 추정기를 훈련시키는 것입니다. 추정기를 인스턴스화하려면 처리 할 시계열의 주파수와 예측 시간 수를 지정해야합니다. 이 예에서는 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' )
예측은 확률 분포 측면에서 표시됩니다. 음영 영역은 각각 중앙값 (Dark Green Line)을 중심으로 각각 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}
}