1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
module MindTest (suite) where
import Test.Tasty
import Test.Tasty.HUnit
import Mind
import Data.Either
suite :: TestTree
suite = testGroup "mind tests" $
[ neuronIndexTests
, networkTests
]
neuronIndexTests :: TestTree
neuronIndexTests = testGroup "neuron index tests" $
[ testCase "get input index" $ getNeuronIndex (Input 4) @?= 4
, testCase "get internal index" $ getNeuronIndex (Internal 12) @?= 12
, testCase "get output index" $ getNeuronIndex (Output 0) @?= 0
]
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))
@?= (Just $ Network 3 [[], []] [[Edge (Input 0, (negate 1.0))]])
, testCase "internal network connection" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Internal 0) (Internal 1) (negate 1.0))
@?= (Just $ Network 3 [[], [Edge (Internal 0, negate 1.0)]] [[]])
, testCase "internal self-connection" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Internal 0) (Internal 0) (negate 1.0))
@?= (Just $ Network 3 [[Edge (Internal 0, negate 1.0)], []] [[]])
, testCase "internal source out of range" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Internal 5) (Internal 0) (negate 1.0))
@?= Nothing
, testCase "internal sink out of range" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Internal 1) (Internal (negate 1)) (negate 1.0))
@?= Nothing
, testCase "input source out of range" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Input (negate 1)) (Internal 0) (negate 1.0))
@?= Nothing
, testCase "input sink" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Input 0) (Input 0) (negate 1.0))
@?= Nothing
, testCase "output source" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Output 0) (Output 0) (negate 1.0))
@?= Nothing
, testCase "output sink out of range" $
let network = Network 3 [[], []] [[]]
in (connectNeurons network (Input 0) (Output 1) (negate 1.0))
@?= Nothing
-- network computations
, testCase "single input, single output" $
let
net = Network 1 [] [[Edge (Input 0, 2.0)]]
Right (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)
]]
Right (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, negate 1.0)
, Edge (Input 1, 1.0)
]
, [ Edge (Input 2, negate 1.0)
, Edge (Input 3, 1.0)
]
]
Right (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)
]
]
Right (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)
]
)
, testCase "computing with self-connection" $
let
net = Network 0
-- hidden neurons
[ [ Edge (Internal 0, negate 0.5) ] ]
-- output neurons
[ [ Edge (Internal 0, 2) ] ]
Right result = compute net [] [1.0]
in result @?=
( [ tanh $ 2 * (tanh (negate 0.5)) ]
, [ tanh (negate 0.5) ]
)
, testCase "computation fails for bad input length" $
let
net = Network 2 [[]] [[]]
result = compute net (replicate 3 1.0) [1]
in (isLeft result) @?= True
, testCase "computation fails for bad state length" $
let
net = Network 2 [[]] [[]]
result = compute net (replicate 2 1.0) [1, 1]
in (isLeft result) @?= True
]
|