Search code examples
pythonpybrain

How to build a neural network with pybrain?


I am new to pybrain and I am having a lot of problem in building a neural network. The documentation is not very clear to me and I did not find a lot of examples in the web.

I would like a neural network with one input, 1 hidden layer, 1 output.

x--->f1(x),f2(x),...,b---->g(z)

It should be a simple example. The hidden layer has different function and a bias unit. For this example we can consider f1=f2=sigmoid , g is a custom function.

This is what I have done so far but I am not sure at all that what I am doing is right.

And I have no idea of how to add the bias unit on the hidden layer.

class gLayer(NeuronLayer):

    def _forwardImplementation(self, inbuf, outbuf):
        outbuf[:]=g(inbuf)


    def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
      inerr[:]=derivative(g,inbuf)*outerr


print "build a network"

#Layer
inLayer=LinearLayer(1)
hLayer=SigmoidLayer(2)
outLayer=gLayer(1)
net=FeedForwardNetwork()
net.addInputModule(inLayer)
net.addModule(hLayer)
net.addOutputModule(outLayer)
#connection
in_to_hidden = FullConnection(inLayer, hLayer)
hidden_to_out = FullConnection(hLayer, outLayer)
net.addConnection(in_to_hidden)
net.addConnection(hidden_to_out)
net.sortModules()

Solution

  • You could add a BiasUnit as another hidden layer.

    hBiasLayer=BiasUnit()
    
    net.addModule(hBiasLayer)