Skip to content

Commit 572f879

Browse files
committed
Java implementation with test
1 parent 5bda2dd commit 572f879

16 files changed

+760
-259
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
* text eol=lf
2+
*.png binary
3+
*.bz2 binary

.github/workflows/ci.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ jobs:
9898
- name: Tests
9999
working-directory: Go
100100
run: bats test.bats
101+
102+
java:
103+
runs-on: ubuntu-latest
104+
steps:
105+
- uses: actions/checkout@v3
106+
- name: Setup
107+
run: |
108+
sudo npm install -g bats
109+
- name: Build
110+
working-directory: Java
111+
run: make jar
112+
- name: Tests
113+
working-directory: Java
114+
run: bats test.bats

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
semeion.data
2+
Java/Main.jar
3+
Java/out

CSharp/CSharp.csproj

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
</PropertyGroup>
9-
10-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

CSharp/Program.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323
using static Neural.Logical;
2424

2525
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
26-
if (args[0] == "--logical")
26+
if (args.FirstOrDefault() == "--logical")
2727
{
2828
var trainingData = Enumerable.Range(0, 2)
2929
.SelectMany(x => Enumerable.Range(0, 2), (l, r) => (l, r))
@@ -66,7 +66,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6666
};
6767
Console.WriteLine($"network: {networkVals.ToJson()}");
6868
}
69-
else if (args[0] == "--semeion")
69+
else if (args.FirstOrDefault() == "--semeion")
7070
{
7171
// --semeion <file> hiddens epochs lr
7272
const int inputCount = 16 * 16;
@@ -128,6 +128,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
128128
Console.WriteLine();
129129
}
130130
}
131+
else {
132+
Console.WriteLine("Specify --logical or --semeion <file>");
133+
}
131134

