pythonneural-networklstmpybrain

Pybrain - SequentialDataSet using Backprop trainer give a slice indices error


I searched throughout the inter-webs for this one, didnt find anything, except for a 'deleted' post on stack overflow.

The problem is the following : Every examples, tutorials on PyBrain LSTM + SequencialDataSet givwa me the following error :

TypeError: slice indices must be integers or None or have an __index__ method

From my previous searches, it seems to be a problem where, after a certain version of python, integer operations returns float instead of integers

However, it seems to work with everyone else using python 2.x like me. Any idea ?

Code (Which is not mine, took from another stackoverflow post) :

from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.datasets import SequentialDataSet
from pybrain.structure import SigmoidLayer, LinearLayer
from pybrain.structure import LSTMLayer

import itertools
import numpy as np

INPUTS = 5
OUTPUTS = 1
HIDDEN = 40

net = buildNetwork(INPUTS, HIDDEN, OUTPUTS, hiddenclass=LSTMLayer,     outclass=LinearLayer, recurrent=True, bias=True)

ds = SequentialDataSet(INPUTS, OUTPUTS)
ds.addSample([0,1,2,3,4],[5])
ds.addSample([5,6,7,8,9],[10])
ds.addSample([10,11,12,13,14],[15])
ds.addSample([16,17,18,19,20],[21])

net.randomize()

trainer = BackpropTrainer(net, ds)

for _ in range(1000):
    print trainer.train()

x=net.activate([0,1,2,3,4])
print x 

Which gives me this error :

File "try3.py", line 27, in <module>
print trainer.train()
File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.3-py2.7.egg/pybrain/supervised/trainers/backprop.py", line 57, in train
for seq in self.ds._provideSequences():
File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.3-py2.7.egg/pybrain/datasets/sequential.py", line 173, in _provideSequences
return iter(map(list, iter(self)))
File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.3-py2.7.egg/pybrain/datasets/sequential.py", line 169, in __iter__
yield self.getSequenceIterator(i)
File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.3-py2.7.egg/pybrain/datasets/sequential.py", line 63, in getSequenceIterator
return zip(*self.getSequence(index))
File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.3-py2.7.egg/pybrain/datasets/sequential.py", line 56, in getSequence
return [self._getSequenceField(index, l) for l in self.link]
File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.3-py2.7.egg/pybrain/datasets/sequential.py", line 45, in _getSequenceField
return self.getField(field)[seq[index]:]
TypeError: slice indices must be integers or None or have an __index__ method

Using :

Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2

Solution

  • Found it, the difference is in pybrain, this fixed it in my setup:

    pip uninstall pybrain
    pip install pybrain
    

    Try that,