MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Convolution_tests.cpp
Go to the documentation of this file.
1 
25 #include "Convolution_tests.hpp"
26 
27 namespace mic { namespace neural_nets { namespace unit_tests {
28 
33 TEST(Convolutions, LayerDimensions) {
34 
35  // Stride = 1.
37 
38  ASSERT_EQ(l1.output_height, 3);
39  ASSERT_EQ(l1.output_width, 5);
40  ASSERT_EQ(l1.s["y"]->rows(), 3*5*1);
41  // Stride = 2.
43  ASSERT_EQ(l2.output_height, 2);
44  ASSERT_EQ(l2.output_width, 3);
45  ASSERT_EQ(l2.s["y"]->rows(), 2*3*1);
46 
47  // Stride = 3.
49  ASSERT_EQ(l3.output_height, 3);
50  ASSERT_EQ(l3.output_width, 4);
51  ASSERT_EQ(l3.s["y"]->rows(), 3*4*1);
52 
53  // Stride = 3.
55  ASSERT_EQ(l4.output_height, 2);
56  ASSERT_EQ(l4.output_width, 3);
57  ASSERT_EQ(l4.s["y"]->rows(), 2*3*1);
58 
59  // Stride = 4.
61  ASSERT_EQ(l5.output_height, 3);
62  ASSERT_EQ(l5.output_width, 4);
63  ASSERT_EQ(l5.s["y"]->rows(), 3*4*1);
64 
65  // Stride = 4.
67  ASSERT_EQ(l6.output_height, 2);
68  ASSERT_EQ(l6.output_width, 3);
69  ASSERT_EQ(l6.s["y"]->rows(), 2*3*1);
70 
71 }
72 
73 
79 
80  /*std::cout<<"W00 = \n" << (*layer.p["W0x0"]) <<std::endl;
81  std::cout<<"W01 = \n" << (*layer.p["W0x1"]) <<std::endl;
82  std::cout<<"W10 = \n" << (*layer.p["W1x0"]) <<std::endl;
83  std::cout<<"W11 = \n" << (*layer.p["W1x1"]) <<std::endl;
84  std::cout<<"x = \n" << (*x) <<std::endl;
85  std::cout<<"desired_y = \n" << (*desired_y) <<std::endl;*/
86 
87  // Forward pass.
88  mic::types::MatrixPtr<double> y = layer.forward(x);
89  //std::cout<<"y = \n" << (*y) <<std::endl;
90 
91  // Check output.
92  for (size_t i=0; i<8; i++)
93  ASSERT_EQ((*y)[i], (*desired_y)[i]) << "at position " << i;
94 
95 }
96 
97 
103 
104  // Backward pass - need to set x.
105  layer.forward(x);
106  mic::types::MatrixPtr<double> dx = layer.backward(dy);
107  //std::cout<<"dx = \n" << (*dx).transpose() <<std::endl;
108 
109  // Check resulting dx gradient.
110  for (size_t i=0; i<8; i++)
111  ASSERT_EQ((*desired_dx)[i], (*dx)[i]) << "at position " << i;
112 
113  mic::types::MatrixPtr<double> db = layer.g["b"];
114  // Check resulting db gradient.
115  for (size_t i=0; i<2; i++)
116  ASSERT_EQ((*desired_db)[i], (*db)[i]) << "at position " << i;
117 
118  // Check resulting dW gradient.
119  std::cout<<"dW0x0 = \n" << (*layer.g["W0x0"]).transpose() <<std::endl;
120  std::cout<<"dW1x1 = \n" << (*layer.g["W1x1"]).transpose() <<std::endl;
121  std::cout<<"dW0x1 = \n" << (*layer.g["W0x1"]).transpose() <<std::endl;
122  std::cout<<"dW1x0 = \n" << (*layer.g["W1x0"]).transpose() <<std::endl;
123 
124  ASSERT_EQ((*desired_dW)[0], (*layer.g["W0x0"])[0]);
125  ASSERT_EQ((*desired_dW)[1], (*layer.g["W1x1"])[0]);
126  ASSERT_EQ((*desired_dW)[2], (*layer.g["W0x1"])[0]);
127  ASSERT_EQ((*desired_dW)[3], (*layer.g["W1x0"])[0]);
128 
129  // Second backward - just to assure that all the "internal dimensions" are ok after the first pass.
130 // layer.backward(dy);
131 }
132 
133 
134 
140 
141  // Forward pass.
142  mic::types::MatrixPtr<float> output = layer.forward(x);
143  //std::cout<<"output = \n" << (*output) <<std::endl;
144 
145  // Check output.
146  for (size_t i=0; i<9; i++)
147  ASSERT_EQ((*output)[i], (*desired_y)[i]) << "at position " << i;
148 
149  // Second forward - just to check whether "internal dimensions" are ok after the first pass.
150  output = layer.forward(x);
151 }
152 
153 
159 
160  // Forward pass.
161  mic::types::MatrixPtr<float> y = layer.forward(x);
162 
163  // Check output.
164  for (size_t i=0; i<4; i++)
165  ASSERT_EQ((*y)[i], (*desired_y)[i]) << "at position " << i;
166 
167 }
168 
169 
175 
176  // Backward pass - need to set x.
177  layer.forward(x);
178  mic::types::MatrixPtr<float> dx = layer.backward(dy);
179 
180  // Check resulting dx gradient.
181  for (size_t i=0; i<8; i++)
182  ASSERT_EQ((*desired_dx)[i], (*dx)[i]) << "at position " << i;
183 
184  // Check resulting db gradient.
185  mic::types::MatrixPtr<float> db = layer.g["b"];
186  ASSERT_EQ((*desired_db)[0], (*db)[0]);
187 
188  // Check resulting dW gradient.
189  mic::types::MatrixPtr<float> dW = layer.g["W0x0"];
190  for (size_t i=0; i<4; i++)
191  ASSERT_EQ((*desired_dW)[i], (*dW)[i]) << "at position " << i;
192 }
193 
194 
195 
201 
202  // Forward pass.
203  mic::types::MatrixPtr<double> y = layer.forward(x);
204 
205  // Check output.
206  for (size_t i=0; i<12; i++)
207  ASSERT_EQ((*y)[i], (*desired_y)[i]) << "at position " << i;
208 
209 }
210 
211 
217 
218  // Backward pass - need to set x.
219  layer.forward(x);
220  mic::types::MatrixPtr<double> dx = layer.backward(dy);
221 
222  // Check resulting dx gradient.
223  for (size_t i=0; i<16; i++)
224  ASSERT_EQ((*desired_dx)[i], (*dx)[i]) << "at position " << i;
225 
226  // Check resulting db gradient.
227  mic::types::MatrixPtr<double> db = layer.g["b"];
228  for (size_t i=0; i<3; i++)
229  ASSERT_EQ((*desired_db)[i], (*db)[i]);
230 
231  // Check resulting dW gradient.
232  ASSERT_EQ((*desired_dW)[0], (*layer.g["W0x0"])[0]);
233  ASSERT_EQ((*desired_dW)[1], (*layer.g["W1x0"])[0]);
234  ASSERT_EQ((*desired_dW)[2], (*layer.g["W2x0"])[0]);
235 }
236 
244 
245  // Check filter size - W.
246  ASSERT_EQ((*layer.p["W0x0"]).rows(), 1);
247  ASSERT_EQ((*layer.p["W0x0"]).cols(), 9);
248 
249  // Check filter size - b.
250  ASSERT_EQ((*layer.p["b"]).rows(), 1);
251  ASSERT_EQ((*layer.p["b"]).cols(), 1);
252 
253  // Assert input size.
254  ASSERT_EQ((*layer.s["x"]).rows(), 25);
255  ASSERT_EQ((*layer.s["x"]).cols(), 1);
256 
257  // Assert output size.
258  ASSERT_EQ((*layer.s["y"]).rows(), 9);
259  ASSERT_EQ((*layer.s["y"]).cols(), 1);
260 
261 }
262 
263 
269 
270  // Forward pass.
271  mic::types::MatrixPtr<float> y = layer.forward(x);
272 
273  // Check y.
274  for (size_t i=0; i<9; i++)
275  ASSERT_EQ((*y)[i], (*desired_y)[i]) << "at position " << i;
276 
277 }
278 
279 
285 
286  // Forward pass.
287  mic::types::MatrixPtr<float> y = layer.forward(x);
288 
289  // Check output.
290  for (size_t i=0; i<4; i++)
291  ASSERT_EQ((*y)[i], (*desired_y)[i]) << "at position " << i;
292 
293 }
294 
295 
301 
302  // Backward pass - need to set x.
303  layer.forward(x);
304  mic::types::MatrixPtr<float> dx = layer.backward(dy);
305 
306  // Check resulting dx gradient.
307  for (size_t i=0; i<25; i++)
308  ASSERT_EQ((*desired_dx)[i], (*dx)[i]) << "at position " << i;
309 
310  // Check resulting db gradient.
311  mic::types::MatrixPtr<float> db = layer.g["b"];
312  for (size_t i=0; i<1; i++)
313  ASSERT_EQ((*desired_db)[i], (*db)[i]);
314 
315  // Check resulting dW gradient.
316  for (size_t i=0; i<4; i++)
317  ASSERT_EQ((*desired_dW)[i], (*layer.g["W0x0"])[i]);
318 }
319 
325 
326  // Forward pass.
327  mic::types::MatrixPtr<float> output = layer.forward(x);
328  //std::cout<<"output = \n" << (*output) <<std::endl;
329 
330  // Check output.
331  for (size_t i=0; i<6; i++)
332  ASSERT_EQ((*output)[i], (*desired_y)[i]) << "at position " << i;
333 
334 }
335 
336 
337 
343 
344  // Forward pass.
345  mic::types::MatrixPtr<float> output = layer.forward(x);
346  //std::cout<<"output = \n" << (*output) <<std::endl;
347 
348  // Check output.
349  for (size_t i=0; i<18; i++)
350  ASSERT_EQ((*output)[i], (*desired_y)[i]) << "at position " << i;
351 
352 }
353 
354 
355 
356 
357 } } } //: namespaces
358 
359 int main(int argc, char **argv) {
360  testing::InitGoogleTest(&argc, argv);
361  return RUN_ALL_TESTS();
362 }
Test Fixture - layer of input size 4x4x1 and with filter bank of 3 filters of size 1x1 with stride 3...
Test Fixture - layer of input size 5x6x1 and with filter bank of 1 filter of size 4x4 with stride 1...
TEST(Convolutions, LayerDimensions)
Test Fixture - layer of input size 4x4x1 and with filter bank of 1 filters of size 2x2 with stride 2...
Test Fixture - layer of input size 2x2x2 and with filter bank of 2 filters of size 1x1 with stride 1...
Test Fixture - layer of input size 5x5x1 and with filter bank of 1 filter of size 3x3 with stride 1 (...
TEST_F(Conv2x2x2Filter2x1x1s1Double, NumericalGradientCheck)
Numerical gradient test of all parameters for layer of input size 2x2x2 and with filter bank of 2 fil...
Test Fixture - layer of input size 3x3x2 and with filter bank of 3 filters of size 2x2 with stride 1...
mic::types::MatrixArray< eT > s
States - contains input [x] and output [y] matrices.
Definition: Layer.hpp:753
size_t output_height
Number of receptive fields in a single channel - vertical direction.
Definition: Layer.hpp:735
size_t output_width
Number of receptive fields in a single channel - horizontal direction.
Definition: Layer.hpp:738
int main(int argc, char **argv)
Test Fixture - layer of input size 5x5x1 and with filter bank of 1 filter of size 2x2 with stride 3 (...
Test Fixture - layer of input size 7x7x3 and with filter bank of 2 filters of 3x3 with stride 2 (floa...