训练LSTM神经网络以预测pybrain,python中的时间序列 [英] Training an LSTM neural network to forecast time series in pybrain, python

查看:158
本文介绍了训练LSTM神经网络以预测pybrain,python中的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PyBrain创建的神经网络,旨在预测时间序列.

I have a neural network created using PyBrain and designed to forecast time series.

我正在使用顺序数据集函数,并尝试使用5个先前值的滑动窗口来预测第6个.我的问题之一是,我无法弄清楚如何通过将5个先前的值附加到输入和6个附加值作为输出来创建所需的数据集.

I am using the sequential dataset function, and trying to use a sliding window of 5 previous values to predict the 6th. One of my problems is that I can't figure out how to create the required dataset by appending the 5 previous values to the inputs and the 6th as an output.

我也不确定一旦训练好网络后如何准确预测系列中的值.

I am also unsure of how exactly to forecast values in the series once the network is trained.

在下面发布我的代码:

from pybrain.datasets import SupervisedDataSet
from pybrain.datasets import SequentialDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.supervised.trainers import RPropMinusTrainer
from pylab import ion, ioff, figure, draw, contourf, clf, show, hold, plot
from pybrain.structure import RecurrentNetwork
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer, TanhLayer
from pybrain.structure import FullConnection
from pybrain.structure import LSTMLayer
from pybrain.structure import BiasUnit
from pybrain.rl.learners.valuebased import Q
import pybrain
import matplotlib as plt
import translate
import time
import pickle
import scipy as sp
import numpy as np
import pylab as pl
import itertools

#Opening data from database
data = translate.translate(3600)
time, price, volume = zip(*data)

#Creating data lists instead of tuples
timeList = []
priceList = []
volumeList = []

for record in time:
    timeList.append(record)

for record in price:
    priceList.append(record)

for record in volume:
    volumeList.append(record)

#Creating lookback window and target
datain = priceList[:5] 
dataout = priceList[6]
print datain
print dataout
#Creating the dataset
ds = SequentialDataSet(5, 1)

for x, y in itertools.izip(datain, dataout):
    ds.newSequence()
    ds.appendLinked(tuple(x), tuple(y))
    print (x, y)

print ds

#Building the network
n = RecurrentNetwork()

#Create the network modules
n.addInputModule(SigmoidLayer(5, name = 'in'))
n.addModule(LSTMLayer(100, name = 'LSTM'))
n.addModule(LSTMLayer(100, name = 'LSTM2'))
n.addOutputModule(SigmoidLayer(1, name = 'out'))

#Add the network connections
n.addConnection(FullConnection(n['in'], n['LSTM'], name = 'c_in_to_LSTM'))
n.addConnection(FullConnection(n['in'], n['LSTM2'], name = 'c_in_to_LSTM2'))
n.addConnection(FullConnection(n['LSTM'], n['out'], name = 'c_LSTM_to_out'))
n.addConnection(FullConnection(n['LSTM2'], n['out'], name = 'c_LSTM2_to_out'))

n.sortModules()
n.randomize()

#Creating the trainer
trainer = BackpropTrainer(n, ds)

#Training the network
#for i in range (1000):
#   print trainer.train()

#Make predictions

#Plotting the results
pl.plot(time, price)


pl.show()

上面的代码给出: TypeError:izip参数2必须支持迭代

The above code gives: TypeError: izip argument #2 must support iteration

我看到了下面链接的问题,但是我没有成功

I have seen the question linked below however I haven't been successful

事件序列,递归神经网络,PyBrain

在这个很棒的网站上的第一个问题,我们将为您提供任何帮助

First question on this great site, any help is appreciated

推荐答案

我猜想TypeError可以说明一切. priceList[:5]是一个列表,因此是可迭代的,而priceList[6]是一个元素.

I'd guess the TypeError says it all. Whereas priceList[:5] is a list and hence iterable, priceList[6] is a single element.

您可能想要类似的东西

datain = priceList[:5] 
dataout = priceList[6:6]

这将使dataout具有单个元素的列表.

which will make dataout a list with a single element.

这篇关于训练LSTM神经网络以预测pybrain,python中的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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