Saturday, December 31, 2016

Simple Single Layer Perceptron in VBA

I've been tinkering with machine learning and AI for some days now. I've come across TensorFlow, what seems to be the standard library to use for machine learning these days. But to use it and to understand what it was actually doing underneath the high level APIs it provides, I searched for the most basic of machine learning codes on the internet.
I found a great C source for a single layer perceptron(a simple linear classifier based on artificial neural network) here by Richard Knop. I studied it and thought it was simple enough to be implemented in Visual Basic 6. And because it would be useful to represent training and test data in a graphical form, I thought Excel VBA would be better.

So, here it is.
What's being done in the excel file is this: 20 $(x,y)$ data that fall in the $(-,+)$ quadrant of the XY plane are generated in random with the constraint $x \in [-5,0]$ and  $y \in [0,10]$ and 20 that lie in the $(+,-)$ quadrant with the constraint $x \in [0,5]$ and $y \in [-10,0]$. These fill the first two columns X and Y of the worksheet. The next column is the $y$ data from the line $y=x$ for checking the prediction validity later whether a given $(x,y)$ falls above or below this line, which we know beforehand and with certainty that divides the two classes of data linearly. So, the fourth column Bool represents what the prediction should be i.e. it is a column of the boolean expression : column 2 $>$ column 3, for use as a benchmark and more importantly for use as the training data for the algorithm. The next column Prediction is the actual output of the perceptron after being fed all the 40 training datasets of $(col.1,col.2,col.4)$. The remaining two columns are only auxiliary and show a measure of the overall accuracy of the predictions it made on the same training datasets used as test.
And, since the activation function used in this single layer perceptron is just a unit step/threshold function $$t(p(x,y)) \text{ where,}\\  p(x,y)=w_1x+w_2y+w_3$$ that returns 1 for any non-negative input i.e. $>=0$, the function $p(x,y)$ is essentially the equation of the line that, the algorithm guesses, best separates the two classes of data.