annotate trunk/backprop_test.d @ 3:314d68bafeff

Backprop and backprop_test added (no testing).
author revcompgeek
date Fri, 11 Apr 2008 18:12:55 -0600
parents
children 73beed484455
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 backprop_test;
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.multilayer.backprop;
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
4 import aid.nn.outputFunctions;
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
5 import std.stdio;
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
6
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
7 /+float[][] trainingInputs = [
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
8 [0,0,0],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
9 [0,0,1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
10 [0,1,0],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
11 [0,1,1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
12 [1,0,0],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
13 [1,0,1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
14 [1,1,0],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
15 [1,1,1]];
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
16
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
17 float[][] trainingOutputs = [
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
18 [0.1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
19 [0.9],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
20 [0.9],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
21 [0.1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
22 [0.9],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
23 [0.1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
24 [0.1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
25 [0.9]];+/
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 float[][] trainingInputs = [
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
28 [0,0],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
29 [0,1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
30 [1,0],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
31 [1,1]];
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
32
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
33 float[][] trainingOutputs = [
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
34 [0.1],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
35 [0.9],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
36 [0.9],
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
37 [0.1]];
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 void main(){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
40 Backprop nn = new Backprop(2,[4,1],[&sigmoid,&sigmoid]);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
41
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
42 float error = 10.0;
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
43 float[] output;
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
44 int iter = 0;
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
45 while(error >= 0.5){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
46 error = nn.calculateError(trainingInputs,trainingOutputs);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
47 if(iter % 100 == 0){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
48 writefln("Iter: %d",iter);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
49 for(int i=0; i<trainingInputs.length; i++){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
50 output = nn.evaluate(trainingInputs[i]);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
51 writef(" %d:", i); printArray(output);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
52 }
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
53 writefln(" Error: %f", error);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
54 }
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
55 nn.train(trainingInputs,trainingOutputs);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
56 }
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
57 writefln("Total Iters: %d",iter);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
58 for(int i=0; i<trainingInputs.length; i++){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
59 output = nn.evaluate(trainingInputs[i]);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
60 writef(" %d:", i); printArray(output);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
61 }
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
62 writefln(" Error: %f", error);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
63 }
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
64
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
65 void printArray(float[] array){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
66 writef("[");
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
67 for(int i=0; i<array.length-1; i++){
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
68 writef("%f, ",array[i]);
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 writefln("%f]",array[$]);
314d68bafeff Backprop and backprop_test added (no testing).
revcompgeek
parents:
diff changeset
71 }