lint & reduce gradient check stepsize to pass checks
authorJeff Donahue <jeff.donahue@gmail.com>
Mon, 8 Sep 2014 15:49:42 +0000 (17:49 +0200)
committerJeff Donahue <jeff.donahue@gmail.com>
Mon, 8 Sep 2014 16:05:17 +0000 (18:05 +0200)
src/caffe/layers/eltwise_layer.cpp
src/caffe/layers/eltwise_layer.cu
src/caffe/test/test_eltwise_layer.cpp

index b25611a..46034be 100644 (file)
@@ -1,5 +1,5 @@
-#include <vector>
 #include <cfloat>
+#include <vector>
 
 #include "caffe/layer.hpp"
 #include "caffe/util/math_functions.hpp"
@@ -77,19 +77,18 @@ void EltwiseLayer<Dtype>::Forward_cpu(
     bottom_data_b = bottom[1]->cpu_data();
     for (int idx = 0; idx < count; ++idx) {
       if (bottom_data_a[idx] > bottom_data_b[idx]) {
-        top_data[idx] = bottom_data_a[idx]; // maxval
-        mask[idx] = 0; // maxid
+        top_data[idx] = bottom_data_a[idx];  // maxval
+        mask[idx] = 0;  // maxid
       } else {
-        top_data[idx] = bottom_data_b[idx]; // maxval
-        mask[idx] = 1; // maxid
+        top_data[idx] = bottom_data_b[idx];  // maxval
+        mask[idx] = 1;  // maxid
       }
     }
     // bottom 2++
-    bottom_data_a = top_data;
     for (int blob_idx = 2; blob_idx < bottom.size(); ++blob_idx) {
       bottom_data_b = bottom[blob_idx]->cpu_data();
       for (int idx = 0; idx < count; ++idx) {
-        if (bottom_data_a[idx] < bottom_data_b[idx]) {
+        if (bottom_data_b[idx] > top_data[idx]) {
           top_data[idx] = bottom_data_b[idx];  // maxval
           mask[idx] = blob_idx;  // maxid
         }
index 78f911b..c0d47fd 100644 (file)
@@ -1,5 +1,5 @@
-#include <vector>
 #include <cfloat>
+#include <vector>
 
 #include "caffe/layer.hpp"
 #include "caffe/util/math_functions.hpp"
@@ -31,7 +31,7 @@ __global__ void MaxForward(const int nthreads, const Dtype* bottom_data_a,
   }
 }
 
-template<typename Dtype>
+template <typename Dtype>
 void EltwiseLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
     vector<Blob<Dtype>*>* top) {
   int* mask = NULL;
@@ -68,7 +68,7 @@ void EltwiseLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
   }
 }
 
-template<typename Dtype>
+template <typename Dtype>
 __global__ void MaxBackward(const int nthreads, const Dtype* top_diff,
     const int blob_idx, const int* mask, Dtype* bottom_diff) {
   CUDA_KERNEL_LOOP(index, nthreads) {
@@ -80,7 +80,7 @@ __global__ void MaxBackward(const int nthreads, const Dtype* top_diff,
   }
 }
 
-template<typename Dtype>
+template <typename Dtype>
 void EltwiseLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
     const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom) {
   const int* mask = NULL;
@@ -119,8 +119,8 @@ void EltwiseLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
         break;
       case EltwiseParameter_EltwiseOp_MAX:
         mask = max_idx_->gpu_data();
-        // NOLINT_NEXT_LINE(whitespace/operators)
-        MaxBackward<Dtype> <<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
+        MaxBackward<Dtype>  // NOLINT_NEXT_LINE(whitespace/operators)
+            <<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
             count, top_diff, i, mask, bottom_diff);
         break;
       default:
index 3c61728..d5cf082 100644 (file)
@@ -1,3 +1,4 @@
+#include <algorithm>
 #include <vector>
 
 #include "gtest/gtest.h"
@@ -23,6 +24,7 @@ class EltwiseLayerTest : public MultiDeviceTest<TypeParam> {
         blob_bottom_c_(new Blob<Dtype>(2, 3, 4, 5)),
         blob_top_(new Blob<Dtype>()) {
     // fill the values
+    Caffe::set_random_seed(1701);
     FillerParameter filler_param;
     UniformFiller<Dtype> filler(filler_param);
     filler.Fill(this->blob_bottom_a_);
@@ -188,9 +190,8 @@ TYPED_TEST(EltwiseLayerTest, TestMax) {
   const Dtype* in_data_b = this->blob_bottom_b_->cpu_data();
   const Dtype* in_data_c = this->blob_bottom_c_->cpu_data();
   for (int i = 0; i < count; ++i) {
-    EXPECT_GE(data[i], in_data_a[i]);
-    EXPECT_GE(data[i], in_data_b[i]);
-    EXPECT_GE(data[i], in_data_c[i]);
+    EXPECT_EQ(data[i],
+              std::max(in_data_a[i], std::max(in_data_b[i], in_data_c[i])));
   }
 }
 
@@ -200,7 +201,7 @@ TYPED_TEST(EltwiseLayerTest, TestMaxGradient) {
   EltwiseParameter* eltwise_param = layer_param.mutable_eltwise_param();
   eltwise_param->set_operation(EltwiseParameter_EltwiseOp_MAX);
   EltwiseLayer<Dtype> layer(layer_param);
-  GradientChecker<Dtype> checker(1e-2, 1e-3);
+  GradientChecker<Dtype> checker(1e-4, 1e-3);
   checker.CheckGradientEltwise(&layer, &(this->blob_bottom_vec_),
       &(this->blob_top_vec_));
 }