28 namespace mic {
namespace neural_nets {
namespace unit_tests {
37 mic::types::MatrixPtr<double> predicted_y = layer.forward(x);
38 mic::types::MatrixPtr<double> dy = loss.calculateGradient(target_y, predicted_y);
42 std::map<std::string, size_t> keys = layer.p.keys();
45 mic::types::MatrixArray<double> grads;
47 grads.add(i.first, MAKE_MATRIX_PTR(
double, *layer.g[i.first]));
53 mic::types::MatrixArray<double> ngrads;
57 ngrads.add(i.first, MAKE_MATRIX_PTR(
double, *ngrad));
66 mic::types::MatrixPtr<double> grad = grads[i.first];
67 mic::types::MatrixPtr<double> ngrad = ngrads[i.first];
69 for (
size_t j=0; j<(size_t)grad->size(); j++){
71 EXPECT_LE( fabs((*grad)[j] - (*ngrad)[j]), eps) <<
"Too big difference between grad and numerical grad of " << i.first <<
" at position j=" << j;
87 mic::types::MatrixPtr<double> predicted_y = layer.forward(x);
88 mic::types::MatrixPtr<double> dy = loss.calculateGradient(target_y, predicted_y);
92 std::map<std::string, size_t> keys = layer.p.keys();
95 mic::types::MatrixArray<double> grads;
97 grads.add(i.first, MAKE_MATRIX_PTR(
double, *layer.g[i.first]));
103 mic::types::MatrixArray<double> ngrads;
104 for (
auto& i: keys) {
107 ngrads.add(i.first, MAKE_MATRIX_PTR(
double, *ngrad));
114 for (
auto& i: keys) {
116 mic::types::MatrixPtr<double> grad = grads[i.first];
117 mic::types::MatrixPtr<double> ngrad = ngrads[i.first];
119 for (
size_t j=0; j<(size_t)grad->size(); j++){
121 EXPECT_LE( fabs((*grad)[j] - (*ngrad)[j]), eps) <<
"Too big difference between grad and numerical grad of " << i.first <<
" at position j=" << j;
137 size_t iteration = 0;
142 while (iteration < 1000) {
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);
150 if (loss_value < eps)
155 layer.update(0.01, 0.0);
160 EXPECT_LE(loss_value, eps);
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...