diff options
author | sanine <sanine.not@pm.me> | 2023-11-21 14:16:38 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-21 14:16:38 -0600 |
commit | fae22e68282336fc0d7f0efda236410a294b7eb5 (patch) | |
tree | c42926a3edf1f4c4e98f9fb2c76af5ade24d7f6c | |
parent | 23e8acfc1eaa1702e55f47239e4b4dfbe9d35fca (diff) |
add failing compute tests
-rw-r--r-- | src/Mind.hs | 6 | ||||
-rw-r--r-- | test/MindTest.hs | 74 |
2 files changed, 80 insertions, 0 deletions
diff --git a/src/Mind.hs b/src/Mind.hs index 1ce8ab3..2971476 100644 --- a/src/Mind.hs +++ b/src/Mind.hs @@ -7,6 +7,7 @@ module Mind , Network (..) , createEmptyNetwork , connectNeurons + , compute ) where import Data.Ix @@ -71,3 +72,8 @@ insertEdge ns i e | (inRange (0, length ns) i) = let (front, es:back) = splitAt i ns in Just $ front ++ [e:es] ++ back | otherwise = Nothing + + +-- network computation +compute :: Network -> [Float] -> [Float] -> Maybe ([Float], [Float]) +compute net input state = undefined diff --git a/test/MindTest.hs b/test/MindTest.hs index 65d5a84..cf5c50a 100644 --- a/test/MindTest.hs +++ b/test/MindTest.hs @@ -21,6 +21,8 @@ networkTests :: TestTree networkTests = testGroup "network tests" $ [ testCase "create empty network" $ (createEmptyNetwork 3 2 1) @?= Network 3 [[], []] [[]] + + -- neuron connections , testCase "output network connection" $ let network = Network 3 [[], []] [[]] in (connectNeurons network (Input 0) (Output 0) (negate 1.0)) @@ -57,4 +59,76 @@ networkTests = testGroup "network tests" $ let network = Network 3 [[], []] [[]] in (connectNeurons network (Input 0) (Output 4) (negate 1.0)) @?= Nothing + + -- network computations + , testCase "single input, single output" $ + let + net = Network 1 [] [[Edge (Input 0, 2.0)]] + Just (output, state) = compute net [negate 0.5] [] + in (output, state) @?= + ( [tanh (2.0 * (negate 0.5))] + , [] + ) + , testCase "multiple inputs, single output" $ + let + net = Network 4 [] [ + [ Edge (Input 0, negate 1.0) + , Edge (Input 1, negate 2.0) + , Edge (Input 2, 1.0) + , Edge (Input 3, 2.0) + ]] + Just (output, state) = compute net [1, 2, 3, 5] [] + in (output, state) @?= + ( + [tanh + ( (1 * (negate 1.0)) + + (2 * (negate 2.0)) + + (3 * 1.0) + + (5 * 2.0) + ) + ] + , [] + ) + , testCase "multiple inputs, multiple outputs" $ + let + net = Network 4 [] + [ [ Edge (Input 0, 1.0) + , Edge (Input 1, negate 2.0) + ] + , [ Edge (Input 2, negate 1.0) + , Edge (Input 3, 1.0) + ] + ] + Just (output, state) = compute net [1, 2, 3, 5] [] + in (output, state) @?= + ( + [ tanh (2 - 1) + , tanh (5 - 3) + ] + , [] + ) + , testCase "hidden neurons" $ + let + net = Network 4 + -- hidden neurons + [ [ Edge (Input 0, negate 1.0) + , Edge (Input 1, 1.0) + ] + , [ Edge (Input 2, negate 1.0) + , Edge (Input 3, 1.0) + ] + ] + -- output neurons + [ [ Edge (Internal 0, negate 1.0) + , Edge (Internal 1, 1.0) + ] + ] + Just (output, state) = compute net [1, 2, 3, 5] [0, 0] + in (output, state) @?= + ( + [ tanh ( (tanh (5-3)) - (tanh (2-1)) ) ] + , [ tanh (2-1) + , tanh (5-3) + ] + ) ] |