1

I a new to python and neural networks. I tried using a sample code i found online on stack but i get errors. I have no idea why this is occurring. The following is the code i used.

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]]

the following is the errors i get:

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'

Any help would be greatly appreciated.

hoefling
  • 59,418
  • 12
  • 147
  • 194

1 Answers1

0

The code works on Python-2.x but in Python-3.x you need to use list in order to call each item.

As an example:

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())))

Updating all the example, works perfect:

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]])
jgorostegui
  • 1,290
  • 1
  • 8
  • 13