Gradient test fails on own implemented layer

25 views
Skip to first unread message

Aske Rasch Lejbølle

unread,
Sep 14, 2015, 7:48:35 AM9/14/15
to Caffe Users
Hi,

I have implemented my own layer (also called neighborhood difference layer if anyone should know it) which takes in two bottom blobs of e.g. size 2x2x2x2 and calculates the differences which results in two top blobs, each of size 2x2x10x10.
I will not go into detail, but in forward prop, it basically creates 5x5 difference patches from each input pixel (therefore 10x10).
I tested it and both the setup and forward propagation succeeds, but when test the back propagation using gradient test, it fails as all computed gradients are just 0.
Even with at very simple case (bottom blobs of size 1x1x1x1), the computed gradient is just 0.
Here is the backward_cpu - it simply passes the smallest error from each 5x5 patch to the corresponding pixel at the bottom blob.
<code>
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
Dtype* bottom_diff2 = bottom[1]->mutable_cpu_diff();
 
  Dtype min_temp = 0;
  Dtype min_temp2 = 0;
  Dtype min_val = 0;
  Dtype min_val2 = 0;
 
  for (int n = 0; n < top[0]->num(); ++n) {
    for (int c = 0; c < channels_i; ++c) {
        for (int h = 0; h < height_i; ++h) {
            for (int w = 0; w < width_i; ++w) {
                min_val = top[0]->diff_at(n,c,5*h,5*w);
                min_val2 = top[1]->diff_at(n,c,5*h,5*w);
               
                for (int y = 0; y < 5; ++y) {
                    for (int x = 0; x < 5; ++x) {
                        min_temp = top[0]->diff_at(n,c,(5*h)+y,(5*w)+x);
                        min_temp2 = top[1]->diff_at(n,c,(5*h)+y,(5*w)+x);
                       
                        if (min_temp < min_val){
                            min_val = min_temp;
                        }
                        if (min_temp2 < min_val2) {
                            min_val2 = min_temp2;
                        }
                    }
                }

                bottom_diff[bottom[0]->offset(n,c,h,w)] = min_val;
                bottom_diff2[bottom[1]->offset(n,c,h,w)] = min_val2;
            }
        }
       
    }
  }   
}

</code>

Should be said that I have not focused on optimizing it yet, but the code should be correct?

Best,
Aske
Reply all
Reply to author
Forward
0 new messages