pybrain: how to print a network (nodes and weights)

Dr Sokoban picture Dr Sokoban · Nov 16, 2011 · Viewed 8.9k times · Source

finally I managed to train a network from a file :) Now I want to print the nodes and the weights, especially the weights, because I want to train the network with pybrain and then implement a NN somewhere else that will use it.

I need a way to print the layers, the nodes and the weight between nodes, so that I can easily replicate it. So far I see I can access the layers using n['in'] for example, and then for example I can do:

dir(n['in']) ['class', 'delattr', 'dict', 'doc', 'format', 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', '_backwardImplementation', '_forwardImplementation', '_generateName', '_getName', '_growBuffers', '_name', '_nameIds', '_resetBuffers', '_setName', 'activate', 'activateOnDataset', 'argdict', 'backActivate', 'backward', 'bufferlist', 'dim', 'forward', 'getName', 'indim', 'inputbuffer', 'inputerror', 'name', 'offset', 'outdim', 'outputbuffer', 'outputerror', 'paramdim', 'reset', 'sequential', 'setArgs', 'setName', 'shift', 'whichNeuron']

but I dont see how I can access the weights here. There is also the params attribute, for example my network is 2 4 1 with bias, and it says:

n.params array([-0.8167133 , 1.00077451, -0.7591257 , -1.1150532 , -1.58789386, 0.11625991, 0.98547457, -0.99397871, -1.8324281 , -2.42200963, 1.90617387, 1.93741167, -2.88433965, 0.27449852, -1.52606976, 2.39446258, 3.01359547])

Hard to say what is what, at least with weight connects which nodes. That's all I need.

Answer

schaul picture schaul · Nov 17, 2011

There are many ways to access the internals of a network, namely through its "modules" list or its "connections" dictionary. Parameters are stored within those connections or modules. For example, the following should print all this information for an arbitrary network:

for mod in net.modules:
    print("Module:", mod.name)
    if mod.paramdim > 0:
        print("--parameters:", mod.params)
    for conn in net.connections[mod]:
        print("-connection to", conn.outmod.name)
        if conn.paramdim > 0:
             print("- parameters", conn.params)
    if hasattr(net, "recurrentConns"):
        print("Recurrent connections")
        for conn in net.recurrentConns:
            print("-", conn.inmod.name, " to", conn.outmod.name)
            if conn.paramdim > 0:
                print("- parameters", conn.params)

If you want something more fine-grained (on the neuron level instead of layer level), you will have to further decompose those parameter vectors -- or, alternatively, construct your network from single-neuron-layers.