-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
511 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
semeion.data | ||
Java/Main.jar | ||
Java/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
## | ||
# source directory | ||
## | ||
SRC_DIR := src | ||
|
||
## | ||
# output directory | ||
## | ||
OUT_DIR := out | ||
|
||
## | ||
# sources | ||
## | ||
SRCS := $(wildcard $(SRC_DIR)/*.java) | ||
|
||
## | ||
# classes | ||
## | ||
CLS := $(SRCS:$(SRC_DIR)/%.java=$(OUT_DIR)/%.class) | ||
|
||
## | ||
# compiler and compiler flags | ||
## | ||
JC := javac | ||
JCFLAGS := -d $(OUT_DIR)/ -cp $(SRC_DIR)/ | ||
J := java | ||
|
||
## | ||
# suffixes | ||
## | ||
.SUFFIXES: .java | ||
|
||
## | ||
# targets that do not produce output files | ||
## | ||
.PHONY: all clean | ||
|
||
## | ||
# default target(s) | ||
## | ||
all: run | ||
|
||
$(CLS): $(OUT_DIR)/%.class: $(SRC_DIR)/%.java | ||
$(JC) $(JCFLAGS) $< | ||
|
||
## | ||
# jar | ||
## | ||
jar: $(CLS) | ||
jar cfe Main.jar Main -C out . | ||
|
||
## | ||
# run | ||
## | ||
run: jar | ||
$(J) -jar Main.jar | ||
|
||
## | ||
# clean up any output files | ||
## | ||
clean: | ||
rm -f $(OUT_DIR)/*.class | ||
rm -f Main.jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Licensed under the MIT License given below. | ||
Copyright 2024 Daniel Lidstrom | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the “Software”), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class CustomRandom implements Supplier<Double> { | ||
final int P = 2147483647; | ||
final int A = 16807; | ||
int current = 1; | ||
|
||
public Double get() { | ||
current = Integer.remainderUnsigned(current * A, P); | ||
double result = (double)current / P; | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Licensed under the MIT License given below. | ||
Copyright 2024 Daniel Lidstrom | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the “Software”), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.Random; | ||
import java.util.function.Supplier; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
CustomRandom random = new CustomRandom(); | ||
Supplier<Double> rand = () -> random.get(); | ||
var trainingData = Arrays.asList( | ||
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)}), | ||
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)}), | ||
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)}), | ||
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)}) | ||
).toArray(new DataItem[0]); | ||
|
||
Trainer trainer = Trainer.create(2, 2, 6, rand); | ||
double lr = 1.0; | ||
int ITERS = 4000; | ||
for (int e = 0; e < ITERS; e++) { | ||
var sample = trainingData[e % trainingData.length]; | ||
trainer.train(sample.input(), sample.output(), lr); | ||
} | ||
|
||
Network network = trainer.network(); | ||
System.out.println("Result after " + ITERS + " iterations"); | ||
System.out.println(" XOR XNOR OR AND NOR NAND"); | ||
for (var sample : trainingData) { | ||
double[] pred = network.predict(sample.input()); | ||
System.out.printf( | ||
Locale.ROOT, | ||
"%d,%d = %.3f %.3f %.3f %.3f %.3f %.3f%n", | ||
(int) sample.input()[0], (int) sample.input()[1], | ||
pred[0], pred[1], pred[2], pred[3], pred[4], pred[5]); | ||
} | ||
|
||
System.out.println("weights hidden:"); | ||
for (int i = 0; i < network.inputCount(); i++) { | ||
for (int j = 0; j < network.hiddenCount(); j++) { | ||
System.out.printf(Locale.ROOT, " %9.6f", network.weightsHidden()[network.inputCount() * i + j]); | ||
} | ||
|
||
System.out.printf("\n"); | ||
} | ||
|
||
System.out.printf("biases hidden:\n"); | ||
for (int i = 0; i < network.hiddenCount(); i++) { | ||
System.out.printf(Locale.ROOT, " %9.6f", network.biasesHidden()[i]); | ||
} | ||
|
||
System.out.printf("\n"); | ||
|
||
System.out.printf("weights output:\n"); | ||
for (int i = 0; i < network.hiddenCount(); i++) { | ||
for (int j = 0; j < network.outputCount(); j++) { | ||
System.out.printf(Locale.ROOT, " %9.6f", network.weightsOutput()[i * network.outputCount() + j]); | ||
} | ||
|
||
System.out.printf("\n"); | ||
} | ||
|
||
System.out.printf("biases output:\n"); | ||
for (int i = 0; i < network.outputCount(); i++) { | ||
System.out.printf(Locale.ROOT, " %9.6f", network.biasesOutput()[i]); | ||
} | ||
|
||
System.out.printf("\n"); | ||
} | ||
|
||
public static class DataItem { | ||
private final double[] input; | ||
private final double[] output; | ||
|
||
public DataItem(double[] input, double[] output) { | ||
this.input = input; | ||
this.output = output; | ||
} | ||
|
||
public double[] input() { | ||
return input; | ||
} | ||
|
||
public double[] output() { | ||
return output; | ||
} | ||
} | ||
|
||
public static class Logical { | ||
public static int xor(int a, int b) { | ||
return a ^ b; | ||
} | ||
|
||
public static int xnor(int a, int b) { | ||
return 1 - xor(a, b); | ||
} | ||
|
||
public static int or(int a, int b) { | ||
return a | b; | ||
} | ||
|
||
public static int and(int a, int b) { | ||
return a & b; | ||
} | ||
|
||
public static int nand(int a, int b) { | ||
return 1 - and(a, b); | ||
} | ||
|
||
public static int nor(int a, int b) { | ||
return 1 - or(a, b); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Licensed under the MIT License given below. | ||
Copyright 2024 Daniel Lidstrom | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the “Software”), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
public class Network { | ||
int inputCount; | ||
int hiddenCount; | ||
int outputCount; | ||
double[] weightsHidden; | ||
double[] biasesHidden; | ||
double[] weightsOutput; | ||
double[] biasesOutput; | ||
|
||
public Network( | ||
int inputCount, | ||
int hiddenCount, | ||
int outputCount, | ||
double[] weightsHidden, | ||
double[] biasesHidden, | ||
double[] weightsOutput, | ||
double[] biasesOutput) { | ||
this.inputCount = inputCount; | ||
this.hiddenCount = hiddenCount; | ||
this.outputCount = outputCount; | ||
this.weightsHidden = weightsHidden; | ||
this.biasesHidden = biasesHidden; | ||
this.weightsOutput = weightsOutput; | ||
this.biasesOutput = biasesOutput; | ||
} | ||
|
||
public int inputCount() { | ||
return inputCount; | ||
} | ||
|
||
public int hiddenCount() { | ||
return hiddenCount; | ||
} | ||
|
||
public int outputCount() { | ||
return outputCount; | ||
} | ||
|
||
public double[] weightsHidden() { | ||
return weightsHidden; | ||
} | ||
|
||
public double[] biasesHidden() { | ||
return biasesHidden; | ||
} | ||
|
||
public double[] weightsOutput() { | ||
return weightsOutput; | ||
} | ||
|
||
public double[] biasesOutput() { | ||
return biasesOutput; | ||
} | ||
|
||
public double[] predict(double[] input) { | ||
double[] yHidden = new double[hiddenCount]; | ||
double[] yOutput = new double[outputCount]; | ||
return predict(input, yHidden, yOutput); | ||
} | ||
|
||
public double[] predict(double[] input, double[] yHidden, double[] yOutput) { | ||
for (int c = 0; c < hiddenCount; c++) { | ||
double sum = 0.0; | ||
for (int r = 0; r < inputCount; r++) { | ||
sum += input[r] * weightsHidden[r * hiddenCount + c]; | ||
} | ||
|
||
yHidden[c] = ActivationFunctions.sigmoid(sum + biasesHidden[c]); | ||
} | ||
|
||
for (int c = 0; c < outputCount; c++) { | ||
double sum = 0.0; | ||
for (int r = 0; r < hiddenCount; r++) { | ||
sum += yHidden[r] * weightsOutput[r * outputCount + c]; | ||
} | ||
|
||
yOutput[c] = ActivationFunctions.sigmoid(sum + biasesOutput[c]); | ||
} | ||
|
||
return yOutput; | ||
} | ||
|
||
public static class ActivationFunctions { | ||
public static double sigmoid(double f) { | ||
return 1.0 / (1.0 + Math.exp(-f)); | ||
} | ||
|
||
public static double sigmoidPrim(double f) { | ||
return f * (1.0 - f); | ||
} | ||
} | ||
} |
Oops, something went wrong.