简体   繁体   English

如何在 PyBrain 中加载训练数据?

[英]How to load training data in PyBrain?

I am trying to use PyBrain for some simple NN training.我正在尝试使用 PyBrain 进行一些简单的 NN 培训。 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.我不关心格式,因为我现在可以构建它,但是我需要在文件中完成它而不是手动逐行添加,因为我将有数百行。

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.在这种情况下,神经网络有 6 个输入和 3 个输出。 The csv file has 9 values on each line separated by a comma. csv 文件每行有 9 个值,用逗号分隔。 The first 6 values are input values and the last three are outputs.前 6 个值是输入值,后三个值是输出值。

You just use a pandas DataFrame this way您只需以这种方式使用Pandas DataFrame

import pandas as pd

dataset = SupervisedDataSet(6,3)

df = pd.read_csv('mycsvfile.csv')

dataset.setField('input', df.values[:,:6]) # this sets the features

y=[[x] for x in df.values[:,:6])] # Do this to avoid IndexError: tuple index out of range
                                  # as the target field should be a list of lists, 
                                  # even if its shape is 1

dataset.setField('target', y)     # this set the target[s] field[s]
del df,y

and you are good to go.你很高兴去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM