Search code examples
pythonneural-networkpybrain

Pybrain exporting a network


After seeing this answer, I am having some trouble exporting my network. Here is the network generation code:

net = buildNetwork(2, 1, 1, bias=False)
sol = net.activate([2,3])
print("solution", sol)

for mod in net.modules:
    for conn in net.connections[mod]:
        print("connection:",conn)
        for cc in range(len(conn.params)):
            print(conn.whichBuffers(cc), conn.params[cc])

output:

solution [ 0.12654066]
connection: <FullConnection 'FullConnection-3': 'hidden0' -> 'out'>
(0, 0) 1.02869832075
connection: <FullConnection 'FullConnection-4': 'in' -> 'hidden0'>
(0, 0) 0.410307885215
(1, 0) -0.928280457049

shouldn't the solution that it kicks out be equal to

(0.410307885215*2-0.928280457049*3)*1.02869832075

which is -2.0206, and not 0.12654066


Solution

  • Okay, so I was forgetting about an activation function. The default activation function is the sigmoid function, which in python you can do easily with

    from scipy.special import expit
    expit((0.410307885215*2-0.928280457049*3))*1.02869832075 = 0.1265406616438563
    

    The next question for me was how to add the bias. The bias is added before you run the sigmoid function. So if you are doing:

    net = buildNetwork(2, 1, 1, bias=True)
    

    Then it essentially builds:

    expit(weight_in1*input1+weight_in2*input2+hidden_bias)*hidden_weight+bias_out
    

    Hopefully that makes sense to anyone having the same issues as me.