How to load training data in PyBrain?

2019-04-06 12:52发布

问题:

I am trying to use PyBrain for some simple NN training. What I don't know how to do is to load the training data from a file. It is not explained in their website anywhere. I don't care about the format because I can build it now, but I need to do it in a file instead of adding row by row manually, because I will have several hundreds of rows.

回答1:

Here is how I did it:

ds = SupervisedDataSet(6,3)

tf = open('mycsvfile.csv','r')

for line in tf.readlines():
    data = [float(x) for x in line.strip().split(',') if x != '']
    indata =  tuple(data[:6])
    outdata = tuple(data[6:])
    ds.addSample(indata,outdata)

n = buildNetwork(ds.indim,8,8,ds.outdim,recurrent=True)
t = BackpropTrainer(n,learningrate=0.01,momentum=0.5,verbose=True)
t.trainOnDataset(ds,1000)
t.testOnData(verbose=True)

In this case the neural network has 6 inputs and 3 outputs. The csv file has 9 values on each line separated by a comma. The first 6 values are input values and the last three are outputs.



回答2:

You just use pandas arrays this way

import pandas as pd

ds = SupervisedDataSet(6,3)

dataset = pd.read_csv('mycsvfile.csv','r', delimiter=',',skiprows=1)
ds.setfield('input'  dataset.values[:,0:6])
ds.setfield('target',  dataset.values[:,-2:-1])

and you are good to go.