<< January, 2008 >>
SMTWTFS
12345
6789101112
13141516171819
20212223242526
2728293031
Search Blog

Categories
Archives
RSS

Powered by
BlogCFM v1.14

25 January 2008
An attempt at neural network programming in Actionscript 3
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.
  1. package {
  2.     import flash.display.Sprite;
  3.     public class Perceptron extends Sprite
  4.     {
  5.         public var impressionPattern:Array;
  6.         public var goalPattern:Array;
  7.         public var inputNeurons:int;
  8.         public var outputNeurons:int;
  9.         public var numPattern:int;
  10.         public var weightFactor:Array;
  11.         public function Perceptron()
  12.         {
  13.             // Impression pattern
  14.             this.impressionPattern = new Array(
  15.                                                 [0,0,0,0],
  16.                                                 [0,0,0,1],
  17.                                                 [0,0,1,0],
  18.                                                 [0,0,1,1],
  19.                                                 [0,1,0,0],
  20.                                                 [0,1,0,1],
  21.                                                 [0,1,1,0],
  22.                                                 [0,1,1,1],
  23.                                                 [1,0,0,0],
  24.                                                 [1,0,0,1]
  25.                                                 );
  26.             // Goal Pattern
  27.             this.goalPattern = new Array(
  28.                                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  29.                                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  30.                                          [1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
  31.                                          [1, 1, 1, 0, 0, 0, 0, 0, 0, 0 ],
  32.                                          [1, 1, 1, 1, 0, 0, 0, 0, 0, 0 ],
  33.                                          [1, 1, 1, 1, 1, 0, 0, 0, 0, 0 ],
  34.                                          [1, 1, 1, 1, 1, 1, 0, 0, 0, 0 ],
  35.                                          [1, 1, 1, 1, 1, 1, 1, 0, 0, 0 ],
  36.                                          [1, 1, 1, 1, 1, 1, 1, 1, 0, 0 ],
  37.                                          [1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ]
  38.                                          );
  39.                                          
  40.             this.inputNeurons = this.impressionPattern[0].length;
  41.             this.outputNeurons = this.goalPattern[0].length;
  42.             this.numPattern = this.impressionPattern.length;
  43.             this.weightFactor = new Array();
  44.             var i:int;
  45.             var k:int;
  46.             for(i = 0; i < 20 ; i++){
  47.                 weightFactor[i] = [0,0];
  48.                
  49.             }
  50. this.wthdoesthisdo();
  51.            
  52.            
  53.             }
  54.            
  55.             public function delta():void{
  56.                 var autoCorrect:Boolean = false;
  57.                 var error:Boolean = false;
  58.                 var learningFactor:int = 0.2;
  59.                 while(!autoCorrect){
  60.                     error = false;
  61.                     var i:int;
  62.                     for(i = 0; i < this.numPattern; i++){
  63.                         var output:Array = setOutputVal(i);
  64.                             var j:int;
  65.                             for(j = 0 ; j < this.outputNeurons ; j++){
  66.                                 if(this.goalPattern[i][j] != output[j]){
  67.                                     var k:int;
  68.                                     for(k =0;k <this.inputNeurons;k++){
  69.                                         this.weightFactor[k][j] = this.weightFactor[k][j] + learningFactor * this.impressionPattern[i][k] * (this.goalPattern[j][j] - output[j]);
  70.                                     }
  71.                                 }
  72.                                 var z:int;
  73.                                 for(z=0;z<output.length;z++){
  74.                                     if(output[z] != this.impressionPattern[i][z]){
  75.                                         error = true;
  76.                                     }
  77.                                 }
  78.                             }
  79.                     }
  80.                     if(!error){
  81.                         autoCorrect = true;
  82.                     }
  83.                    
  84.                 }
  85.             }
  86.            
  87.    
  88.             public function setOutputVal(patternNo:int):Array{
  89.                 var bias:int = 0.7;
  90.                 var result:Array = new Array([this.outputNeurons]);
  91.                 var toImpress:Array = new Array()
  92.                 toImpress =  this.impressionPattern[patternNo];
  93.                 var i:int;
  94.                 var j:int;
  95.                 for(i =0 ; i < toImpress.length ; i++){
  96.                     for (j=0;j < result.length;j++){
  97.                         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];
  98.                         if(net > bias){
  99.                             result[j] = 1;
  100.                         }else{
  101.                             result[j] = 0;
  102.                         }
  103.                     }
  104.                 }
  105.                 return result;
  106.             }
  107.             public function printMatrix(matrix:Array):void{
  108.                 var i:int;
  109.                 var j:int;
  110.                 for(i = 0; i < matrix.length; i++){
  111.                     for(j=0;j<matrix[i].length;j++){
  112.                         trace(matrix[i][j]);
  113.                        
  114.                    
  115.                     }
  116.                 }
  117.             }
  118.            
  119.             public function wthdoesthisdo():void{
  120.                 trace("before learn");
  121.                 this.printMatrix(this.weightFactor);
  122.                
  123.                 this.delta();
  124.                 trace("after learn");
  125.                
  126.                
  127.                 this.printMatrix(this.weightFactor);
  128.                
  129.             }
  130.            
  131.            
  132.     }
  133. }
Posted by Faisal at 12:21 AM | Link | 5 comments