Mercurial > projects > aid
annotate trunk/perceptron_test.d @ 7:b9fe92a2d8ad default tip
Removed old code.
author | revcompgeek |
---|---|
date | Tue, 06 May 2008 22:20:26 -0600 |
parents | 9655c8362b25 |
children |
rev | line source |
---|---|
2
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
1 module perceptron_test; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
2 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
3 import aid.nn.perceptron; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
4 import std.stdio; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
5 import std.string; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
6 import std.conv; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
7 import std.math; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
8 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
9 void main(char[][] args){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
10 perceptron nn; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
11 double[][] inputs = [[ 0, 0], |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
12 [ 0, 1], |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
13 [ 1, 0], |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
14 [ 1, 1]]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
15 double[] outputs = [ 0, 0, 0, 1]; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
16 double learningRate = 0.3; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
17 int numReps = 20; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
18 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
19 if(args.length > 1) learningRate = toDouble(args[1]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
20 if(args.length > 2) numReps = toInt(args[2]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
21 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
22 nn = new perceptron(2,learningRate,true,null); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
23 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
24 int iter = 0; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
25 double[] weights; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
26 double t,difference; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
27 while(iter <= numReps){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
28 weights=nn.getWeights(); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
29 writefln("[ %f, %f, %f ]",weights[0],weights[1],weights[2]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
30 //Evaluate |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
31 for(int i = 0; i < 4; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
32 t = nn.evaluate(inputs[i]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
33 writefln(" D: %f %f",t,outputs[i]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
34 difference = abs(t-outputs[i]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
35 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
36 writefln("%d: Total Difference: %f",iter,difference); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
37 // End evaluate |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
38 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
39 // Train |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
40 for(int i = 0; i < 4; i++){ |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
41 nn.train(inputs[i],outputs[i]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
42 } |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
43 writefln("%d: Error Value: %f",iter,nn.getErrorValue(inputs,outputs)); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
44 writefln(); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
45 |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
46 iter++; |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
47 //if(iter > 20) break; |
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 weights=nn.getWeights(); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
50 writefln("[ %f, %f, %f ]",weights[0],weights[1],weights[2]); |
9655c8362b25
Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents:
diff
changeset
|
51 } |