In this guide, we will discuss about working with feed-forward networks in pybrain. A feed-forward network is a neural network, where the information between nodes moves in the forward direction and will never travel backward. Feed Forward network is the first and the simplest one among the networks available in the artificial neural network. The information is passed from the input nodes, next to the hidden nodes and later to the output node.
In this chapter we are going to discuss how to −
- Create Feed-Forward Networks
- Add Connection and Modules to FFN
Creating a Feed Forward Network
You can use the python IDE of your choice, i.e., PyCharm. In this, we are using Visual Studio Code to write the code and will execute the same in terminal.
To create a feedforward network, we need to import it from pybrain.structure as shown below −
ffn.py
from pybrain.structure import FeedForwardNetwork network = FeedForwardNetwork() print(network)
Execute ffn.py as shown below −
C:\pybrain\pybrain\src>python ffn.py FeedForwardNetwork-0 Modules: [] Connections: []
We have not added any modules and connections to the feedforward network. Hence the network shows empty arrays for Modules and Connections.
Adding Modules and Connections
First we will create input, hidden, output layers and add the same to the modules as shown below −
ffy.py
from pybrain.structure import FeedForwardNetwork from pybrain.structure import LinearLayer, SigmoidLayer network = FeedForwardNetwork() #creating layer for input => 2 , hidden=> 3 and output=>1 inputLayer = LinearLayer(2) hiddenLayer = SigmoidLayer(3) outputLayer = LinearLayer(1) #adding the layer to feedforward network network.addInputModule(inputLayer) network.addModule(hiddenLayer) network.addOutputModule(outputLayer) print(network)
Output
C:\pybrain\pybrain\src>python ffn.py FeedForwardNetwork-3 Modules: [] Connections: []
We are still getting the modules and connections as empty. We need to provide a connection to the modules created as shown below −
Here is the code where we have created a connection between input, hidden and output layers and add the connection to the network.
ffy.py
from pybrain.structure import FeedForwardNetwork from pybrain.structure import LinearLayer, SigmoidLayer from pybrain.structure import FullConnection network = FeedForwardNetwork() #creating layer for input => 2 , hidden=> 3 and output=>1 inputLayer = LinearLayer(2) hiddenLayer = SigmoidLayer(3) outputLayer = LinearLayer(1) #adding the layer to feedforward network network.addInputModule(inputLayer) network.addModule(hiddenLayer) network.addOutputModule(outputLayer) #Create connection between input ,hidden and output input_to_hidden = FullConnection(inputLayer, hiddenLayer) hidden_to_output = FullConnection(hiddenLayer, outputLayer) #add connection to the network network.addConnection(input_to_hidden) network.addConnection(hidden_to_output) print(network)
Output
C:\pybrain\pybrain\src>python ffn.py FeedForwardNetwork-3 Modules: [] Connections: []
We are still not able to get the modules and connections. Let us now add the final step, i.e., we need to add the sortModules() method as shown below −
ffy.py
from pybrain.structure import FeedForwardNetwork from pybrain.structure import LinearLayer, SigmoidLayer from pybrain.structure import FullConnection network = FeedForwardNetwork() #creating layer for input => 2 , hidden=> 3 and output=>1 inputLayer = LinearLayer(2) hiddenLayer = SigmoidLayer(3) outputLayer = LinearLayer(1) #adding the layer to feedforward network network.addInputModule(inputLayer) network.addModule(hiddenLayer) network.addOutputModule(outputLayer) #Create connection between input ,hidden and output input_to_hidden = FullConnection(inputLayer, hiddenLayer) hidden_to_output = FullConnection(hiddenLayer, outputLayer) #add connection to the network network.addConnection(input_to_hidden) network.addConnection(hidden_to_output) network.sortModules() print(network)
Output
C:\pybrain\pybrain\src>python ffn.py FeedForwardNetwork-6 Modules: [<LinearLayer 'LinearLayer-3'gt;, <SigmoidLayer 'SigmoidLayer-7'>, <LinearLayer 'LinearLayer-8'>] Connections: [<FullConnection 'FullConnection-4': 'SigmoidLayer-7' -> 'LinearLayer-8'>, <FullConnection 'FullConnection-5': 'LinearLayer-3' -> 'SigmoidLayer-7'>]
We are now able to see the modules and the connections details for feedforwardnetwork.
Next Topic : Click Here
Pingback: PyBrain - Testing Network | Adglob Infosystem Pvt Ltd