Pybrain切片索引必须是整数或None,或者使用numpy 1.11.0的__index__方法

时间:2017-09-28 13:39:11

标签: python numpy pybrain

我正在关注PyBrain的evolino教程并运行到此(切片索引必须是整数或无或具有索引方法)错误。我已经google了这个错误,发现PyBrain和Numpy之间存在一些兼容性问题,而不是版本1.12。我已将Numpy降级到版本1.11.0,但问题仍然存在

Traceback (most recent call last):
  File "/home/TA/neural_network.py", line 70, in <module>
    trainer.trainEpochs(1)
  File "/usr/local/lib/python2.7/dist-packages/pybrain/supervised/trainers/trainer.py", line 37, in trainEpochs
    self.train(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pybrain/supervised/trainers/evolino.py", line 130, in train
    filter.apply(self._population)
  File "/usr/local/lib/python2.7/dist-packages/pybrain/supervised/evolino/filter.py", line 115, in apply
    fitness = self._evaluateNet(net, dataset, self.wtRatio)
  File "/usr/local/lib/python2.7/dist-packages/pybrain/supervised/evolino/filter.py", line 57, in _evaluateNet
    sequence = dataset.getSequence(i)[1]
  File "/usr/local/lib/python2.7/dist-packages/pybrain/datasets/sequential.py", line 55, in getSequence
    return [self._getSequenceField(index, l) for l in self.link]
  File "/usr/local/lib/python2.7/dist-packages/pybrain/datasets/sequential.py", line 44, in _getSequenceField
    return self.getField(field)[ravel(self.getField('sequence_index'))[index]:]            
TypeError: slice indices must be integers or None or have an __index__ method

Process finished with exit code 1

代码:

from pylab import plot, show, ion, cla, subplot, title, figlegend, draw
import numpy

from pybrain.structure.modules.evolinonetwork import EvolinoNetwork
from pybrain.supervised.trainers.evolino import EvolinoTrainer
from sin_generator import generateSuperimposedSineData

print()
print("=== Learning to extrapolate 5 superimposed sine waves ===")
print()
sinefreqs = (0.2, 0.311, 0.42, 0.51, 0.74)
metascale = 8.
scale = 0.5 * metascale
stepsize = 0.1 * metascale

# === create training dataset
# the sequences must be stored in the target field
# the input field will be ignored
print ("creating training data")
trnInputSpace = numpy.arange(0*scale, 540*scale, stepsize)
trnData = generateSuperimposedSineData(sinefreqs, trnInputSpace)

# === create testing dataset
print("creating test data")
tstInputSpace = numpy.arange(400*scale, 540*scale, stepsize)
tstData = generateSuperimposedSineData(sinefreqs, tstInputSpace)

# === create the evolino-network
print("creating EvolinoNetwork")
net = EvolinoNetwork(trnData.outdim, 40)

wtRatio = 1./3

# === init an evolino trainer
# it will train network through evolutionary algorithms
print("creating EvolinoTrainer")
trainer = EvolinoTrainer(
    net,
    dataset=trnData,
    subPopulationSize = 20,
    nParents = 8,
    nCombinations = 1,
    initialWeightRange = (-0.01, 0.01),
    backprojectionFactor = 0.001,
    mutationAlpha = 0.001,
    nBurstMutationEpochs = numpy.Infinity,
    wtRatio = wtRatio,
    verbosity = 2
)

# === prepare sequences for extrapolation and plotting
trnSequence = trnData.getField('target')
seperatorIdx = int(len(trnSequence)*wtRatio)
trnSequenceWashout = trnSequence[0:seperatorIdx]
trnSequenceTarget = trnSequence[seperatorIdx:]

tstSequence = tstData.getField('target')
seperatorIdx = int(len(tstSequence)*wtRatio)
tstSequenceWashout = tstSequence[0:seperatorIdx]
tstSequenceTarget = tstSequence[seperatorIdx:]

ion() #switch matplotlib to interactive mode
for i in range(3000):
    print("======================")
    print("====== NEXT RUN ======")
    print("======================")

    print("=== TRAINING")
    #train the network for 1 epoch
    trainer.trainEpochs(1)

    print("=== PLOTTING\n")
    #calculate the nets output for train and test data
    trnSequenceOutput = net.extrapolate(trnSequenceWashout, len(trnSequenceTarget))
    tstSequenceOutput = net.extrapolate(tstSequenceWashout, len(tstSequenceTarget))

    #plot training data
    sp = subplot(211) #switch to the first subplot
    cla() #clear the subplot
    title("Training Set") #set the subplot's title
    sp.set_autoscale_on(True) #enable autoscaling
    targetline = plot(trnSequenceTarget, "r-") #plot the targets
    sp.set_autoscale_on(False) #disable autoscaling
    outputline = plot(trnSequenceOutput, "b-") #plot the actual ouput

    #plot test data
    sp = subplot(212)
    cla()
    title("Test Set")
    sp.set_autoscale_on(True)
    plot(tstSequenceTarget, "r-")
    sp.set_autoscale_on(False)
    plot(tstSequenceOutput, "b-")

    #create a legend
    figlegend((targetline, outputline), ('target', 'output'), ('upper right'))

    #draw everything
    draw()

show()

值:

ravel(self.getField('sequence_index'))[index]

0.0

0 个答案:

没有答案