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]));

Thursday, 7 July 2011

Colour Wheel - HSV Colour Space

I've just been playing around with colour wheels and colour representation in flash. I had a think about whether it was possible to represent every single colour in the RGB spectrum on a single 2D image. Closer inspection reveals that it isn't possible because RGB is made up of three components - red, green and blue - and therefore needs three dimensions. This is why if you look up different colour representations on Wikipedia you might see them represented by cones, or cubes. I decided to work in the HSV colour space which is  formed of Hue (rotation), Saturation (The amount of colour compared to black/white) and Value (Essentially the brightness). The resulting colour wheel can be seen below:

Just click on the image to launch the app. By rolling over the wheel the colour in the box will show the selected colour. Use the up/down arrow keys to alter the "V" value of the wheel. This should allow you to view a complete (discrete) representation of every colour in the RGB spectrum. 

If you're interested in source code just click here to download the FlashBuilder project and AS files. Enjoy!