Skip to content

Commit

Permalink
Java implementation with test
Browse files Browse the repository at this point in the history
  • Loading branch information
dlidstrom committed Oct 14, 2024
1 parent 5bda2dd commit 45a6d83
Show file tree
Hide file tree
Showing 16 changed files with 752 additions and 251 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* text eol=lf
*.png binary
*.bz2 binary
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
20 changes: 10 additions & 10 deletions CSharp/CSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
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
26 changes: 13 additions & 13 deletions FSharp/FSharp.fsproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Neural.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Neural.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

</Project>
148 changes: 74 additions & 74 deletions FSharp/Program.fs
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
(*
Licensed under the MIT License given below.
Copyright 2023 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.
*)

open Neural

let randFloat =
let P = 2147483647u
let A = 16807u;
let mutable current = 1u
let inner() =
current <- current * A % P;
let result = float current / float P
result
inner
let xor a b = a ^^^ b
let orf (a: int) b = a ||| b
let andf (a: int) b = a &&& b
let xnor a b = 1 - xor a b
let nand a b = 1 - andf a b
let nor a b = 1 - orf a b

let trainingData = [|
for i = 0 to 1 do
for j = 0 to 1 do
[| float i; j |],
[| xor i j |> float; xnor i j; orf i j; andf i j; nor i j; nand i j |]
|]

let trainer = Trainer(2, 2, 6, randFloat)
let lr = 1.0
let ITERS = 4000
for e = 0 to ITERS - 1 do
let input, y = trainingData[e % trainingData.Length]
trainer.Train(input, y, lr)

let network = trainer.Network
printfn "Result after %d iterations" ITERS
printfn " XOR XNOR OR AND NOR NAND"
for i, _ in trainingData do
let pred = network.Predict(i)
printfn
"%.0f,%.0f = %.3f %.3f %.3f %.3f %.3f %.3f"
i[0]
i[1]
pred[0]
pred[1]
pred[2]
pred[3]
pred[4]
pred[5]

let networkVals = {|
WeightsHidden = network.WeightsHidden
BiasesHidden = network.BiasesHidden
WeightsOutput = network.WeightsOutput
BiasesOutput = network.BiasesOutput
|}
printfn $"network: %A{networkVals}"
(*
Licensed under the MIT License given below.
Copyright 2023 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.
*)

open Neural

let randFloat =
let P = 2147483647u
let A = 16807u;
let mutable current = 1u
let inner() =
current <- current * A % P;
let result = float current / float P
result
inner
let xor a b = a ^^^ b
let orf (a: int) b = a ||| b
let andf (a: int) b = a &&& b
let xnor a b = 1 - xor a b
let nand a b = 1 - andf a b
let nor a b = 1 - orf a b

let trainingData = [|
for i = 0 to 1 do
for j = 0 to 1 do
[| float i; j |],
[| xor i j |> float; xnor i j; orf i j; andf i j; nor i j; nand i j |]
|]

let trainer = Trainer(2, 2, 6, randFloat)
let lr = 1.0
let ITERS = 4000
for e = 0 to ITERS - 1 do
let input, y = trainingData[e % trainingData.Length]
trainer.Train(input, y, lr)

let network = trainer.Network
printfn "Result after %d iterations" ITERS
printfn " XOR XNOR OR AND NOR NAND"
for i, _ in trainingData do
let pred = network.Predict(i)
printfn
"%.0f,%.0f = %.3f %.3f %.3f %.3f %.3f %.3f"
i[0]
i[1]
pred[0]
pred[1]
pred[2]
pred[3]
pred[4]
pred[5]

let networkVals = {|
WeightsHidden = network.WeightsHidden
BiasesHidden = network.BiasesHidden
WeightsOutput = network.WeightsOutput
BiasesOutput = network.BiasesOutput
|}
printfn $"network: %A{networkVals}"
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;
}
}
Loading

0 comments on commit 45a6d83

Please sign in to comment.