132135
namespace Neural
133136
{

FSharp/FSharp.fsproj

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
6-
</PropertyGroup>
7-
8-
<ItemGroup>
9-
<Compile Include="Neural.fs" />
10-
<Compile Include="Program.fs" />
11-
</ItemGroup>
12-
13-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Neural.fs" />
10+
<Compile Include="Program.fs" />
11+
</ItemGroup>
12+
13+
</Project>

FSharp/Program.fs

+74-74
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
1-
(*
2-
Licensed under the MIT License given below.
3-
Copyright 2023 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-
open Neural
21-
22-
let randFloat =
23-
let P = 2147483647u
24-
let A = 16807u;
25-
let mutable current = 1u
26-
let inner() =
27-
current <- current * A % P;
28-
let result = float current / float P
29-
result
30-
inner
31-
let xor a b = a ^^^ b
32-
let orf (a: int) b = a ||| b
33-
let andf (a: int) b = a &&& b
34-
let xnor a b = 1 - xor a b
35-
let nand a b = 1 - andf a b
36-
let nor a b = 1 - orf a b
37-
38-
let trainingData = [|
39-
for i = 0 to 1 do
40-
for j = 0 to 1 do
41-
[| float i; j |],
42-
[| xor i j |> float; xnor i j; orf i j; andf i j; nor i j; nand i j |]
43-
|]
44-
45-
let trainer = Trainer(2, 2, 6, randFloat)
46-
let lr = 1.0
47-
let ITERS = 4000
48-
for e = 0 to ITERS - 1 do
49-
let input, y = trainingData[e % trainingData.Length]
50-
trainer.Train(input, y, lr)
51-
52-
let network = trainer.Network
53-
printfn "Result after %d iterations" ITERS
54-
printfn " XOR XNOR OR AND NOR NAND"
55-
for i, _ in trainingData do
56-
let pred = network.Predict(i)
57-
printfn
58-
"%.0f,%.0f = %.3f %.3f %.3f %.3f %.3f %.3f"
59-
i[0]
60-
i[1]
61-
pred[0]
62-
pred[1]
63-
pred[2]
64-
pred[3]
65-
pred[4]
66-
pred[5]
67-
68-
let networkVals = {|
69-
WeightsHidden = network.WeightsHidden
70-
BiasesHidden = network.BiasesHidden
71-
WeightsOutput = network.WeightsOutput
72-
BiasesOutput = network.BiasesOutput
73-
|}
74-
printfn $"network: %A{networkVals}"
1+
(*
2+
Licensed under the MIT License given below.
3+
Copyright 2023 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+
open Neural
21+
22+
let randFloat =
23+
let P = 2147483647u
24+
let A = 16807u;
25+
let mutable current = 1u
26+
let inner() =
27+
current <- current * A % P;
28+
let result = float current / float P
29+
result
30+
inner
31+
let xor a b = a ^^^ b
32+
let orf (a: int) b = a ||| b
33+
let andf (a: int) b = a &&& b
34+
let xnor a b = 1 - xor a b
35+
let nand a b = 1 - andf a b
36+
let nor a b = 1 - orf a b
37+
38+
let trainingData = [|
39+
for i = 0 to 1 do
40+
for j = 0 to 1 do
41+
[| float i; j |],
42+
[| xor i j |> float; xnor i j; orf i j; andf i j; nor i j; nand i j |]
43+
|]
44+
45+
let trainer = Trainer(2, 2, 6, randFloat)
46+
let lr = 1.0
47+
let ITERS = 4000
48+
for e = 0 to ITERS - 1 do
49+
let input, y = trainingData[e % trainingData.Length]
50+
trainer.Train(input, y, lr)
51+
52+
let network = trainer.Network
53+
printfn "Result after %d iterations" ITERS
54+
printfn " XOR XNOR OR AND NOR NAND"
55+
for i, _ in trainingData do
56+
let pred = network.Predict(i)
57+
printfn
58+
"%.0f,%.0f = %.3f %.3f %.3f %.3f %.3f %.3f"
59+
i[0]
60+
i[1]
61+
pred[0]
62+
pred[1]
63+
pred[2]
64+
pred[3]
65+
pred[4]
66+
pred[5]
67+
68+
let networkVals = {|
69+
WeightsHidden = network.WeightsHidden
70+
BiasesHidden = network.BiasesHidden
71+
WeightsOutput = network.WeightsOutput
72+
BiasesOutput = network.BiasesOutput
73+
|}
74+
printfn $"network: %A{networkVals}"

Java/Makefile

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
##
2+
# source directory
3+
##
4+
SRC_DIR := src
5+
6+
##
7+
# output directory
8+
##
9+
OUT_DIR := out
10+
11+
##
12+
# sources
13+
##
14+
SRCS := $(wildcard $(SRC_DIR)/*.java)
15+
16+
##
17+
# classes
18+
##
19+
CLS := $(SRCS:$(SRC_DIR)/%.java=$(OUT_DIR)/%.class)
20+
21+
##
22+
# compiler and compiler flags
23+
##
24+
JC := javac
25+
JCFLAGS := -d $(OUT_DIR)/ -cp $(SRC_DIR)/
26+
J := java
27+
28+
##
29+
# suffixes
30+
##
31+
.SUFFIXES: .java
32+
33+
##
34+
# targets that do not produce output files
35+
##
36+
.PHONY: all clean
37+
38+
##
39+
# default target(s)
40+
##
41+
all: run
42+
43+
$(CLS): $(OUT_DIR)/%.class: $(SRC_DIR)/%.java
44+
$(JC) $(JCFLAGS) $<
45+
46+
##
47+
# jar
48+
##
49+
jar: $(CLS)
50+
jar cfe Main.jar Main -C out .
51+
52+
##
53+
# run
54+
##
55+
run: jar
56+
$(J) -jar Main.jar
57+
58+
##
59+
# clean up any output files
60+
##
61+
clean:
62+
rm -f $(OUT_DIR)/*.class
63+
rm -f Main.jar

Java/src/CustomRandom.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.function.Supplier;
21+
22+
public class CustomRandom implements Supplier<Double> {
23+
final int P = 2147483647;
24+
final int A = 16807;
25+
int current = 1;
26+
27+
public Double get() {
28+
current = Integer.remainderUnsigned(current * A, P);
29+
double result = (double)current / P;
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)