浮点数误差

2024-10-02 18:20:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

任何帮助都将不胜感激。你知道吗


Tags: infromimportselftruemapnetds
1条回答
网友
1楼 · 发布于 2024-10-02 18:20:39

代码在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]])

相关问题 更多 >