Mercurial > projects > aid
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:9655c8362b25 | 3:314d68bafeff |
---|---|
1 module aid.nn.perceptron; | 1 module aid.nn.perceptron; |
2 | 2 |
3 import std.random; | 3 import aid.nn.outputFunctions; |
4 import aid.misc; | |
4 import std.math; | 5 import std.math; |
5 import std.string; | 6 import std.string; |
6 | |
7 double rnd(){ // The function that should be included in every math library! | |
8 return (cast(double)rand())/uint.max; | |
9 } | |
10 | |
11 class InputException : Exception { | |
12 this(char[] message){ | |
13 super(message); | |
14 } | |
15 } | |
16 | |
17 // The output functions | |
18 | |
19 alias double function(double) OutputFunction; | |
20 | |
21 double sign(double y){ | |
22 if(y>0) return 1; | |
23 return -1; | |
24 } | |
25 | |
26 double sigmoid(double x){ | |
27 return 1/(1+exp(-x)); | |
28 } | |
29 | |
30 double tanh(double x){ | |
31 return cast(double)tanh(cast(real)x); | |
32 } | |
33 | |
34 // End output functions | |
35 | 7 |
36 | 8 |
37 class perceptron { | 9 class perceptron { |
38 private int numInputs; | 10 private int numInputs; |
39 private double[] weights; | 11 private double[] weights; |
40 private OutputFunction func; | 12 private OutputFunction func; |
41 public double learningRate; | 13 public double learningRate; |
42 | 14 |
43 /** | 15 /** |
44 * This is the constructor for loading the neural network from a string. | 16 * This is the constructor for loading the neural network from a string. |
45 * | 17 * |
46 * Params: | 18 * Params: |
47 * savedString = The string that was output from the save function. | 19 * savedString = The string that was output from the save function. |
48 * | 20 * |
49 * Throws: | 21 * Throws: |
50 * Throws an InputException when the string is in the wrong format. | 22 * Throws an InputException when the string is in the wrong format. |
72 } | 44 } |
73 } | 45 } |
74 | 46 |
75 /** | 47 /** |
76 * Evaluates the neural network. | 48 * Evaluates the neural network. |
77 * | 49 * |
78 * Params: | 50 * Params: |
79 * inputs = The set of inputs to evaluate. | 51 * inputs = The set of inputs to evaluate. |
80 * | 52 * |
81 * Returns: 1 to indicate true, -1 for false | 53 * Returns: 1 to indicate true, -1 for false |
82 */ | 54 */ |
83 | 55 |
84 double evaluate(double[] inputs){ | 56 double evaluate(double[] inputs){ |
85 if(inputs.length != numInputs-1) throw new InputException("Wrong input length. %d %d"); | 57 if(inputs.length != numInputs-1) throw new InputException("Wrong input length. %d %d"); |
115 } | 87 } |
116 } | 88 } |
117 | 89 |
118 /** | 90 /** |
119 * Calculates the error based on the sum squared error function. | 91 * Calculates the error based on the sum squared error function. |
120 * | 92 * |
121 * Params: | 93 * Params: |
122 * inputs = An array of arrays of all testing inputs. | 94 * inputs = An array of arrays of all testing inputs. |
123 * outputs = An array of all the outputs that the cooresponding inputs should have. | 95 * outputs = An array of all the outputs that the cooresponding inputs should have. |
124 * | 96 * |
125 * Returns: | 97 * Returns: |
138 } | 110 } |
139 return total*0.5; | 111 return total*0.5; |
140 } | 112 } |
141 } | 113 } |
142 | 114 |
115 // TODO: Impliment loading and saving of files |