【问题标题】:Float error python neural network浮点错误python神经网络
【发布时间】:2018-01-11 11:37:42
【问题描述】:

我是 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'

任何帮助将不胜感激。

【问题讨论】:

  • 快速查看documentation,尝试将map 函数转换为tuple
  • 可以举例说明你的意思
  • 如以下答案中所建议的,您可以执行tuple(map(...))

标签: python python-3.x pybrain


【解决方案1】:

代码适用于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]])

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2017-08-28
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多