bugfix
authorYangqing Jia <jiayq84@gmail.com>
Thu, 26 Sep 2013 05:38:20 +0000 (22:38 -0700)
committerYangqing Jia <jiayq84@gmail.com>
Thu, 26 Sep 2013 05:38:20 +0000 (22:38 -0700)
src/caffe/blob.cpp
src/caffe/layers/softmax_layer.cpp
src/caffe/test/test_softmax_layer.cpp

index aacb05c..ecb37b7 100644 (file)
@@ -58,6 +58,7 @@ const Blob<Dtype>& Blob<Dtype>::operator=(const Blob<Dtype>& source) {
     memcpy(diff_->mutable_cpu_data(), source.cpu_diff(),
         count_ * sizeof(Dtype));
   }
+  return (*this);
 }
 
 template <typename Dtype>
index 31f25c3..ead05b3 100644 (file)
@@ -19,7 +19,7 @@ void SoftmaxLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
   sum_multiplier_.Reshape(1, bottom[0]->channels(),
       bottom[0]->height(), bottom[0]->width());
   Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
-  for (int i = 0; i < bottom[0]->num(); ++i) {
+  for (int i = 0; i < sum_multiplier_.count(); ++i) {
     multiplier_data[i] = 1.;
   }
   scale_.Reshape(bottom[0]->num(), 1, 1, 1);
index 37391ea..253ea8a 100644 (file)
@@ -1,5 +1,6 @@
 // Copyright 2013 Yangqing Jia
 
+#include <cmath>
 #include <cstring>
 #include <cuda_runtime.h>
 
@@ -39,15 +40,34 @@ class SoftmaxLayerTest : public ::testing::Test {
 typedef ::testing::Types<float, double> Dtypes;
 TYPED_TEST_CASE(SoftmaxLayerTest, Dtypes);
 
-TYPED_TEST(SoftmaxLayerTest, TestReLUCPU) {
+TYPED_TEST(SoftmaxLayerTest, TestForwardCPU) {
   LayerParameter layer_param;
   Caffe::set_mode(Caffe::CPU);
   SoftmaxLayer<TypeParam> layer(layer_param);
   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
-  NOT_IMPLEMENTED;
+  for (int i = 0; i < this->blob_bottom_->num(); ++i) {
+    TypeParam scale = 0;
+    for (int j = 0; j < this->blob_bottom_->channels(); ++j) {
+      scale += exp(this->blob_bottom_->data_at(i, j, 0, 0));
+    }
+    for (int j = 0; j < this->blob_bottom_->channels(); ++j) {
+      EXPECT_GE(this->blob_top_->data_at(i, j, 0, 0) + 1e-4,
+          exp(this->blob_bottom_->data_at(i, j, 0, 0)) / scale)
+          << "debug: " << i << " " << j;
+      EXPECT_LE(this->blob_top_->data_at(i, j, 0, 0) - 1e-4,
+          exp(this->blob_bottom_->data_at(i, j, 0, 0)) / scale)
+          << "debug: " << i << " " << j;
+    }
+  }
 }
 
-
+TYPED_TEST(SoftmaxLayerTest, TestGradientCPU) {
+  LayerParameter layer_param;
+  Caffe::set_mode(Caffe::CPU);
+  SoftmaxLayer<TypeParam> layer(layer_param);
+  GradientChecker<TypeParam> checker(1e-2, 1e-3);
+  checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
+}
 
 }