Skip to content

Commit

Permalink
Merge branch 'java'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlidstrom committed Oct 14, 2024
2 parents 1712466 + 53f46ad commit 622e2a3
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 12 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,17 @@ jobs:
- name: Tests
working-directory: Go
run: bats test.bats

java:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup
run: |
sudo npm install -g bats
- name: Build
working-directory: Java
run: make jar
- name: Tests
working-directory: Java
run: bats test.bats
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
semeion.data
Java/Main.jar
Java/out
7 changes: 5 additions & 2 deletions CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using static Neural.Logical;

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
if (args[0] == "--logical")
if (args.FirstOrDefault() == "--logical")
{
var trainingData = Enumerable.Range(0, 2)
.SelectMany(x => Enumerable.Range(0, 2), (l, r) => (l, r))
Expand Down Expand Up @@ -66,7 +66,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
};
Console.WriteLine($"network: {networkVals.ToJson()}");
}
else if (args[0] == "--semeion")
else if (args.FirstOrDefault() == "--semeion")
{
// --semeion <file> hiddens epochs lr
const int inputCount = 16 * 16;
Expand Down Expand Up @@ -128,6 +128,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Console.WriteLine();
}
}
else {
Console.WriteLine("Specify --logical or --semeion <file>");
}

namespace Neural
{
Expand Down
63 changes: 63 additions & 0 deletions Java/Makefile
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
32 changes: 32 additions & 0 deletions Java/src/CustomRandom.java
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;
}
}
132 changes: 132 additions & 0 deletions Java/src/Main.java
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);
}
}
}
111 changes: 111 additions & 0 deletions Java/src/Network.java
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);
}
}
}
Loading

0 comments on commit 622e2a3

Please sign in to comment.