Skip to content

Commit

Permalink
fix line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
dlidstrom committed Oct 14, 2024
1 parent 5bda2dd commit 6580e9e
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 239 deletions.
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>
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}"
Loading

0 comments on commit 6580e9e

Please sign in to comment.