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

Solution 1:

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.

Solution 2:

Try this, it worked for me:

def pesos_conexiones(n):
    for mod in n.modules:
        for conn in n.connections[mod]:
            print conn
            for cc in range(len(conn.params)):
                print conn.whichBuffers(cc), conn.params[cc]

The result should be like:

<FullConnection 'co1': 'hidden1' -> 'out'>
(0, 0) -0.926912942354
(1, 0) -0.964135087592
<FullConnection 'ci1': 'in' -> 'hidden1'>
(0, 0) -1.22895643048
(1, 0) 2.97080368887
(2, 0) -0.0182867906276
(3, 0) 0.4292544603
(4, 0) 0.817440427069
(0, 1) 1.90099230604
(1, 1) 1.83477578625
(2, 1) -0.285569867513
(3, 1) 0.592193396226
(4, 1) 1.13092061631

Solution 3:

Maybe this helps (PyBrain for Python 3.2)?

C:\tmp\pybrain_examples>\Python32\python.exe
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pybrain.tools.shortcuts import buildNetwork
>>> from pybrain.structure.modules.tanhlayer import TanhLayer
>>> from pybrain.structure.modules.softmax import SoftmaxLayer
>>>
>>> net = buildNetwork(4, 3, 1,bias=True,hiddenclass = TanhLayer, outclass =   SoftmaxLayer)
>>> print(net)
FeedForwardNetwork-8
Modules:
[<BiasUnit 'bias'>, <LinearLayer 'in'>, <TanhLayer 'hidden0'>, <SoftmaxLayer 'out'>]
Connections:
[<FullConnection 'FullConnection-4': 'hidden0' -> 'out'>, <FullConnection   'FullConnection-5': 'bias' -> 'out'>, <FullConnection
'FullConnection-6': 'bias' -> 'hidden0'>, <FullConnection 'FullConnection-7': 'in' -> 'hidden0'>]