|
| 1 | +/* |
| 2 | +Licensed under the MIT License given below. |
| 3 | +Copyright 2024 Daniel Lidstrom |
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | +this software and associated documentation files (the “Software”), to deal in |
| 6 | +the Software without restriction, including without limitation the rights to |
| 7 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | +subject to the following conditions: |
| 10 | +The above copyright notice and this permission notice shall be included in all |
| 11 | +copies or substantial portions of the Software. |
| 12 | +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 14 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 15 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 16 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 17 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | +*/ |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Random; |
| 23 | +import java.util.function.Supplier; |
| 24 | + |
| 25 | +public class Main { |
| 26 | + public static void main(String[] args) { |
| 27 | + CustomRandom random = new CustomRandom(); |
| 28 | + Supplier<Double> rand = () -> random.get(); |
| 29 | + var trainingData = Arrays.asList( |
| 30 | + new DataItem(new double[]{0, 0}, new double[]{Logical.xor(0, 0), Logical.xnor(0, 0), Logical.or(0, 0), Logical.and(0, 0), Logical.nor(0, 0), Logical.nand(0, 0)}), |
| 31 | + new DataItem(new double[]{0, 1}, new double[]{Logical.xor(0, 1), Logical.xnor(0, 1), Logical.or(0, 1), Logical.and(0, 1), Logical.nor(0, 1), Logical.nand(0, 1)}), |
| 32 | + new DataItem(new double[]{1, 0}, new double[]{Logical.xor(1, 0), Logical.xnor(1, 0), Logical.or(1, 0), Logical.and(1, 0), Logical.nor(1, 0), Logical.nand(1, 0)}), |
| 33 | + new DataItem(new double[]{1, 1}, new double[]{Logical.xor(1, 1), Logical.xnor(1, 1), Logical.or(1, 1), Logical.and(1, 1), Logical.nor(1, 1), Logical.nand(1, 1)}) |
| 34 | + ).toArray(new DataItem[0]); |
| 35 | + |
| 36 | + Trainer trainer = Trainer.create(2, 2, 6, rand); |
| 37 | + double lr = 1.0; |
| 38 | + int ITERS = 4000; |
| 39 | + for (int e = 0; e < ITERS; e++) { |
| 40 | + var sample = trainingData[e % trainingData.length]; |
| 41 | + trainer.train(sample.input(), sample.output(), lr); |
| 42 | + } |
| 43 | + |
| 44 | + Network network = trainer.network(); |
| 45 | + System.out.println("Result after " + ITERS + " iterations"); |
| 46 | + System.out.println(" XOR XNOR OR AND NOR NAND"); |
| 47 | + for (var sample : trainingData) { |
| 48 | + double[] pred = network.predict(sample.input()); |
| 49 | + System.out.printf( |
| 50 | + Locale.ROOT, |
| 51 | + "%d,%d = %.3f %.3f %.3f %.3f %.3f %.3f%n", |
| 52 | + (int) sample.input()[0], (int) sample.input()[1], |
| 53 | + pred[0], pred[1], pred[2], pred[3], pred[4], pred[5]); |
| 54 | + } |
| 55 | + |
| 56 | + System.out.println("weights hidden:"); |
| 57 | + for (int i = 0; i < network.inputCount(); i++) { |
| 58 | + for (int j = 0; j < network.hiddenCount(); j++) { |
| 59 | + System.out.printf(Locale.ROOT, " %9.6f", network.weightsHidden()[network.inputCount() * i + j]); |
| 60 | + } |
| 61 | + |
| 62 | + System.out.printf("\n"); |
| 63 | + } |
| 64 | + |
| 65 | + System.out.printf("biases hidden:\n"); |
| 66 | + for (int i = 0; i < network.hiddenCount(); i++) { |
| 67 | + System.out.printf(Locale.ROOT, " %9.6f", network.biasesHidden()[i]); |
| 68 | + } |
| 69 | + |
| 70 | + System.out.printf("\n"); |
| 71 | + |
| 72 | + System.out.printf("weights output:\n"); |
| 73 | + for (int i = 0; i < network.hiddenCount(); i++) { |
| 74 | + for (int j = 0; j < network.outputCount(); j++) { |
| 75 | + System.out.printf(Locale.ROOT, " %9.6f", network.weightsOutput()[i * network.outputCount() + j]); |
| 76 | + } |
| 77 | + |
| 78 | + System.out.printf("\n"); |
| 79 | + } |
| 80 | + |
| 81 | + System.out.printf("biases output:\n"); |
| 82 | + for (int i = 0; i < network.outputCount(); i++) { |
| 83 | + System.out.printf(Locale.ROOT, " %9.6f", network.biasesOutput()[i]); |
| 84 | + } |
| 85 | + |
| 86 | + System.out.printf("\n"); |
| 87 | + } |
| 88 | + |
| 89 | + public static class DataItem { |
| 90 | + private final double[] input; |
| 91 | + private final double[] output; |
| 92 | + |
| 93 | + public DataItem(double[] input, double[] output) { |
| 94 | + this.input = input; |
| 95 | + this.output = output; |
| 96 | + } |
| 97 | + |
| 98 | + public double[] input() { |
| 99 | + return input; |
| 100 | + } |
| 101 | + |
| 102 | + public double[] output() { |
| 103 | + return output; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static class Logical { |
| 108 | + public static int xor(int a, int b) { |
| 109 | + return a ^ b; |
| 110 | + } |
| 111 | + |
| 112 | + public static int xnor(int a, int b) { |
| 113 | + return 1 - xor(a, b); |
| 114 | + } |
| 115 | + |
| 116 | + public static int or(int a, int b) { |
| 117 | + return a | b; |
| 118 | + } |
| 119 | + |
| 120 | + public static int and(int a, int b) { |
| 121 | + return a & b; |
| 122 | + } |
| 123 | + |
| 124 | + public static int nand(int a, int b) { |
| 125 | + return 1 - and(a, b); |
| 126 | + } |
| 127 | + |
| 128 | + public static int nor(int a, int b) { |
| 129 | + return 1 - or(a, b); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments