Following the PyBrain documentation, Building Networks with Modules and Connections, I'm building a neural network piecewise (in contrast to using the buildNetwork shortcut). I'm constructing a simple 3-layer (input, hidden, output) neural network. How do I properly add a bias unit?
I'm guessing I construct a BiasUnit
module as in:
b = BiasUnit(name='bias')
network.addModule(b)
Is this the right way? Do I have to create FullConnection
object? If so, what should I be connecting?
PyBrain stands for Python-Based Reinforcement Learning, Artificial Intelligence, and Neural Networks Library. It is a modular machine learning library n python that contains very powerful and easy-to-use algorithms used to aid in a variety of machine learning tasks.
Pybrain provides Networkreader, which is there in pybrain. tools. customxml, using which we can load the saved network(XML file). In this way, we can use NetworkReader to read the XML file of the network.
PyBrain already includes a function that does just that, gradientCheck(). You can pass it to any network containing a structural component that you have programmed. It will check if the numeric gradients are (roughly) equal to the gradient specified by the _backwardImplementation() methods.
Realized PyBrain is open source and I have the source code sitting in my Python directory. I opened the C:\Python27\Lib\site-packages\pybrain\tools\shortcuts.py file. Inside this file I located the buildNetwork function and saw how it adds BiasUnit's. The relevant code is here:
...
n = Network()
# linear input layer
n.addInputModule(LinearLayer(layers[0], name='in'))
# output layer of type 'outclass'
n.addOutputModule(opt['outclass'](layers[-1], name='out'))
if opt['bias']:
# add bias module and connection to out module, if desired
n.addModule(BiasUnit(name='bias'))
if opt['outputbias']:
n.addConnection(FullConnection(n['bias'], n['out']))
# arbitrary number of hidden layers of type 'hiddenclass'
for i, num in enumerate(layers[1:-1]):
layername = 'hidden%i' % i
n.addModule(opt['hiddenclass'](num, name=layername))
if opt['bias']:
# also connect all the layers with the bias
n.addConnection(FullConnection(n['bias'], n[layername]))
# connections between hidden layers
...
Basically it looks like it creates a single BiasUnit and connects it to each hidden layer and optionally to the output layer as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With