使用LSTM递归网络的Pybrain时间序列预测 [英] Pybrain time series prediction using LSTM recurrent nets

查看:161
本文介绍了使用LSTM递归网络的Pybrain时间序列预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想到一个问题,该问题与使用pybrain进行时间序列回归有关.我计划在pybrain中使用LSTM层来训练和预测时间序列.

I have a question in mind which relates to the usage of pybrain to do regression of a time series. I plan to use the LSTM layer in pybrain to train and predict a time series.

我在下面的链接中找到了示例代码

I found an example code here in the link below

例如请求:用于预测序列中下一个值的递归神经网络

在上面的示例中,网络能够在训练后预测序列.但是问题是,网络通过将数据一次性输入到输入层来获取所有顺序数据.例如,如果训练数据每个都有10个特征,那么这10个特征将同时被同时馈送到10个输入节点中.

In the example above, the network is able to predict a sequence after its being trained. But the issue is, network takes in all the sequential data by feeding it in one go to the input layer. For example, if the training data has 10 features each, the 10 features will be simultaneously fed into 10 input nodes at one time.

根据我的理解,这不再是时间序列预测了,对吗?既然每个功能馈入网络的时间没有区别?如果我做错了,请纠正我.

From my understanding, this is no longer a time series prediction am I right? Since there is no difference in terms of the time each feature is fed into the network? Correct me if I am wrong on this.

因此,我试图实现的是一个只有一个输入节点和一个输出节点的递归网络.输入节点是所有时间序列数据将在不同时间步长顺序馈送的位置.将训练网络在输出节点上重现输入.

Therefore, what I am trying to achieve is a recurrent network that has only ONE input node, and ONE output node. The input node is where all the time series data will be fed sequentially at different time steps. The network will be trained to reproduce the input at the output node.

您能否建议或指导我构建我提到的网络? 提前非常感谢您.

Could you please suggest or guide me in constructing the network I mentioned? Thank you very much in advance.

推荐答案

您可以训练具有单个输入节点和单个输出节点的LSTM网络,以进行时间序列预测,如下所示:

You can train an LSTM network with a single input node and a single output node for doing time series prediction like this:

首先,作为一种好习惯,让我们使用Python3的打印功能:

First, just as a good practice, let's use Python3's print function:

from __future__ import print_function

然后,创建一个简单的时间序列:

Then, make a simple time series:

data = [1] * 3 + [2] * 3
data *= 3
print(data)

[1、1、1、2、2、2、1、1、1、2、2、2、1、1、1、2、2、2]

[1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2]

现在将这个时间序列放入一个有监督的数据集中,每个样本的目标是下一个样本:

Now put this timeseries into a supervised dataset, where the target for each sample is the next sample:

from pybrain.datasets import SequentialDataSet
from itertools import cycle

ds = SequentialDataSet(1, 1)
for sample, next_sample in zip(data, cycle(data[1:])):
    ds.addSample(sample, next_sample)

构建一个具有1个输入节点,5个LSTM单元和1个输出节点的简单LSTM网络:

Build a simple LSTM network with 1 input node, 5 LSTM cells and 1 output node:

from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import LSTMLayer

net = buildNetwork(1, 5, 1, 
                   hiddenclass=LSTMLayer, outputbias=False, recurrent=True)

训练网络:

from pybrain.supervised import RPropMinusTrainer
from sys import stdout

trainer = RPropMinusTrainer(net, dataset=ds)
train_errors = [] # save errors for plotting later
EPOCHS_PER_CYCLE = 5
CYCLES = 100
EPOCHS = EPOCHS_PER_CYCLE * CYCLES
for i in xrange(CYCLES):
    trainer.trainEpochs(EPOCHS_PER_CYCLE)
    train_errors.append(trainer.testOnData())
    epoch = (i+1) * EPOCHS_PER_CYCLE
    print("\r epoch {}/{}".format(epoch, EPOCHS), end="")
    stdout.flush()

print()
print("final error =", train_errors[-1])

绘制错误(请注意,在这个简单的玩具示例中,我们正在同一数据集上进行测试和训练,这当然不是您要为真实项目所做的!):

Plot the errors (note that in this simple toy example, we are testing and training on the same dataset, which is of course not what you'd do for a real project!):

import matplotlib.pyplot as plt

plt.plot(range(0, EPOCHS, EPOCHS_PER_CYCLE), train_errors)
plt.xlabel('epoch')
plt.ylabel('error')
plt.show()

现在让网络预测下一个样本:

Now ask the network to predict the next sample:

for sample, target in ds.getSequenceIterator(0):
    print("               sample = %4.1f" % sample)
    print("predicted next sample = %4.1f" % net.activate(sample))
    print("   actual next sample = %4.1f" % target)
    print()

(上面的代码基于 example_rnn.py PyBrain文档)

这篇关于使用LSTM递归网络的Pybrain时间序列预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