Friday, June 12, 2015

Creating Neural Networks with Python PyBrain

Let's learn how to create a simple artificial neural network (ann) with PyBrain tool kit. Before jumping on to the coding let's give ourselves a heads up about the inside creation of the network. I assume you have done your research about what a neural network is and what its capabilities are. As we know ANN's communicate similar to neurons . Let me explain to you through a diagram. 

The circles in the diagram denote nodes. As per this example we have three input nodes, 4 middle layer nodes and 2 output nodes. When it comes to the ANN development we do not deal with the middle layer node series, a point to note would be, higher the number of hidden nodes, higher will be the complexity thus resulting in accurate results. 

A single node from the input layer will communicate with every single node in the hidden layer therefore when it comes to writing code we initialize the sizes of all three layers respectively.

Lets get started with the code

Open up your Python shell and import the following libraries.

Now we have the supporting libraries imported to define a neural network, so lets define a neural network by passing in three parameters to denote the input nodes, hidden nodes and output nodes. We shall pass 3,5,2 respectively as per the above diagram

The ANN has been defined, now the network has to be activated with our set of inputs. We have defined 3 input nodes therefore let's activate the ANN with 3 inputs. (XOR function)

We have activated the network with three inputs, what's next is to define our dataset of which we will be using to train. we refer to it as the training data set, so lets quickly have it defined. The dataset takes in 2 parameters. First parameter denotes the width of the dataset, in our case its 3 and the second parameter is the output which is 2 in our case.

The dataset is now defined, lets add sample values to our dataset to train it. Lets add in sample inputs
to the dataset. 2 parameters are passed to the sample data set as well. 1st param is set of inputs and the 2nd is the expected output.

The dataset is now ready to be trained. Lets train the dataset against the input.

let's execute the script in command prompt to see the value.

1 comment:

  1. Guys, the diagram contains only 4 middle layer nodes but in text below I've mentioned 5, please ignore the mistake. Let's assume the graph contains 5 middle layer nodes.