Saturday, 16 July 2011

Perceptron in Actionscript

I've always thought neural networks and the idea of computational learning was pretty cool but until now I'd never actually tried to program one. I decided to start out with something simple so I went for the single layer perceptron with the hope of building it up to something a bit smarter.

For those of you who don't know much about neural networks Wikipedia has done a pretty decent job in teaching me everything I know. All the project files are available here and sample source code is provided below. It is a simple solution to the NAND problem using two inputs.

The perceptron class essentially has two functions, train and evaluate. The train function is called 100 times on the training set which adjusts the respective weights on each input. When evaluate is called on a particular inputs an output is produced. The output trace from the below program looks like this:
Initiating new perceptron:
Training 100 times using training set...
Evaluating NAND[0,0] : 1
Evaluating NAND[1,0] : 1
Evaluating NAND[0,1] : 1
Evaluating NAND[1,1] : 0

This is a fast solution to these kinds of problems. I plan on implementing optimisations such as exiting training when the weights change by less than some threshold (i.e the solution limit has been reached), but the end goal is to produce a full library with many kinds of next step. I think the next of these will be a multilayer perceptron which initial research suggests is much more complicated! Enjoy the classes and let me know if you do anything interesting with them.

import flash.display.Sprite;
import org.experimentalized.networks.SingleLayerPerceptron;
import org.experimentalized.networks.objects.Trainer;
import org.experimentalized.networks.objects.TrainerSet;
public class Main extends Sprite
public function Main()
//NAND example
trace("Initiating new perceptron:");
var perceptron:SingleLayerPerceptron = new SingleLayerPerceptron(0.1);
var training_set:TrainerSet = new TrainerSet([ new Trainer([0,0],1),
new Trainer([1,0],1),
new Trainer([0,1],1),
new Trainer([1,1],0)
trace("Training 100 times using training set...");
//Test some evaluations
trace("Evaluating NAND[0,0] :",perceptron.Evaluate([1,0]));
trace("Evaluating NAND[1,0] :",perceptron.Evaluate([1,0]));
trace("Evaluating NAND[0,1] :",perceptron.Evaluate([0,1]));
trace("Evaluating NAND[1,1] :",perceptron.Evaluate([1,1]));

1 comment:

  1. You've included all the great information in this article.
    play game juegos kizi online and play game jogos do friv