pythonneural-networkneurolab

Showing Neurolabs Weights/Bias for each node?


Is there a simple way to show the bias or weight for each property that I feed into a ANN developed using neurolab after it has already been trained?


Solution

  • Yes you can see all the layer's weights and biases. through using

    net.layers[i].np['w'] for weights

    net.layers[i].np['b'] for biases

    To change them manually yourself you just have to use [:] added to the end and set them to a numpy array.

    here's a sample test code that i used on a simple network with 3 layers (1 input layer, 1 hidden layer and 1 output layer).

    import neurolab as nl
    import numpy as np
    
    net = nl.net.newff([[0,1]] * 3, [4,2])
    
    net.save("test.net")
    
    net = nl.load("test.net")
    # show layer weights and biases
    for i in range(0,len(net.layers)):
        print "Net layer", i
        print net.layers[i].np['w']
        print "Net bias", i
        print net.layers[i].np['b']
    
    #try setting layer weights
    net.layers[0].np['w'][:] = np.array ([[0,1,2],  
                                         [3,4,5],  
                                         [4,5,6],  
                                         [6,7,8]]
                                         )
    
    
    # show layer weights and biases 
    for i in range(0,len(net.layers)):
        print "Net layer", i
        print net.layers[i].np['w']
        print "Net bias", i
        print net.layers[i].np['b']