@@ -5,7 +5,6 @@
55module Test.Grenade.Layers.Convolution where
66
77import Grenade.Core.Shape
8−import Grenade.Core.Vector as Grenade
98import Grenade.Core.Network
109import Grenade.Layers.Convolution
1110import Grenade.Layers.Internal.Convolution
@@ -27,7 +26,7 @@ prop_im2col_no_stride = once $
2726 , 5.0, 6.0, 9.0, 10.0
2827 , 6.0, 7.0, 10.0, 11.0
2928 , 7.0, 8.0, 11.0, 12.0 ]
30− out = im2colUnsafe 2 2 1 1 input
29+ out = im2col 2 2 1 1 input
3130 in expected === out
3231
3332prop_im2col_stride = once $
@@ -40,7 +39,7 @@ prop_im2col_stride = once $
4039 , 3.0, 4.0, 7.0, 8.0
4140 , 5.0, 6.0, 9.0, 10.0
4241 , 7.0, 8.0, 11.0, 12.0 ]
43− out = im2colUnsafe 2 2 1 2 input
42+ out = im2col 2 2 1 2 input
4443 in expected === out
4544
4645prop_im2col_other = once $
@@ -51,7 +50,7 @@ prop_im2col_other = once $
5150 expected = (2><6)
5251 [ 1.0, 2.0, 5.0, 6.0 , 9.0, 10.0
5352 , 3.0, 4.0, 7.0, 8.0 , 11.0 ,12.0 ]
54− out = im2colUnsafe 3 2 1 2 input
53+ out = im2col 3 2 1 2 input
5554 in expected === out
5655
5756-- If there's no overlap (stride is the same size as the kernel)
@@ -61,20 +60,9 @@ prop_im2col_sym_on_same_stride = once $
6160 [ 1.0, 2.0, 3.0, 4.0
6261 , 5.0, 6.0, 7.0, 8.0
6362 , 9.0, 10.0, 11.0, 12.0 ]
64− out = col2imUnsafe 3 2 3 2 3 4 . im2colUnsafe 3 2 3 2 $ input
63+ out = col2im 3 2 3 2 3 4 . im2col 3 2 3 2 $ input
6564 in input === out
6665
67−-- If there's no overlap (stride is the same size as the kernel)
68−-- then col2im . im2col should be symmetric.
69−prop_im2colunsafe_sym_on_same_stride = once $
70− let input = (3><4)
71− [ 1.0, 2.0, 3.0, 4.0
72− , 5.0, 6.0, 7.0, 8.0
73− , 9.0, 10.0, 11.0, 12.0 ]
74− out = col2imUnsafe 3 2 3 2 3 4 . im2colUnsafe 3 2 3 2 $ input
75− in input === out
76−
77−
7866-- If there is an overlap, then the gradient passed back should be
7967-- the sum of the gradients across the filters.
8068prop_im2col_col2im_additive = once $
@@ -86,7 +74,7 @@ prop_im2col_col2im_additive = once $
8674 [ 1.0, 2.0, 2.0, 1.0
8775 , 2.0, 4.0, 4.0, 2.0
8876 , 1.0, 2.0, 2.0, 1.0 ]
89− out = col2imUnsafe 2 2 1 1 3 4 . im2colUnsafe 2 2 1 1 $ input
77+ out = col2im 2 2 1 1 3 4 . im2col 2 2 1 1 $ input
9078 in expected === out
9179
9280prop_simple_conv_forwards = once $
@@ -116,25 +104,18 @@ prop_simple_conv_forwards = once $
116104 [ 1.0, 2.0, 5.0
117105 , 3.0, 4.0, 6.0] :: HStatic.L 2 3)
118106
119− expect = [ HStatic.matrix
120− [ -3.0 , -4.0 ] :: HStatic.L 1 2
121− , HStatic.matrix
122− [ -1.0 , 1.0 ] :: HStatic.L 1 2
123− , HStatic.matrix
124− [ 5.0 , 9.0 ] :: HStatic.L 1 2
125− , HStatic.matrix
126− [ -7.0 , -10.0 ] :: HStatic.L 1 2] :: [HStatic.L 1 2]
107+ expect = HStatic.matrix
108+ [ -3.0 , -4.0
109+ , -1.0 , 1.0
110+ , 5.0 , 9.0
111+ , -7.0 , -10.0 ] :: HStatic.L 4 2
127112 out = runForwards convLayer input :: S' ('D3 1 2 4)
128113
129− grad = S3D' ( mkVector
130− [ HStatic.matrix
131− [ 1 , 0 ] :: HStatic.L 1 2
132− , HStatic.matrix
133− [ 0 , 0 ] :: HStatic.L 1 2
134− , HStatic.matrix
135− [ 0 , 0 ] :: HStatic.L 1 2
136− , HStatic.matrix
137− [ 0 , 1 ] :: HStatic.L 1 2] ) :: S' ('D3 1 2 4)
114+ grad = S3D' ( HStatic.matrix
115+ [ 1 , 0
116+ , 0 , 0
117+ , 0 , 0
118+ , 0 , 1 ] :: HStatic.L 4 2 ) :: S' ('D3 1 2 4)
138119
139120 expectBack = (HStatic.matrix
140121 [ 1.0, 0.0, 0.0
@@ -143,19 +124,19 @@ prop_simple_conv_forwards = once $
143124
144125 in case (out, inX, nc) of
145126 (S3D' out' , S2D' inX', Convolution' backGrad)
146− -> ((HStatic.extract <$> expect) === (HStatic.extract <$> vecToList out'))
127+ -> (HStatic.extract expect === HStatic.extract out')
147128 .&&. (HStatic.extract expectBack === HStatic.extract inX')
148129 .&&. (HStatic.extract expectedGradient === HStatic.extract backGrad)
149130
150131prop_vid2col_no_stride = once $
151− let input = [(3><4)
132+ let input = (6><4)
152133 [ 1.0, 2.0, 3.0, 4.0
153134 , 5.0, 6.0, 7.0, 8.0
154− , 9.0, 10.0, 11.0, 12.0 ]
155− , (3><4)
156− [ 21.0, 22.0, 23.0, 24.0
135+ , 9.0, 10.0, 11.0, 12.0
136+ -- -- --
137+ , 21.0, 22.0, 23.0, 24.0
157138 , 25.0, 26.0, 27.0, 28.0
158− , 29.0, 30.0, 31.0, 32.0 ] ]
139+ , 29.0, 30.0, 31.0, 32.0 ]
159140 expected = (6><8)
160141 [ 1.0, 2.0, 5.0, 6.0 , 21.0, 22.0, 25.0, 26.0
161142 , 2.0, 3.0, 6.0, 7.0 , 22.0, 23.0, 26.0, 27.0
@@ -163,36 +144,36 @@ prop_vid2col_no_stride = once $
163144 , 5.0, 6.0, 9.0, 10.0 , 25.0, 26.0, 29.0, 30.0
164145 , 6.0, 7.0, 10.0, 11.0 , 26.0, 27.0, 30.0, 31.0
165146 , 7.0, 8.0, 11.0, 12.0 , 27.0, 28.0, 31.0, 32.0 ]
166− out = vid2colUnsafe 2 2 1 1 3 4 input
167− in expected === out
147+ out_c = vid2col 2 2 1 1 3 4 input
148+ in expected === out_c
168149
169150prop_vid2col_stride = once $
170− let input = [(3><4)
151+ let input = (6><4)
171152 [ 1.0, 2.0, 3.0, 4.0
172153 , 5.0, 6.0, 7.0, 8.0
173− , 9.0, 10.0, 11.0, 12.0 ]
174− , (3><4)
175− [ 21.0, 22.0, 23.0, 24.0
154+ , 9.0, 10.0, 11.0, 12.0
155+ -- -- -- -- --
156+ , 21.0, 22.0, 23.0, 24.0
176157 , 25.0, 26.0, 27.0, 28.0
177− , 29.0, 30.0, 31.0, 32.0 ] ]
158+ , 29.0, 30.0, 31.0, 32.0 ]
178159 expected = (4><8)
179160 [ 1.0, 2.0, 5.0, 6.0 , 21.0, 22.0, 25.0, 26.0
180161 , 3.0, 4.0, 7.0, 8.0 , 23.0, 24.0, 27.0, 28.0
181162 , 5.0, 6.0, 9.0, 10.0 , 25.0, 26.0, 29.0, 30.0
182163 , 7.0, 8.0, 11.0, 12.0 , 27.0, 28.0, 31.0, 32.0 ]
183− out = vid2colUnsafe 2 2 1 2 3 4 input
184− in expected === out
164+ out_c = vid2col 2 2 1 2 3 4 input
165+ in expected === out_c
185166
186167prop_vid2col_invert = once $
187− let input = [(3><4)
168+ let input = (6><4)
188169 [ 1.0, 2.0, 3.0, 4.0
189170 , 5.0, 6.0, 7.0, 8.0
190− , 9.0, 10.0, 11.0, 12.0 ]
191− , (3><4)
192− [ 21.0, 22.0, 23.0, 24.0
171+ , 9.0, 10.0, 11.0, 12.0
172+ -- -- -- -- --
173+ , 21.0, 22.0, 23.0, 24.0
193174 , 25.0, 26.0, 27.0, 28.0
194− , 29.0, 30.0, 31.0, 32.0 ] ]
195− out = col2vidUnsafe 3 2 3 2 3 4 . vid2colUnsafe 3 2 3 2 3 4 $ input
175+ , 29.0, 30.0, 31.0, 32.0 ]
176+ out = col2vid 3 2 3 2 3 4 . vid2col 3 2 3 2 3 4 $ input
196177 in input === out
197178
198179
@@ -221,29 +202,22 @@ prop_single_conv_forwards = once $
221202
222203 convLayer = Convolution myKernel zeroKernel :: Convolution 1 4 2 2 1 1
223204
224− input = S3D' ( mkVector [HStatic.matrix
205+ input = S3D' ( HStatic.matrix
225206 [ 1.0, 2.0, 5.0
226− , 3.0, 4.0, 6.0] :: HStatic.L 2 3] ) :: S' ('D3 2 3 1)
207+ , 3.0, 4.0, 6.0] :: HStatic.L 2 3 ) :: S' ('D3 2 3 1)
227208
228− expect = [HStatic.matrix
229− [ -3.0 , -4.0 ] :: HStatic.L 1 2
230− ,HStatic.matrix
231− [ -1.0 , 1.0 ] :: HStatic.L 1 2
232− ,HStatic.matrix
233− [ 5.0 , 9.0 ] :: HStatic.L 1 2
234− ,HStatic.matrix
235− [ -7.0 , -10.0 ] :: HStatic.L 1 2] :: [HStatic.L 1 2]
209+ expect = HStatic.matrix
210+ [ -3.0 , -4.0
211+ , -1.0 , 1.0
212+ , 5.0 , 9.0
213+ , -7.0 , -10.0 ] :: HStatic.L 4 2
236214 out = runForwards convLayer input :: S' ('D3 1 2 4)
237215
238− grad = S3D' ( mkVector
239− [HStatic.matrix
240− [ 1 , 0 ] :: HStatic.L 1 2
241− ,HStatic.matrix
242− [ 0 , 0 ] :: HStatic.L 1 2
243− ,HStatic.matrix
244− [ 0 , 0 ] :: HStatic.L 1 2
245− ,HStatic.matrix
246− [ 0 , 1 ] :: HStatic.L 1 2] ) :: S' ('D3 1 2 4)
216+ grad = S3D' (HStatic.matrix
217+ [ 1 , 0
218+ , 0 , 0
219+ , 0 , 0
220+ , 0 , 1 ] :: HStatic.L 4 2 ) :: S' ('D3 1 2 4)
247221
248222 expectBack = (HStatic.matrix
249223 [ 1.0, 0.0, 0.0
@@ -252,8 +226,8 @@ prop_single_conv_forwards = once $
252226
253227 in case (out, inX, nc) of
254228 (S3D' out' , S3D' inX', Convolution' backGrad)
255− -> ((HStatic.extract <$> expect) === (HStatic.extract <$> vecToList out'))
256− .&&. ([HStatic.extract expectBack] === (HStatic.extract <$> vecToList inX'))
229+ -> (HStatic.extract expect === HStatic.extract out')
230+ .&&. (HStatic.extract expectBack === HStatic.extract inX')
257231 .&&. (HStatic.extract expectedGradient === HStatic.extract backGrad)
258232
259233return []