pythonpython-3.xneurolab

Getting Assertion Error when training data in neural network in python?


I have a file BCICIV1bAF3.dat which contain data. The file size is 20x1

This is my code...

In newff function the range i decide based on Min/Max but i dont know how to decide the other parameters. How much hidden layer do i want etc.

import numpy as np
import neurolab as nl

input_data = np.fromfile('BCICIV1bAF3.dat' ,dtype=float)

print(len(input_data))
transformed_input_data = [[x] for x in input_data] # added
print(len(transformed_input_data))
output_data = np.fromfile('test.dat',dtype=float)

transformed_output_data = [[x] for x in output_data] # added

net = nl.net.newff([[-21, -10.5]], [1020, 1])
error = net.train(transformed_input_data, transformed_output_data)
predicted_output = net.sim(input_data)

Input Data:

-10.5 -91.7 -219.8 -227 -190.8 -218.7 -208.2 -205 -214.3 -202 -211.5 -211.1 -208.2 -212.4 -206 -206.7 -211.5 -210.7 -212 -215.1

Output Data:

-5.2 -45.6 -108.6 -112 -94.5 -106.7 -99.6 -98.5 -105.4 -101.2 -106.4 -106.5 -102.4 -105.7 -104 -97.9 -99.5 -101.3 -100.6 -103.7

Error:

Traceback (most recent call last):
  File "NNwork2.py", line 15, in <module>

     error = net.train(transformed_input_data, transformed_output_data)
  File "C:\Python34\lib\site-packages\neurolab\core.py", line 328, in __call__
    assert target.shape[0] == input.shape[0]
AssertionError

How can i train? And simulate the input_data?

If anyone could guide...I'll be very grateful. Thanks


Solution

  • Did you try other training methods? I saw in other answer that it helped, because of a bug in library. Available methods: train_gd, train_gdm, train_gda, train_gdx, train_rprop, train_bfgs (DEFAULT), train_cg

    You can change it by calling:

    net.trainf = nl.train.train_gd

    If you could provide input data (even with changed values) it would be great.

    I tried calling train method for input in form: [0,1,2,3...18,19] and it failed - I had to change input (and target) to [[0],[1],...[18],[19]]

    EDIT:

    Your data is in wrong format, you should transform it to list of lists. I don't have scipy on my machine, but try this:

    import numpy as np
    import neurolab as nl
    
    input_data = np.fromfile('BCICIV1bAF3.dat' ,dtype=float)
    transformed_input_data = [[x] for x in input_data] # added
    
    print(len(transformed_input_data)) # changed
    net = nl.net.newff([[-215.1, -10.5]], [20, 1])
    error = net.train(transformed_input_data, transformed_input_data, epochs=500) # changed
    

    EDIT 2:

    I won't explain what neural network is (I didn't use them in quite a while), but it should look like this when we want to transform 3D input into 2D output with use of 1 hidden layer:

    INPUT [3D] | HIDDEN LAYER | OUTPUT [2D]
                    ----
                   | H1 |
                    ----
       ----
      | X1 |
       ----
                    ----         ----
                   | H2 |       | Y1 |
                    ----         ----
    
    
       ----               
      | X2 |                      
       ----                      
    
                    ----         ----
                   | H3 |       | Y2 |
                    ----         ----
       ----
      | X3 |
       ----
                    ----
                   | H4 |
                    ----
    

    Every X is multiplied by every H and we calculate output. How do we have those H values? They're computed by algorithms during training of neural network. We specify how many hidden layers we want and by trial and error arrive at satisfactory solution. Very important - we should use different data to train and to check ouput of neural network.

    When could we use this particular network? E.g. when calculating how many Big Macs and fries people order at McDonald based on age, salary of client and placement of particular restaurant. It would look like this:

        -----
       | AGE |
        -----
                    ----         ----------
                   | H2 |       | BIG MACS |
                    ----         ----------
    
    
       --------               
      | SALARY |                      
       --------                      
    
                    ----         -----------
                   | H3 |       |   FRIES   |
                    ----         -----------
        -------
       | PLACE |
        -------
                    ----
                   | H4 |
                    ----
    

    So we could say that transformation looks like this f([Age, Salary, Place]) = [Big Macs, Fries]. We may have millions of input and output data records gathered by employees to train our network, so translating into python it would be list of inputs (3D) and we expect list of outputs (2D). E.g. f([[A_1, S_1, P_1], [A_2, S_2, P_2], ... , [A_N, S_N, P_N]]) -> [[BM_1, F_1], [BM_2, F_2], ... , [BM_N, F_N]]

    We want same thing with your data, BUT we want to have both input and output to be 1D, hence we had to "wrap" every element of a list into another list. Same thing with output AND simulation input - you forgot that.

    predicted_output = net.sim(input_data) # this won't work! You should wrap it

    But testing neural network on training data is just wrong - you shouldn't do that