How can I use a matrix as a dataset in PyBran?

I am using pybrain to train a simple neural network where the input will be a 7x5 matrix.

The inputs are listed below:

    A = [[0, 0, 1, 0, 0],
    [0, 1, 1, 0, 0],
    [0, 1, 0, 1, 0],
    [0, 1, 0, 1, 0],
    [1, 1, 1, 1, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1]]

E = [[1, 1, 1, 1, 1],
    [1, 0, 0, 0, 0],
    [1, 0, 0, 0, 0],
    [1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0],
    [1, 0, 0, 0, 0],
    [1, 1, 1, 1, 1]]
I = [[0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0]]

O = [[1, 1, 1, 1, 0],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 1, 1, 1, 0]]

U = [[1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [0, 1, 0, 0, 1],
    [0, 0, 1, 1, 0]]

      

I thought of writing something like:

ds = SupervisedDataSet(1, 1)
ds.addSample((A), ("A",))

      

might work, but I get:

ValueError: cannot copy sequence with size 7 to array axis with dimension 1

      

Is there a way to pass these datasets to pyBrain?

+3


source to share


1 answer


You should know first that SupervisedDataSet works with a list, so you will need to convert 2D arrays to a list. You can do it something like this:

def convertToList (matrix):
    list = [ y for x in matrix for y in x]
    return list

      

Then you will need to provide a new list to the SupervisedDataSet method. Also, if you want to use this information to create a network, you must use some number to identify the letter, such as A = 1, E = 2, i = 3, O = 4, U = 5. So that to do this, the second Parameter for the SupervisedDataSet should be just number 1. So you say something like "For a list of 35 elements, these numbers are used to identify one number."



Finally, your code should look like this:

ds = SupervisedDataSet(35, 1)

A2 = convertToList(A)
ds.addSample(A2, (1,))

E2 = convertToList(E)
ds.addSample(E2, (2,))

I2 = convertToList(I)
ds.addSample(I2, (3,))

O2 = convertToList(O)
ds.addSample(O2, (4,))

U2 = convertToList(U)
ds.addSample(U2, (5,))

      

Hope this helps.

+3


source







All Articles