1 import java.util.Random;
2 public class Client {
3 static Random rand;
4 public static void main(String [] args) {
5 rand = new Random();
6 NeuralNetwork nn = new NeuralNetwork(2,8,2);
7 nn.train(pattern(5000));
8 double incorrect = nn.test(pattern(100));
9 System.out.println("% Error: " + incorrect * 100 + "%");
10 }
11
14 public static double[][][] pattern(int n) {
15 double [][][] pattern = new double[n * 2][2][2];
16 int index;
17 double [][] temp;
18 System.arraycopy(apoints(n), 0, pattern, 0, n);
19 System.arraycopy(bpoints(n), 0, pattern, n, n);
20
21 for(int i = 0; i < n * 2; i++) {
22 index = (int)(Math.random() * n * 2);
23 temp = pattern[i];
24 pattern[i] = pattern[index];
25 pattern[index] = temp;
26 }
27 return pattern;
28 }
29
30 public static double[][][] apoints(int n) {
31 double [][][] pattern = new double[n][2][2];
32 double x, y;
33 for(int i = 0; i < n; i++) {
34 x = nextDouble();
35 y = Math.sqrt(1 - Math.pow(x, 2));
36
37 pattern[i][0][0] = x;
38 pattern[i][0][1] = (rand.nextDouble() <= .5) ? y : -1 * y;
39 pattern[i][1][0] = 1;
40 pattern[i][1][1] = 0;
41 }
42 return pattern;
43 }
44
45 public static double[][][] bpoints(int n) {
46 double [][][] pattern = new double[n][2][2];
47 double x, y;
48 for(int i = 0; i < n; i++) {
49 x = nextDouble() * 2;
50 y = nextDouble() * 2;
51
52 while(1 >= Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))) y = nextDouble() * 2;
53 pattern[i][0][0] = x;
54 pattern[i][0][1] = y;
55 pattern[i][1][0] = 0;
56 pattern[i][1][1] = 1;
57 }
58 return pattern;
59 }
60
61 public static double nextDouble() {
62 double d = rand.nextDouble();
63 return (rand.nextDouble() <= .5) ? d : -1 * d;
64 }
65 }
66