How to load training data in PyBrain?

痞子三分冷 提交于 2019-12-21 09:33:11

问题


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[:,:6])
ds.setField('target',  dataset.values[:,6:])
del dataset

and you are good to go.



来源:https://stackoverflow.com/questions/8139822/how-to-load-training-data-in-pybrain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!