Mercurial > projects > aid
annotate trunk/aid/nn/perceptron.d @ 3:314d68bafeff
Backprop and backprop_test added (no testing).
author | revcompgeek |
---|---|
date | Fri, 11 Apr 2008 18:12:55 -0600 |
parents | 9655c8362b25 |
children | ff92c77006c7 |
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 |
3 | 3 import aid.nn.outputFunctions; |
4 import aid.misc; | |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
5 import std.math; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
6 import std.string; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
7 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
8 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
9 class perceptron { |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
10 private int numInputs; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
11 private double[] weights; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
12 private OutputFunction func; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
13 public double learningRate; |
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 * This is the constructor for loading the neural network from a string. |
3 | 17 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
18 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
19 * 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
|
20 * |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
21 * Throws: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
22 * 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
|
23 */ |
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 public this(char[] savedString){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
26 //TODO: Impliment loading! |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
27 throw new Exception("Not implimented."); |
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 // 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
|
31 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
|
32 this.numInputs = numInputs + 1; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
33 weights.length = numInputs + 1; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
34 func = f; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
35 this.learningRate = learningRate; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
36 if(randomize){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
37 for(int i = 0; i < this.numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
38 weights[i] = rnd() * 2 - 1; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
39 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
40 } else { |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
41 for(int i = 0; i < this.numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
42 weights[i] = 0; |
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 } |
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 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
47 /** |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
48 * Evaluates the neural network. |
3 | 49 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
50 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
51 * inputs = The set of inputs to evaluate. |
3 | 52 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
53 * Returns: 1 to indicate true, -1 for false |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
54 */ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
55 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
56 double evaluate(double[] inputs){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
57 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
|
58 double total = weights[0]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
59 for(int i = 1; i < numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
60 total += inputs[i-1] * weights[i]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
61 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
62 if(func != null) return func(total); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
63 return total; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
64 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
65 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
66 public double[] getWeights(){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
67 return weights.dup; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
68 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
69 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
70 /** |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
71 * 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
|
72 * |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
73 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
74 * inputs = The array of inputs to the nerual network. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
75 * targetOutput = The output that the nerual network should give. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
76 */ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
77 /* 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
|
78 */ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
79 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
80 void train(double[] inputs,double targetOutput){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
81 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
|
82 double output = evaluate(inputs); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
83 double error = this.learningRate * (targetOutput - output); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
84 weights[0] += error; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
85 for(int i = 1; i < numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
86 weights[i] += error * inputs[i-1]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
87 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
88 } |
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 /** |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
91 * Calculates the error based on the sum squared error function. |
3 | 92 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
93 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
94 * inputs = An array of arrays of all testing inputs. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
95 * 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
|
96 * |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
97 * Returns: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
98 * The error value. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
99 */ |
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 double getErrorValue(double[][] inputsArray, double[] outputsArray){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
102 double total = 0; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
103 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
|
104 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
|
105 double output,temp; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
106 for(int i = 0; i < inputsArray.length; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
107 output=evaluate(inputsArray[i]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
108 temp = outputsArray[i] - output; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
109 total += temp*temp; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
110 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
111 return total*0.5; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
112 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
113 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
114 |
3 | 115 // TODO: Impliment loading and saving of files |