|
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}" |
0 commit comments