reduced multiplications & fixed unit test
authorqipeng <pengrobertqi@163.com>
Sun, 20 Jul 2014 00:28:52 +0000 (17:28 -0700)
committerqipeng <pengrobertqi@163.com>
Mon, 21 Jul 2014 22:32:22 +0000 (15:32 -0700)
src/caffe/layers/relu_layer.cpp
src/caffe/layers/relu_layer.cu
src/caffe/test/test_neuron_layer.cpp

index 57ccf97..3a3e8a2 100644 (file)
@@ -33,8 +33,8 @@ void ReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
     const int count = (*bottom)[0]->count();
     Dtype negative_slope = this->layer_param_.relu_param().negative_slope();
     for (int i = 0; i < count; ++i) {
-      bottom_diff[i] = top_diff[i] * (bottom_data[i] >= 0)
-          + top_diff[i] * negative_slope * (bottom_data[i] < 0);
+      bottom_diff[i] = top_diff[i] * ((bottom_data[i] >= 0)
+          + negative_slope * (bottom_data[i] < 0));
     }
   }
 }
index f5a24a9..503147c 100644 (file)
@@ -39,8 +39,8 @@ template <typename Dtype>
 __global__ void ReLUBackward(const int n, const Dtype* in_diff,
     const Dtype* in_data, Dtype* out_diff, Dtype negative_slope) {
   CUDA_KERNEL_LOOP(index, n) {
-    out_diff[index] = in_diff[index] * (in_data[index] >= 0)
-        + in_diff[index] * (in_data[index] < 0) * negative_slope;
+    out_diff[index] = in_diff[index] * ((in_data[index] >= 0)
+        + (in_data[index] < 0) * negative_slope);
   }
 }
 
index db9b934..7a7af31 100644 (file)
@@ -81,8 +81,8 @@ TYPED_TEST(NeuronLayerTest, TestReLUWithNegativeSlope) {
 TYPED_TEST(NeuronLayerTest, TestReLUGradientWithNegativeSlope) {
   typedef typename TypeParam::Dtype Dtype;
   LayerParameter layer_param;
-  ReLULayer<Dtype> layer(layer_param);
   layer_param.ParseFromString("relu_param{negative_slope:0.01}");
+  ReLULayer<Dtype> layer(layer_param);
   GradientChecker<Dtype> checker(1e-2, 1e-3, 1701, 0., 0.01);
   checker.CheckGradientEltwise(&layer, &(this->blob_bottom_vec_),
       &(this->blob_top_vec_));