MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Convolution_convergence_tests.cpp
Go to the documentation of this file.
1 
26 #include "Convolution_tests.hpp"
27 
28 namespace mic { namespace neural_nets { namespace unit_tests {
29 
34 TEST_F(Conv2x2x2Filter2x1x1s1Double, NumericalGradientCheck) {
35 
36  // Calculate gradients.
37  mic::types::MatrixPtr<double> predicted_y = layer.forward(x);
38  mic::types::MatrixPtr<double> dy = loss.calculateGradient(target_y, predicted_y);
39  layer.backward(dy);
40 
41  // Get differentiable parameters.
42  std::map<std::string, size_t> keys = layer.p.keys();
43 
44  // Store resulting gradients - make a copy!
45  mic::types::MatrixArray<double> grads;
46  for (auto& i: keys) {
47  grads.add(i.first, MAKE_MATRIX_PTR(double, *layer.g[i.first]));
48  //std::cout << "** d" << i.first << " = \n" << *(grads[i.first]) << std::endl;
49  }//: for
50 
51  // Calculate numerical gradients.
52  double delta = 1e-5;
53  mic::types::MatrixArray<double> ngrads;
54  for (auto& i: keys) {
55  mic::types::MatrixPtr<double> ngrad = layer.calculateNumericalGradient<mic::neural_nets::loss::SquaredErrorLoss<double> >(x, target_y, layer.p[i.first], loss, delta);
56  // Store gradient - make a copy as well.
57  ngrads.add(i.first, MAKE_MATRIX_PTR(double, *ngrad));
58  //std::cout << "** n" << i.first << " = \n" << *(ngrads[i.first]) << std::endl;
59  }//: for
60 
61 
62  // Compare gradients.
63  double eps = 1e-8;
64  for (auto& i: keys) {
65  // Get gradient and numerical gradient.
66  mic::types::MatrixPtr<double> grad = grads[i.first];
67  mic::types::MatrixPtr<double> ngrad = ngrads[i.first];
68  // Iterate through params.
69  for (size_t j=0; j<(size_t)grad->size(); j++){
70  //std::cout << "param " << i.first << " j=" << j << " (*grad)[j]= " << (*grad)[j] << " (*ngrad)[j]= " << (*ngrad)[j] << std::endl;
71  EXPECT_LE( fabs((*grad)[j] - (*ngrad)[j]), eps) << "Too big difference between grad and numerical grad of " << i.first << " at position j=" << j;
72  }//: for
73 
74  }//: for
75 
76 }
77 
78 
79 
84 TEST_F(Conv28x28x1Filter2x28x28s1Double, NumericalGradientCheck) {
85 
86  // Calculate gradients.
87  mic::types::MatrixPtr<double> predicted_y = layer.forward(x);
88  mic::types::MatrixPtr<double> dy = loss.calculateGradient(target_y, predicted_y);
89  layer.backward(dy);
90 
91  // Get differentiable parameters.
92  std::map<std::string, size_t> keys = layer.p.keys();
93 
94  // Store resulting gradients - make a copy!
95  mic::types::MatrixArray<double> grads;
96  for (auto& i: keys) {
97  grads.add(i.first, MAKE_MATRIX_PTR(double, *layer.g[i.first]));
98  //std::cout << "** d" << i.first << " = \n" << *(grads[i.first]) << std::endl;
99  }//: for
100 
101  // Calculate numerical gradients.
102  double delta = 1e-7;
103  mic::types::MatrixArray<double> ngrads;
104  for (auto& i: keys) {
105  mic::types::MatrixPtr<double> ngrad = layer.calculateNumericalGradient<mic::neural_nets::loss::SquaredErrorLoss<double> >(x, target_y, layer.p[i.first], loss, delta);
106  // Store gradient - make a copy as well.
107  ngrads.add(i.first, MAKE_MATRIX_PTR(double, *ngrad));
108  //std::cout << "** n" << i.first << " = \n" << *(ngrads[i.first]) << std::endl;
109  }//: for
110 
111 
112  // Compare gradients.
113  double eps = 1e-8;
114  for (auto& i: keys) {
115  // Get gradient and numerical gradient.
116  mic::types::MatrixPtr<double> grad = grads[i.first];
117  mic::types::MatrixPtr<double> ngrad = ngrads[i.first];
118  // Iterate through params.
119  for (size_t j=0; j<(size_t)grad->size(); j++){
120  //std::cout << "param " << i.first << " j=" << j << " (*grad)[j]= " << (*grad)[j] << " (*ngrad)[j]= " << (*ngrad)[j] << std::endl;
121  EXPECT_LE( fabs((*grad)[j] - (*ngrad)[j]), eps) << "Too big difference between grad and numerical grad of " << i.first << " at position j=" << j;
122  }//: for
123 
124  }//: for
125 
126 }
127 
128 
129 
135  double eps = 1e-6;
136  double loss_value;
137  size_t iteration = 0;
138  // Change optimization function from default GradientDescent.
139  //layer.setOptimization<mic::neural_nets::optimization::RMSProp<double> >();
140 
141  // Train for a number of iterations.
142  while (iteration < 1000) {
143  // Perform single learning step.
144  mic::types::MatrixPtr<double> predicted_y = layer.forward(x);
145  mic::types::MatrixPtr<double> dy = loss.calculateGradient(target_y, predicted_y);
146  loss_value = loss.calculateMeanLoss(target_y, predicted_y);
147 
148  //std::cout<<"[" << iteration << "]\t Loss = " << loss_value <<std::endl;
149  //std::cout << (*target_y).transpose() << " vs " << (*predicted_y).transpose() << std::endl;
150  if (loss_value < eps)
151  break;
152 
153  // Apply the changes - according to the optimization function.
154  layer.backward(dy);
155  layer.update(0.01, 0.0);
156  // Next iteration.
157  iteration++;
158  }//: while
159  // Check loss.
160  EXPECT_LE(loss_value, eps);
161 }
162 
163 
164 } } } //: namespaces
Test Fixture - layer of input size 2x2x2 and with filter bank of 2 filters of size 1x1 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 28x28x1 and with filter bank of 2 filters of size 28x28 with strid...