Mercurial > projects > aid
annotate trunk/aid/nn/perceptron.d @ 7:b9fe92a2d8ad default tip
Removed old code.
author | revcompgeek |
---|---|
date | Tue, 06 May 2008 22:20:26 -0600 |
parents | ff92c77006c7 |
children |
rev | line source |
---|---|
6
ff92c77006c7
Added support for reading training examples from files.
revcompgeek
parents:
3
diff
changeset
|
1 /** |
ff92c77006c7
Added support for reading training examples from files.
revcompgeek
parents:
3
diff
changeset
|
2 * perceptron.d |
ff92c77006c7
Added support for reading training examples from files.
revcompgeek
parents:
3
diff
changeset
|
3 * Holds the simple perceptron class. |
ff92c77006c7
Added support for reading training examples from files.
revcompgeek
parents:
3
diff
changeset
|
4 */ |
ff92c77006c7
Added support for reading training examples from files.
revcompgeek
parents:
3
diff
changeset
|
5 |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
6 module aid.nn.perceptron; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
7 |
3 | 8 import aid.nn.outputFunctions; |
9 import aid.misc; | |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
10 import std.math; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
11 import std.string; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
12 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
13 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
14 class perceptron { |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
15 private int numInputs; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
16 private double[] weights; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
17 private OutputFunction func; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
18 public double learningRate; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
19 |
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 * This is the constructor for loading the neural network from a string. |
3 | 22 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
23 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
24 * 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
|
25 * |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
26 * Throws: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
27 * 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
|
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 public this(char[] savedString){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
31 //TODO: Impliment loading! |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
32 throw new Exception("Not implimented."); |
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 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
35 // 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
|
36 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
|
37 this.numInputs = numInputs + 1; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
38 weights.length = numInputs + 1; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
39 func = f; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
40 this.learningRate = learningRate; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
41 if(randomize){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
42 for(int i = 0; i < this.numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
43 weights[i] = rnd() * 2 - 1; |
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 } else { |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
46 for(int i = 0; i < this.numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
47 weights[i] = 0; |
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 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
50 } |
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 * Evaluates the neural network. |
3 | 54 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
55 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
56 * inputs = The set of inputs to evaluate. |
3 | 57 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
58 * Returns: 1 to indicate true, -1 for false |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
59 */ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
60 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
61 double evaluate(double[] inputs){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
62 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
|
63 double total = weights[0]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
64 for(int i = 1; i < numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
65 total += inputs[i-1] * weights[i]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
66 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
67 if(func != null) return func(total); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
68 return total; |
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 public double[] getWeights(){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
72 return weights.dup; |
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 * 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
|
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 array of inputs to the nerual network. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
80 * targetOutput = The output that the nerual network should give. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
81 */ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
82 /* 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
|
83 */ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
84 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
85 void train(double[] inputs,double targetOutput){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
86 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
|
87 double output = evaluate(inputs); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
88 double error = this.learningRate * (targetOutput - output); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
89 weights[0] += error; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
90 for(int i = 1; i < numInputs; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
91 weights[i] += error * inputs[i-1]; |
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 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
95 /** |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
96 * Calculates the error based on the sum squared error function. |
3 | 97 * |
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
98 * Params: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
99 * inputs = An array of arrays of all testing inputs. |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
100 * 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
|
101 * |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
102 * Returns: |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
103 * The error value. |
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 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
106 double getErrorValue(double[][] inputsArray, double[] outputsArray){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
107 double total = 0; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
108 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
|
109 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
|
110 double output,temp; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
111 for(int i = 0; i < inputsArray.length; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
112 output=evaluate(inputsArray[i]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
113 temp = outputsArray[i] - output; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
114 total += temp*temp; |
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 return total*0.5; |
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 |
3 | 120 // TODO: Impliment loading and saving of files |