谁能给我一个 (pybrain) python 中递归神经网络的实际示例,以预测序列的下一个值? (我已经阅读了 pybrain 文档,我认为没有明确的例子。) 我还发现了这个 question .但是我看不到它在更一般的情况下是如何工作的。因此,我想问这里是否有人可以提出一个如何使用循环神经网络预测 pybrain 中序列的下一个值的清晰示例。
举个例子。
例如,我们有一个 [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
下一个值是什么
这个问题可能看起来很懒惰,但我认为没有一个很好的例子来说明如何用 pybrain 做到这一点。
另外:如果存在超过 1 个功能,如何做到这一点:
例子:
例如,我们有几个序列(每个序列有 2 个特征)在 [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.
现在给出一个新序列的开始:
feature 1: 1 3 5 7 2 4 6 7 1 3
feature 2: 1 2 3 7 2 3 4 6 2 4
下一个值是什么
请随意使用您自己的示例,只要它与这些示例相似并且有一些深入的解释。
最佳答案
Issam Laradji 帮助我预测序列序列,但我的 pybrain 版本需要 UnserpervisedDataSet 对象的元组:
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]
要预测更小的序列,只需将其训练为子序列或重叠序列(此处显示重叠):
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]
不太好……
关于python - 请求例如 : Recurrent neural network for predicting next value in a sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16830946/