Pythorch概率时间序列建模框架
pytorchts的Python项目详细描述
侏儒
PyTorchTS是一个PyTorch概率时间序列预测框架,它利用GluonTS作为API(变化最小),提供最先进的Pythorch时间序列模型,用于加载、转换和回测时间序列数据集。目前,随着PyTorch的更改,gloonts代码被复制到这个存储库中,但是最终gloonts应该成为一个外部需求。在
安装
$ pip3 install pytorchts
快速入门
在这里,我们通过gloonts自述文件来强调API的更改。在
^{pr2}$这个简单的例子说明了如何根据一些数据训练模型,然后使用它来进行预测。作为第一步,我们需要收集一些数据:在本例中,我们将使用提到AMZN股票代码的tweet的数量。在
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()
我们现在可以为我们的模型准备一个训练数据集。数据集本质上是字典的iterable集合:每个字典代表一个可能具有关联特征的时间序列。对于本例,我们只有一个条目,由"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分钟的数据,所以req="5min"
,我们将训练一个模型来预测下一个小时,所以prediction_length=12
。模型的输入将是每个时间点的一个大小为input_size=43
的向量。我们还为epoch=10
指定一些最小的训练选项,特别是关于device
的训练。在
device=torch.device("cuda"iftorch.cuda.is_available()else"cpu")estimator=DeepAREstimator(freq="5min",prediction_length=12,input_size=43,trainer=Trainer(epochs=10,device=device))predictor=estimator.train(training_data=training_data)
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")
fortest_entry,forecastinzip(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
科学文章
我们使用此框架实现了以下模型:
@article{rasul2020tempflow, Author = {Kashif Rasul, Abdul-Saboor Sheikh, Ingmar Schuster, Urs Bergmann, Roland Vollgraf} Title = {Multi-variate Probabilistic Time Series Forecasting via Conditioned Normalizing Flows}, Year = {2020}, archivePrefix = {arXiv}, eprint = {2002.06103}, }
- 项目
标签: