annotate trunk/aid/nn/multilayer/backprop.d @ 4:73beed484455

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