我是python和神经网络的新手。我尝试使用我在网上找到的示例代码,但是我收到了错误。我不知道为什么会这样。以下是我使用的代码。
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]]
以下是我得到的错误:
TypeError Traceback (most recent call last)
<ipython-input-3-7000795b45c6> in <module>()
4 from pybrain.structure import LinearLayer
5 ds = SupervisedDataSet(21, 21)
----> 6 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()))
7 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()))
8 net = buildNetwork(21, 20, 21, outclass=LinearLayer,bias=True, recurrent=True)
C:\ProgramData\Anaconda3\lib\site-packages\pybrain\datasets\supervised.py in addSample(self, inp, target)
46 def addSample(self, inp, target):
47 """Add a new sample consisting of `input` and `target`."""
---> 48 self.appendLinked(inp, target)
49
50 def getSample(self, index=None):
C:\ProgramData\Anaconda3\lib\site-packages\pybrain\datasets\dataset.py in appendLinked(self, *args)
214 assert len(args) == len(self.link)
215 for i, l in enumerate(self.link):
--> 216 self._appendUnlinked(l, args[i])
217
218 def getLinked(self, index=None):
C:\ProgramData\Anaconda3\lib\site-packages\pybrain\datasets\dataset.py in _appendUnlinked(self, label, row)
196 self._resize(label)
197
--> 198 self.data[label][self.endmarker[label], :] = row
199 self.endmarker[label] += 1
200
TypeError: float() argument must be a string or a number, not 'map'
非常感谢任何帮助。
答案 0 :(得分:0)
代码适用于Python-2.x
,但在Python-3.x
中,您需要使用list
才能调用每个项目。
举个例子:
ds.addSample( list (map(int,'1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6'.split())), list (map(int,'1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6'.split())))
更新所有示例,效果很好:
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(list(map(int,'1 2 4 6 2 3 4 5 1 3 5 6 7 1 4 7 1 2 3 5 6'.split())),list(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(list(map(int,'1 2 5 6 2 4 4 5 1 2 5 6 7 1 4 6 1 2 3 3 6'.split())),list(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(list(map(int,'1 3 5 7 2 4 6 7 1 3 5 6 7 1 4 6 1 2 2 3 7'.split())))
print ([ int(round(i)) for i in net.activateOnDataset(ts)[0]])