An attempt at neural network programming in Actionscript 3
Categories:
Neural Network Programming
After studying for a couple of tests for school, i had some free time on my hands and i decided to take a shot at implementing a perceptron in actionscript. Wikipedia quotes "The perceptron is a type of artificial neural network invented in 1957 at the Cornell Aeronautical Laboratory by Frank Rosenblatt. It can be seen as the simplest kind of feedforward neural network: a linear classifier."
EDIT: I will be posting more about what this code is, and making the code better, right now its 1 am in the morning and I have an exam tommro !.
Normally this type of stuff is implemented on languages such as Java and C, but i decided to give it a shot in Actionscript, below is the code for a fully working Perceptron, however it is really CPU intensive, it did not work on my 3 year old pc, but it did work on my laptop which is fairly new but even then it took a while. I will optimize the code, but tomorrow after my exam, the code is below for you to learn from. enjoy.
EDIT: I will be posting more about what this code is, and making the code better, right now its 1 am in the morning and I have an exam tommro !.
Normally this type of stuff is implemented on languages such as Java and C, but i decided to give it a shot in Actionscript, below is the code for a fully working Perceptron, however it is really CPU intensive, it did not work on my 3 year old pc, but it did work on my laptop which is fairly new but even then it took a while. I will optimize the code, but tomorrow after my exam, the code is below for you to learn from. enjoy.
- package {
- import flash.display.Sprite;
- public class Perceptron extends Sprite
- {
- public var impressionPattern:Array;
- public var goalPattern:Array;
- public var inputNeurons:int;
- public var outputNeurons:int;
- public var numPattern:int;
- public var weightFactor:Array;
- public function Perceptron()
- {
- // Impression pattern
- this.impressionPattern = new Array(
- [0,0,0,0],
- [0,0,0,1],
- [0,0,1,0],
- [0,0,1,1],
- [0,1,0,0],
- [0,1,0,1],
- [0,1,1,0],
- [0,1,1,1],
- [1,0,0,0],
- [1,0,0,1]
- );
- // Goal Pattern
- this.goalPattern = new Array(
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
- [1, 1, 1, 0, 0, 0, 0, 0, 0, 0 ],
- [1, 1, 1, 1, 0, 0, 0, 0, 0, 0 ],
- [1, 1, 1, 1, 1, 0, 0, 0, 0, 0 ],
- [1, 1, 1, 1, 1, 1, 0, 0, 0, 0 ],
- [1, 1, 1, 1, 1, 1, 1, 0, 0, 0 ],
- [1, 1, 1, 1, 1, 1, 1, 1, 0, 0 ],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ]
- );
- this.inputNeurons = this.impressionPattern[0].length;
- this.outputNeurons = this.goalPattern[0].length;
- this.numPattern = this.impressionPattern.length;
- this.weightFactor = new Array();
- var i:int;
- var k:int;
- for(i = 0; i < 20 ; i++){
- weightFactor[i] = [0,0];
- }
- this.wthdoesthisdo();
- }
- public function delta():void{
- var autoCorrect:Boolean = false;
- var error:Boolean = false;
- var learningFactor:int = 0.2;
- while(!autoCorrect){
- error = false;
- var i:int;
- for(i = 0; i < this.numPattern; i++){
- var output:Array = setOutputVal(i);
- var j:int;
- for(j = 0 ; j < this.outputNeurons ; j++){
- if(this.goalPattern[i][j] != output[j]){
- var k:int;
- for(k =0;k <this.inputNeurons;k++){
- this.weightFactor[k][j] = this.weightFactor[k][j] + learningFactor * this.impressionPattern[i][k] * (this.goalPattern[j][j] - output[j]);
- }
- }
- var z:int;
- for(z=0;z<output.length;z++){
- if(output[z] != this.impressionPattern[i][z]){
- error = true;
- }
- }
- }
- }
- if(!error){
- autoCorrect = true;
- }
- }
- }
- public function setOutputVal(patternNo:int):Array{
- var bias:int = 0.7;
- var result:Array = new Array([this.outputNeurons]);
- var toImpress:Array = new Array()
- toImpress = this.impressionPattern[patternNo];
- var i:int;
- var j:int;
- for(i =0 ; i < toImpress.length ; i++){
- for (j=0;j < result.length;j++){
- var net:int = this.weightFactor[0][j] * toImpress[0] + this.weightFactor[1][j] * toImpress[1] + this.weightFactor[2][j] * toImpress[2] + this.weightFactor[3][j] + toImpress[3];
- if(net > bias){
- result[j] = 1;
- }else{
- result[j] = 0;
- }
- }
- }
- return result;
- }
- public function printMatrix(matrix:Array):void{
- var i:int;
- var j:int;
- for(i = 0; i < matrix.length; i++){
- for(j=0;j<matrix[i].length;j++){
- trace(matrix[i][j]);
- }
- }
- }
- public function wthdoesthisdo():void{
- trace("before learn");
- this.printMatrix(this.weightFactor);
- this.delta();
- trace("after learn");
- this.printMatrix(this.weightFactor);
- }
- }
- }
Posted by Faisal at 12:21 AM | Link | 5 comments