Mercurial > projects > aid
annotate trunk/aid/nn/multilayer/backprop.d @ 5:810d58835f86
Added momentum and stochastic training to backprop.
author | revcompgeek |
---|---|
date | Tue, 15 Apr 2008 14:39:49 -0600 |
parents | 73beed484455 |
children | ff92c77006c7 |
rev | line source |
---|---|
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
1 |
3 | 2 module aid.nn.multilevel.backprop; |
3 | |
4 import aid.nn.outputFunctions; | |
5 import aid.misc; | |
6 import std.random; | |
7 import std.stream; | |
4 | 8 import std.stdio; |
3 | 9 |
10 class Backprop { | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
11 private uint numInputs; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
12 private double[][][] units; // Includes the output units. units[layer][unit][inputWeight] |
3 | 13 private OutputFunctionPtr[] functions; |
4 | 14 public double learningRate; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
15 public double momentum; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
16 private double[][][] oldWeightUpdate; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
17 |
3 | 18 ///Constructor |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
19 public this(uint numInputs, uint[] numUnits, OutputFunctionPtr[] functions, |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
20 double learningRate = 0.03, double momentum = 0.1, |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
21 double value = 0.1, bool randomize = true) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
22 if(numUnits.length == 0) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
23 throw new InputException("numUnits must be greater than 0"); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
24 if(numUnits.length != functions.length) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
25 throw new InputException( |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
26 "numUnits and functions must be the same length"); |
3 | 27 this.numInputs = numInputs; |
28 this.functions = functions; | |
4 | 29 this.learningRate = learningRate; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
30 this.momentum = momentum; |
4 | 31 units.length = numUnits.length; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
32 oldWeightUpdate.length = numUnits.length; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
33 initUnitLayer(0, numUnits[0], numInputs, value, randomize); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
34 for(int i = 1; i < numUnits.length; i++) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
35 initUnitLayer(i, numUnits[i], numUnits[i - 1], value, randomize); |
3 | 36 } |
37 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
38 |
3 | 39 // Helper function to initialize a certain layer. |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
40 private void initUnitLayer(uint layer, uint num, uint numPrev, |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
41 double value, bool randomize) { |
3 | 42 units[layer].length = num; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
43 oldWeightUpdate[layer].length = num; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
44 for(int i = 0; i < num; i++) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
45 units[layer][i].length = numPrev + 1; // include the bias weight |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
46 oldWeightUpdate[layer][i].length = numPrev + 1; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
47 |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
48 for(int j = 0; j < numPrev + 1; j++) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
49 if(randomize) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
50 units[layer][i][j] = rnd() * value * 2 - value; // between -value and value |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
51 else |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
52 units[layer][i][j] = value; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
53 oldWeightUpdate[layer][i][j] = 0; |
3 | 54 } |
55 } | |
56 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
57 |
3 | 58 ////////////////////////////////////////////////////// Evaluation ////////////////////////////////////////////////////// |
59 /// Evaluates the neural network. | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
60 public double[] evaluate(double[] inputs) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
61 return evaluateFull(inputs)[$ - 1]; // the last item (outputs) of the return value |
3 | 62 } |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
63 |
3 | 64 /// Evaluates the neural network and returns the output from all units. |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
65 public double[][] evaluateFull(double[] inputs) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
66 if(inputs.length != numInputs) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
67 throw new InputException("Wrong length of inputs."); |
4 | 68 double[][] outputs; |
3 | 69 outputs.length = units.length; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
70 outputs[0] = evaluateLayer(0, inputs); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
71 for(int i = 1; i < units.length; i++) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
72 outputs[i] = this.evaluateLayer(i, outputs[i - 1]); |
3 | 73 } |
74 return outputs; | |
75 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
76 |
3 | 77 // Helper function to evaluate the outputs of a single layer. |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
78 private double[] evaluateLayer(uint layer, double[] layerInputs) { |
4 | 79 double[] output; |
80 output.length = units[layer].length; | |
81 //printArray(layerInputs); | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
82 for(int i = 0; i < units[layer].length; i++) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
83 output[i] = evaluateUnit(layer, i, layerInputs); |
3 | 84 } |
85 return output; | |
86 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
87 |
3 | 88 // Helper function to evaluate the output of a single unit. |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
89 private double evaluateUnit(uint layer, uint unit, double[] layerInputs) { |
4 | 90 //writef("(%d,%d)=",layer,unit); |
91 //printArray(layerInputs); | |
92 double total = units[layer][unit][0]; //bias | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
93 for(int i = 1; i < layerInputs.length + 1; i++) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
94 total += layerInputs[i - 1] * units[layer][unit][i]; // wi * xi |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
95 //writef("@"); |
3 | 96 } |
4 | 97 //writefln(" ! %f",total); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
98 if(functions[layer] != null) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
99 return functions[layer](total); // apply the function (if there is one) |
4 | 100 writefln("no function"); |
101 return total; // just return the result instead | |
3 | 102 } |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
103 |
3 | 104 ////////////////////////////////////////////////////// Training ////////////////////////////////////////////////////// |
105 /// Trains the neural network. | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
106 /// TODO: Pull error calculation into a separate function. (maybe) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
107 public void train(double[][] trainingInputs, double[][] trainingOutputs, |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
108 bool stochastic = false) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
109 if(trainingInputs.length != trainingOutputs.length) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
110 throw new InputException( |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
111 "trainingInputs and trainingOutputs must be the same size"); |
4 | 112 double[][][] weightUpdate; |
113 double[][] outputsError; | |
114 double[][] outputs; | |
115 double total; //temp variable | |
3 | 116 |
117 // Initialize the weightUpdate and outputsError variables | |
118 weightUpdate.length = units.length; | |
119 outputsError.length = units.length; | |
4 | 120 //writefln("#%d,%d",weightUpdate.length,outputsError.length); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
121 for(int i = 0; i < units.length; i++) { |
3 | 122 weightUpdate[i].length = units[i].length; |
123 outputsError[i].length = units[i].length; | |
4 | 124 //writefln("##(%d)%d,%d",i,weightUpdate[i].length,outputsError[i].length); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
125 for(int j = 0; j < weightUpdate[i].length; j++) { |
3 | 126 weightUpdate[i][j].length = units[i][j].length; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
127 for(int k = 0; k < weightUpdate[i][j].length; k++) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
128 weightUpdate[i][j][k] = 0.0f; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
129 //writefln("###(%d)%d",j,weightUpdate[i][j].length); |
3 | 130 } |
131 } | |
132 | |
133 | |
134 // Loop through each of the training examples | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
135 for(int example = 0; example < trainingInputs.length; example++) { |
4 | 136 outputs = evaluateFull(trainingInputs[example]); |
3 | 137 |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
138 |
3 | 139 // Computing error of output layer |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
140 for(int i = 0; i < outputs[$ - 1].length; i++) { // units of last layer |
4 | 141 //writefln("{%d,%d,%d,%d}",example,i,outputs.length,outputsError[$-1].length); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
142 outputsError[$ - 1][i] = outputs[$ - 1][i] * (1 - outputs[$ - 1][i]) * (trainingOutputs[example][i] - outputs[$ - 1][i]); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
143 } |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
144 // o(1-o)(t-o) |
3 | 145 |
4 | 146 //printArray(outputsError[$-1]); |
147 //printArray(units[length-1]); | |
148 | |
149 //* | |
3 | 150 // Loop through each of the hidden layers (backwards - BACKpropagation!) |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
151 for(int layer = units.length - 2; layer >= 0; layer--) { // -2 to skip the output layer |
4 | 152 //writef("|"); |
3 | 153 // loop through the units in each hidden layer |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
154 for(int unit = 0; unit < units[layer].length; unit++) { |
4 | 155 //writef("*"); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
156 total = 0; |
3 | 157 // total up w * e for the units the output of this unit goes into |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
158 for(int k = 0; k < units[layer + 1].length; k++) { |
4 | 159 //writef("{weight=%f,error=%f}", units[layer+1][k][unit+1/* +1 for bias*/], outputsError[layer+1][k]); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
160 total += units[layer + 1][k][unit + 1/* +1 for bias*/] * outputsError[layer + 1][k]; |
3 | 161 } |
4 | 162 //writefln("=%f(total)",total); |
3 | 163 // multiply total by o(1-o), store in outputsError |
4 | 164 outputsError[layer][unit] = outputs[layer][unit] * (1 - outputs[layer][unit]) * total; |
3 | 165 } |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
166 } |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
167 //writefln(); |
4 | 168 |
169 //writef("outputError="); printArray(outputsError); | |
3 | 170 |
171 // special case for the units that receive the input values | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
172 for(int unit = 0; unit < units[0].length; unit++) { // unit |
4 | 173 //writefln(":%d,%d,%d,%d",j,weightUpdate.length,weightUpdate[0].length,weightUpdate[0][j].length); |
174 weightUpdate[0][unit][0] += outputsError[0][unit]; //bias | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
175 for(int input = 1; input < units[0][unit].length; input++) { // input |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
176 weightUpdate[0][unit][input] += outputsError[0][unit] * trainingInputs[example][input - 1]; // account for bias |
3 | 177 } |
178 } | |
179 | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
180 |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
181 // Update the weightUpdate array OR update the weights |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
182 for(int i = 1; i < units.length; i++) { // layer |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
183 for(int j = 0; j < units[i].length; j++) { // unit |
3 | 184 weightUpdate[i][j][0] += outputsError[i][j]; //bias |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
185 for(int k = 1; k < units[i][j].length; k++) { // input |
4 | 186 //writefln("[%d,%d,%d]=%f; %f; %f",i,j,k,weightUpdate[i][j][k],outputsError[i][j],outputs[i-1][k-1]); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
187 weightUpdate[i][j][k] += outputsError[i][j] * outputs[i - 1][k - 1]; // previous layer, account for bias |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
188 } |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
189 } |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
190 } |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
191 if(stochastic) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
192 // Apply the weightUpdate array to the weights |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
193 for(int i = 0; i < units.length; i++) { // layer |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
194 for(int j = 0; j < units[i].length; j++) { // unit |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
195 for(int k = 0; k < units[i][j].length; k++) { // input |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
196 units[i][j][k] += this.learningRate * weightUpdate[i][j][k] + (this.momentum * this.oldWeightUpdate[i][j][k]); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
197 this.oldWeightUpdate[i][j][k] = weightUpdate[i][j][k]; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
198 weightUpdate[i][j][k] = 0; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
199 } |
3 | 200 } |
201 } | |
202 } | |
203 } | |
204 | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
205 if(!stochastic) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
206 // Apply the weightUpdate array to the weights |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
207 for(int i = 0; i < units.length; i++) { // layer |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
208 for(int j = 0; j < units[i].length; j++) { // unit |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
209 for(int k = 0; k < units[i][j].length; k++) { // input |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
210 //writefln("[%d,%d,%d]=%f; %f",i,j,k,units[i][j][k],weightUpdate[i][j][k]); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
211 units[i][j][k] += this.learningRate * weightUpdate[i][j][k] + (this.momentum * this.oldWeightUpdate[i][j][k]); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
212 } |
3 | 213 } |
214 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
215 this.oldWeightUpdate = weightUpdate; |
3 | 216 } |
217 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
218 |
3 | 219 /// Calculate the output error |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
220 double calculateError(double[][] trainingInputs, double[][] trainingOutputs) { |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
221 if(trainingInputs.length != trainingOutputs.length) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
222 throw new InputException( |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
223 "trainingInputs and trainingOutputs must be the same size"); |
4 | 224 double[] outputs; |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
225 double total = 0, temp; |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
226 for(int i = 0; i < trainingInputs.length; i++) { |
4 | 227 outputs = evaluate(trainingInputs[i]); |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
228 if(outputs.length != trainingOutputs[i].length) |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
229 throw new InputException("Wrong output length"); |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
230 for(int j = 0; j < outputs.length; j++) { |
4 | 231 temp = trainingOutputs[i][j] - outputs[j]; |
232 //writefln("&%f,%f",temp*temp,total); | |
3 | 233 total += temp * temp; |
234 } | |
235 } | |
236 return 0.5 * total; | |
237 } | |
5
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
238 |
810d58835f86
Added momentum and stochastic training to backprop.
revcompgeek
parents:
4
diff
changeset
|
239 double[][][] getWeights() { |
4 | 240 return units.dup; |
241 } | |
242 } |