annotate trunk/aid/nn/perceptron.d @ 2:9655c8362b25

Added the Perceptron class and the perceptron_test testing program.
author revcompgeek
date Sat, 05 Apr 2008 23:41:30 -0600
parents
children 314d68bafeff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
1 module aid.nn.perceptron;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
2
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
3 import std.random;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
4 import std.math;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
5 import std.string;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
6
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
7 double rnd(){ // The function that should be included in every math library!
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
8 return (cast(double)rand())/uint.max;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
9 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
10
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
11 class InputException : Exception {
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
12 this(char[] message){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
13 super(message);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
14 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
15 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
16
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
17 // The output functions
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
18
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
19 alias double function(double) OutputFunction;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
20
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
21 double sign(double y){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
22 if(y>0) return 1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
23 return -1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
24 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
25
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
26 double sigmoid(double x){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
27 return 1/(1+exp(-x));
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
28 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
29
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
30 double tanh(double x){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
31 return cast(double)tanh(cast(real)x);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
32 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
33
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
34 // End output functions
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
35
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
36
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
37 class perceptron {
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
38 private int numInputs;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
39 private double[] weights;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
40 private OutputFunction func;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
41 public double learningRate;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
42
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
43 /**
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
44 * This is the constructor for loading the neural network from a string.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
45 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
46 * Params:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
47 * savedString = The string that was output from the save function.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
48 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
49 * Throws:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
50 * Throws an InputException when the string is in the wrong format.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
51 */
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
52
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
53 public this(char[] savedString){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
54 //TODO: Impliment loading!
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
55 throw new Exception("Not implimented.");
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
56 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
57
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
58 // This is private because one type of perceptron training requires the use of the sign function.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
59 this(int numInputs, double learningRate=0.3, bool randomize=true,OutputFunction f=null){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
60 this.numInputs = numInputs + 1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
61 weights.length = numInputs + 1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
62 func = f;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
63 this.learningRate = learningRate;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
64 if(randomize){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
65 for(int i = 0; i < this.numInputs; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
66 weights[i] = rnd() * 2 - 1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
67 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
68 } else {
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
69 for(int i = 0; i < this.numInputs; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
70 weights[i] = 0;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
71 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
72 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
73 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
74
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
75 /**
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
76 * Evaluates the neural network.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
77 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
78 * Params:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
79 * inputs = The set of inputs to evaluate.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
80 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
81 * Returns: 1 to indicate true, -1 for false
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
82 */
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
83
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
84 double evaluate(double[] inputs){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
85 if(inputs.length != numInputs-1) throw new InputException("Wrong input length. %d %d");
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
86 double total = weights[0];
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
87 for(int i = 1; i < numInputs; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
88 total += inputs[i-1] * weights[i];
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
89 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
90 if(func != null) return func(total);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
91 return total;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
92 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
93
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
94 public double[] getWeights(){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
95 return weights.dup;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
96 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
97
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
98 /**
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
99 * Trains the neural network. This must be overloaded in a subclass.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
100 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
101 * Params:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
102 * inputs = The array of inputs to the nerual network.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
103 * targetOutput = The output that the nerual network should give.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
104 */
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
105 /* Returns: True if it trained the network, false if not.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
106 */
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
107
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
108 void train(double[] inputs,double targetOutput){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
109 if(inputs.length != numInputs-1) throw new InputException("Wrong input length.");
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
110 double output = evaluate(inputs);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
111 double error = this.learningRate * (targetOutput - output);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
112 weights[0] += error;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
113 for(int i = 1; i < numInputs; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
114 weights[i] += error * inputs[i-1];
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
115 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
116 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
117
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
118 /**
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
119 * Calculates the error based on the sum squared error function.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
120 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
121 * Params:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
122 * inputs = An array of arrays of all testing inputs.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
123 * outputs = An array of all the outputs that the cooresponding inputs should have.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
124 *
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
125 * Returns:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
126 * The error value.
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
127 */
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
128
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
129 double getErrorValue(double[][] inputsArray, double[] outputsArray){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
130 double total = 0;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
131 if(inputsArray.length != outputsArray.length) throw new InputException("inputsArray and outputsArray must be the same length");
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
132 if(inputsArray.length < 1) throw new InputException("Must have at least 1 training example");
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
133 double output,temp;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
134 for(int i = 0; i < inputsArray.length; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
135 output=evaluate(inputsArray[i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
136 temp = outputsArray[i] - output;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
137 total += temp*temp;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
138 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
139 return total*0.5;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
140 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
141 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff changeset
142