PyTorchTS
PyTorchTS 是一个基于 PyTorch 的概率时间序列预测框架,通过使用 GluonTS 作为其后端 API,并用于加载、转换和回测时间序列数据集,提供最先进的 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"
字段。对于训练,我们将使用截至 2015 年 4 月 5 日午夜的数据。
training_data = ListDataset(
[{"start": df.index[0], "target": df.value[:"2015-04-05 00:00:00"]}],
freq = "5min"
)
预测模型是一个 predictor 对象。获得预测器的一种方法是通过训练相应的估计器。实例化估计器需要指定它将处理的时间序列的频率,以及要预测的时间步数。在我们的例子中,我们使用 5 分钟的数据,所以 freq="5min"
,我们将训练一个模型来预测下一个小时,所以 prediction_length=12
。模型在每个时间点的输入将是大小为 input_size=43
的向量。我们还指定了一些最小的训练选项,特别是在 device
上训练 epoch=10
。
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}
}