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

时间:2013-05-30 08:04:52

标签: python machine-learning neural-network time-series pybrain

为了预测序列的下一个值,有人能给我一个(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,7]范围内有几个序列(每个序列有2个特征)。

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

下一个值是什么


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

2 个答案:

答案 0 :(得分:8)

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]

不太好......

答案 1 :(得分:4)

这些步骤旨在在问题的第一部分执行您的要求。

1)创建一个监督数据集,在其参数中需要样本和目标

 ds = SupervisedDataSet(21, 21)
 #add samples (this can be done automatically)
 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()))

后续样本是其前身y的目标或标签x。我们设置了数字21,因为每个样本都有21个数字或功能。

请注意,对于问题后半部分的标准符号,最好将feature1和feature2称为sample1和sample2作为序列,并让feature表示样本中的数字。

2)创建网络,初始化培训师并运行100个时期

net = buildNetwork(21, 20, 21, outclass=LinearLayer,bias=True, recurrent=True)
trainer = BackpropTrainer(net, ds)
trainer.trainEpochs(100)

确保将recurrent参数设置为True

3)创建测试数据

ts = UnsupervisedDataSet(21, 21)
#add the sample to be predicted
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()))

我们创建了一个无监督的数据集,因为我们假设我们没有标签或目标。

4)使用经过训练的网络预测测试样本

net.activateOnDataset(ts)

这应该显示预期的fourth run

的值

对于第二种情况,当一个序列可以有多个样本时,而不是创建一个监督数据集,创建一个序列ds = SequentialDataSet(21,21)。然后,每次获得新序列时,请致电ds.newSequence()并使用ds.addSample()添加您在该序列中调用要素的样本。

希望这是明确的:)

如果您希望获得完整的代码以省去导入库的麻烦,请告诉我。