请求示例:用于预测序列中下一个值的循环神经网络 [英] Request for example: Recurrent neural network for predicting next value in a sequence

查看:25
本文介绍了请求示例:用于预测序列中下一个值的循环神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个在(pybrain)python 中循环神经网络的实用示例,以便预测序列的下一个值?(我已经阅读了 pybrain 文档,我认为没有明确的例子.)我还发现了这个问题.但我看不出它在更一般的情况下是如何工作的.因此,我问这里是否有人可以找出一个清晰的例子,说明如何使用循环神经网络预测 pybrain 中序列的下一个值.

Can anyone give me a practicale example of a recurrent neural network in (pybrain) python in order to predict the next value of a sequence ? (I've read the pybrain documentation and there is no clear example for it I think.) I also found this question. But I fail to see how it works in a more general case. So therefore I'm asking if anyone here could work out a clear example of how to predict the next value of a sequence in pybrain, with a recurrent neural network.

举个例子.

例如,我们有一个范围为 [1,7] 的数字序列.

Say for example we have a sequence of numbers in the range [1,7].

First run (So first example): 1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6

Second run (So second example): 1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6

Third run (So third example): 1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7

and so on.

现在给出一个新序列的开始:1 3 5 7 2 4 6 7 1 3

Now given for example the start of a new sequence: 1 3 5 7 2 4 6 7 1 3

下一个值是什么

这个问题可能看起来很懒,但我认为缺乏一个很好的例子来说明如何用 pybrain 做到这一点.

This question might seem lazy, but I think there lacks a good and decent example of how to do this with pybrain.

另外:如果存在超过 1 个功能,如何做到这一点:

示例:

比如说我们有几个序列(每个序列有 2 个特征)在 [1,7] 范围内.

Say for example we have several sequences (each sequence having 2 features) in the range [1,7].

First run (So first example): feature1: 1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6
                              feature2: 1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7


Second run (So second example): feature1: 1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6
                                feature2: 1 2 3 7 2 3 4 6 2 3 5 6 7 2 4 7 1 3 3 5 6    

Third run (So third example): feature1: 1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7
                              feature2: 1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6

and so on.

现在给出一个新序列的开始:

Now given for example the start of a new sequences:

                                            feature 1: 1 3 5 7 2 4 6 7 1 3

                                            feature 2: 1 2 3 7 2 3 4 6 2 4

下一个值是什么

请随意使用您自己的示例,只要它与这些示例相似并且有一些深入的解释.

Feel free to use your own example as long it is similar to these examples and has some in depth explanation.

推荐答案

Issam Laradji 帮助我预测序列序列,除了我的 pybrain 版本需要一个用于 UnserpervisedDataSet 对象的元组:

Issam Laradji's worked for me to predict sequence of sequences, except my version of pybrain required a tuple for the UnserpervisedDataSet object:

from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.datasets import SupervisedDataSet,UnsupervisedDataSet
from pybrain.structure import LinearLayer
ds = SupervisedDataSet(21, 21)
ds.addSample(map(int,'1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6'.split()),map(int,'1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6'.split()))
ds.addSample(map(int,'1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6'.split()),map(int,'1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7'.split()))
net = buildNetwork(21, 20, 21, outclass=LinearLayer,bias=True, recurrent=True)
trainer = BackpropTrainer(net, ds)
trainer.trainEpochs(100)
ts = UnsupervisedDataSet(21,)
ts.addSample(map(int,'1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7'.split()))
[ int(round(i)) for i in net.activateOnDataset(ts)[0]]

给出:

<代码>=>[1, 2, 5, 6, 2, 4, 5, 6, 1, 2, 5, 6, 7, 1, 4, 6, 1, 2, 2, 3, 6]

要预测较小的序列,只需将其训练为子序列或重叠序列(此处显示重叠):

To predict smaller sequences, just train it up as such, either as sub sequences or as overlapping sequences (overlapping shown here):

from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.datasets import SupervisedDataSet,UnsupervisedDataSet
from pybrain.structure import LinearLayer
ds = SupervisedDataSet(10, 11)
z = map(int,'1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6 1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6 1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7'.split())
obsLen = 10
predLen = 11
for i in xrange(len(z)):
  if i+(obsLen-1)+predLen < len(z):
    ds.addSample([z[d] for d in range(i,i+obsLen)],[z[d] for d in range(i+1,i+1+predLen)])

net = buildNetwork(10, 20, 11, outclass=LinearLayer,bias=True, recurrent=True)
trainer = BackpropTrainer(net, ds)
trainer.trainEpochs(100)
ts = UnsupervisedDataSet(10,)
ts.addSample(map(int,'1 3 5 7 2 4 6 7 1 3'.split()))
[ int(round(i)) for i in net.activateOnDataset(ts)[0]]

给出:

<代码>=>[3, 5, 6, 2, 4, 5, 6, 1, 2, 5, 6]

不太好...

这篇关于请求示例:用于预测序列中下一个值的循环神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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