forked from bobappleyard/NeuralNets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
100 lines (81 loc) · 3.21 KB
/
Copy pathTest.java
File metadata and controls
100 lines (81 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import appleyard.bob.maths.*;
import appleyard.bob.nets.Layer;
import appleyard.bob.nets.NeuralNet;
import appleyard.bob.nets.NeuralNetException;
import appleyard.bob.nets.TrainingData;
import appleyard.bob.nets.functions.Bipolar;
import appleyard.bob.nets.functions.HardLim;
import appleyard.bob.nets.functions.Unipolar;
public class Test {
private static void output(Object obj) {
System.out.println(obj);
}
private static void runTest(NeuralNet n, double vals[]) throws NeuralNetException {
output(n.forwards(new Vector(vals)));
}
private static void testError(NeuralNet n, double[] vals, double[] des) throws NeuralNetException {
Vector desired = new Vector(des);
Vector out = n.forwards(new Vector(vals));
double err = n.backwards(out, desired);
output(String.format("desired: %s output: %s\n error: %s", new Object[] {desired, out, err}));
}
private static double trainUp(NeuralNet n, TrainingData[] data, int epochs) throws NeuralNetException {
double errVal = n.train(data, epochs);
output("error: " + errVal);
return errVal;
}
public static void main(String args[]) {
NeuralNet n;
Layer l;
double errMax = 0.001;
try {
output("Hardwired XOR");
n = new NeuralNet();
n.setInputDim(2);
n.setBias(-0.1);
n.setFcn(HardLim.INSTANCE);
n.addHidden(new Layer(new Matrix(3, 2, new double[] {1, -1, 1,
-1, 1, 1})));
n.setOutput(new Layer(new Matrix(3, 1, new double[] {1, 1, 1})));
runTest(n, new double[] {0, 0});
runTest(n, new double[] {0, 1});
runTest(n, new double[] {1, 0});
runTest(n, new double[] {1, 1});
output("Learning XOR");
n = new NeuralNet(new int[] {2, 2, 1});
n.setBias(-0.1);
n.setFcn(Unipolar.INSTANCE);
n.setErrMax(errMax);
/*testError(n, new double[] {0, 0}, new double[] {0});
testError(n, new double[] {1, 0}, new double[] {1});
testError(n, new double[] {0, 1}, new double[] {1});
testError(n, new double[] {1, 1}, new double[] {0});*/
// bipolar training data
/* TrainingData[] data = {
new TrainingData(new Vector(new double[] {-1, -1}), new Vector (new double[] {-1})),
new TrainingData(new Vector(new double[] {-1, 1}), new Vector (new double[] {1})),
new TrainingData(new Vector(new double[] {1, -1}), new Vector (new double[] {1})),
new TrainingData(new Vector(new double[] {1, 1}), new Vector (new double[] {-1})),
};*/
TrainingData[] data = {
new TrainingData(new Vector(new double[] {0, 0}), new Vector (new double[] {0})),
new TrainingData(new Vector(new double[] {0, 1}), new Vector (new double[] {1})),
new TrainingData(new Vector(new double[] {1, 0}), new Vector (new double[] {1})),
new TrainingData(new Vector(new double[] {1, 1}), new Vector (new double[] {0}))
};
n.train(data, 10000, 500);
// prior to the method above, below was used
/*double errVal = 1;
for (int i = 0; (errVal > errMax) && (i < 500); ++i) {
errVal = trainUp(n, data, 10000);
}*/
output("A run to see what it's doing: ");
runTest(n, new double[] {0, 0});
runTest(n, new double[] {0, 1});
runTest(n, new double[] {1, 0});
runTest(n, new double[] {1, 1});
} catch (Exception e) {
e.printStackTrace();
}
}
}