Implementing Matlab's weights and bias on Neurolab (Neural Network)
Question: I exported weights and bias from Matlab into python to use with neurolab. My
network has 8 inputs, 3 arrays of input weights, 4 arrays of layered weights
and 4 output neurons. This is my first time of doing this, I'll really need
help to get it done. Below is my implementation and the error I got.

    
    
    import numpy as np
    import neurolab as nl
    
    net = nl.net.newff([[0, 1]], [3, 4])
    input_w=[[-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680],...]
    input_w = np.array(input_w)
    input_w = np.reshape(input_w, (8,3))
    
    layer_w=[[-3.7940,-0.0336,-14.9024],......]
    layer_w = [np.array(x) for x in (layer_w)]
    layer_w = np.reshape(layer_w, (3,4))
    
    input_bias =[0.4747,-1.2475,-1.2470]
    bias_2=np.array([-10.9982,1.9692,5.0705,-0.1236])
    bias_2 = np.reshape(bias_2, (4))
    
    net.layers[0].np['w'][:] = input_w.
    net.layers[1].np['w'][:] = layer_w.
    net.layers[1].np['b'][:] = np.array([input_bias])
    net.layers[0].np['b'][:] = bias_2
    
    print net.sim([[0.015,0.022,0.0,0.0,0.432,0.647,0.831]])
    
    Traceback (most recent call last):
      File "C:\Python27\neural13.py", line 206, in <module>
        net.layers[0].np['w'][:] = input_w
    ValueError: could not broadcast input array from shape (8,3) into shape (3,1)
    

Thanks for your suggestions, and please feel free to ask if you need more
info.


Answer: There are several problems here.

The first problem is that you want 8 input neurons. For that, you need the
first list in newff to have a length of 8 (one min value and one max value for
each of 8 inputs). So you are ending up with only one input, not 8 (hence a
3x1 array instead of a 3x8 array). That can be fixed by changing:

    
    
    net = nl.net.newff([[0, 1]], [3, 4])
    

to:

    
    
    net = nl.net.newff([[0, 1]]*8, [3, 4])
    

which is a shorter way to write:

    
    
    net = nl.net.newff([[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]], [3, 4])
    

The next problem is that python uses a different dimension ordering (by
default) than matlab. So a 2D array with shape (8,3) would have a shape of
(3,8) in numpy. So all of your reshaping is unnecessary.

The third problem is that you have the dimensions for input_bias and bias_2
mixed up. I don't know enough about neurlab to say whether you want to change
`[3, 4]` at the beginning to `[4, 3]`, or whether you want to switch the
biases. I will assume the latter.

The final problem is that you need 8 elements in the input to net.sim, but you
only have 7.

Here is a fixed version of your code, assuming you have the biases mixed up
and with some dummy values to fill in what you left out:

    
    
    import numpy as np
    import neurolab as nl
    
    net = nl.net.newff([[0, 1]]*8, [3, 4])
    
    input_w = np.array([[-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680],
                        [-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680],
                        [-24.1874,24.1622,0.0755,-0.2521,4.4625,-10.7961,6.2183,0.2680]])
    
    layer_w = np.array([[-3.7940,-0.0336,-14.9024],
                        [-3.7940,-0.0336,-14.9024],
                        [-3.7940,-0.0336,-14.9024],
                        [-3.7940,-0.0336,-14.9024]])
    
    input_bias = np.array([0.4747,-1.2475,-1.2470])
    bias_2 = np.array([-10.9982,1.9692,5.0705,-0.1236])
    
    net.layers[0].np['w'][:] = input_w
    net.layers[1].np['w'][:] = layer_w
    net.layers[0].np['b'][:] = input_bias
    net.layers[1].np['b'][:] = bias_2
    
    print net.sim([[0.015,0.022,0.0,0.0,0.432,0.647,0.831]])
    

