Thursday, November 28, 2013

Matlab NNtool : Simple Perceptron model

Its been quite a while since my last post. The past few months have been busy as I spent most of the time working on my research work. I'm back with my zeal to share with you what I have been learning.
Note:  Don't worry if you are not accustomed to the word perceptron. The only pre-requisite for this tutorial is your basic understanding of AND gate. If you are still not confident Google "AND gate".
  In the beginning I was looking at neural networks as a theoretical  perspective but to completely understand its true nature we have to implement it. Though it is not possible to implement immediately, we can use tools like NNtool (Neural networks tool) to simulate and analyze the networks. Besides reducing the time consumed, it is easier to do a comparative study on different type of networks. This tutorial is focused on implementing AND gate logic with a simple perceptron model. If you are not sure about how neuron works just imagine a bucket of water. If you keep on adding water (will increase weight) to the bucket at a certain point of time it overflows.
   Please follow the steps carefully. If you are stuck at any step, please mention your query in the comments below.
1. Download Matlab (Dont ask me which version :P i.e..e Any version of Matlab that supports NNtool box can work perfectly. Preferably use the latest version Matlab 2013a/2013b)
2.After installation just start Matlab. (Double click the matlab icon in your desktop after installation :-0)
 3. The truth table for AND gate is as follows:
Where A and B are inputs and A.B is the expected outputs. Now we have to train the neural network in such a way that when ewe give the value of A and B, it should be able to reproduce the output of A.B
4. First we have to define input data as vector matrix. When you type nntool and press enter a popup window will appear. From that popup window press the New button which in turn will produce another popup windows. Under this click the Data tab. Enter name as input_AB, datatype as Input and value as [ 0 0 1 1; 0 1 0 1]. Now if you observe closely the input vector is represented column-wise separated by a semicolon i.e. [A ; B ]. Finally press the create button.
 5. Now it is time to define the target vector. The target vector is represented with name as target_AND, type as Targets and value as [0 0 0 1] (similar to [ A.B ] ). Now press create.
 6. Once Input and target vectors have been defined, the network has to be created. Press the network tab set the name as AND_net, type as Perceptron and respective Input and target data. Don't worry about other parameter (I will explain you that in later tutorials)
 7. Now go back to the earlier popup and double click AND_net. A windows will be created. Under that press the train tab and set the input and target data. Press the train network button.
 8. Once it is trained the performance and the training states occurred during the training process can be analysed.
 9. Now it is time to simulate. Now to provide the input follow step 3 with value as [ 1 ; 1] representing a input 1, 1.
 10. In the Network AND_net popup navigate to the simulate tab. set the input and press Simulate network. Almost done :-)
 11. Now go the nntool popup and double click ANS_net_outputs. BAZZINGA.. we have got the expected output 1. i.e. when we gave the value A and B the perceptron model was able to predict the value of A.B

12. Wonderful isn't it. This is just the simplest of all. Now just try and explore the toolbox and widen your knowledge. (You can understand how your brain works :P)

If you have any queries or suggestions please feel free to contact me ."I always say the minute I stop making mistakes is the minute I stop learning and I've definitely learned a lot".


  1. which part of the total input specifically is used as testing dataset by the nntool?

    1. The [1;1] is used as the simple dataset it means that A=1 and B=1 and corresponding output will be X=1(A AND B)

  2. May God bless u. Very helpful. Thank you so much!

  3. Why do I get the error message, 'Too Many Output Arguments' when trying to simulate? hope you could help. tq

  4. Thank you very much, you made it really clear and helpful.

  5. Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
    CCNA Training in Chennai

  6. If we want to sort positive numbers from negative numbers what are the steps?