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);
// Copyright 2013 Yangqing Jia
+#include <cmath>
#include <cstring>
#include <cuda_runtime.h>
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_);
+}
}