From: Yangqing Jia Date: Fri, 27 Sep 2013 15:34:12 +0000 (-0700) Subject: softmax bug fix and net testing X-Git-Tag: submit/tizen/20180823.020014~995 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=032af1e972c0da6ea321504a3207b967fb82b24c;p=platform%2Fupstream%2Fcaffeonacl.git softmax bug fix and net testing --- diff --git a/src/caffe/layers/softmax_layer.cpp b/src/caffe/layers/softmax_layer.cpp index ead05b3..7e8bbd1 100644 --- a/src/caffe/layers/softmax_layer.cpp +++ b/src/caffe/layers/softmax_layer.cpp @@ -1,9 +1,10 @@ // Copyright 2013 Yangqing Jia +#include + #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" #include "caffe/util/math_functions.hpp" -#include using std::max; @@ -34,11 +35,15 @@ void SoftmaxLayer::Forward_cpu(const vector*>& bottom, int num = bottom[0]->num(); int dim = bottom[0]->count() / bottom[0]->num(); memcpy(top_data, bottom_data, sizeof(Dtype) * bottom[0]->count()); - // we need to subtract the sum to avoid numerical issues, compute the exp, + // we need to subtract the max to avoid numerical issues, compute the exp, // and then normalize. // Compute sum - caffe_cpu_gemv(CblasNoTrans, num, dim, 1., bottom_data, - sum_multiplier_.cpu_data(), 0., scale_data); + for (int i = 0; i < num; ++i) { + scale_data[i] = bottom_data[i*dim]; + for (int j = 0; j < dim; ++j) { + scale_data[i] = max(scale_data[i], bottom_data[i * dim + j]); + } + } // subtraction caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, num, dim, 1, -1., scale_data, sum_multiplier_.cpu_data(), 1., top_data); diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index 75c9043..ac9f6a9 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -121,7 +121,9 @@ Dtype Net::Backward() { Dtype loss = 0; // TODO(Yangqing): figure out those layers that do not need backward. for (int i = layers_.size() - 1; i >= 0; --i) { - loss += layers_[i]->Backward(top_vecs_[i], true, &bottom_vecs_[i]); + Dtype layer_loss = layers_[i]->Backward( + top_vecs_[i], true, &bottom_vecs_[i]); + loss += layer_loss; } return loss; } diff --git a/src/caffe/test/test_net_proto.cpp b/src/caffe/test/test_net_proto.cpp index f53107e..f0b0e7d 100644 --- a/src/caffe/test/test_net_proto.cpp +++ b/src/caffe/test/test_net_proto.cpp @@ -8,6 +8,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/net.hpp" +#include "caffe/filler.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/test/lenet.hpp" @@ -35,6 +36,13 @@ TYPED_TEST(NetProtoTest, TestSetup) { // Now, initialize a network using the parameter shared_ptr > data(new Blob(10, 1, 28, 28)); shared_ptr > label(new Blob(10, 1, 1, 1)); + FillerParameter filler_param; + shared_ptr > filler; + filler.reset(new ConstantFiller(filler_param)); + filler->Fill(label.get()); + filler.reset(new GaussianFiller(filler_param)); + filler->Fill(data.get()); + vector*> bottom_vec; bottom_vec.push_back(data.get()); bottom_vec.push_back(label.get()); @@ -43,6 +51,7 @@ TYPED_TEST(NetProtoTest, TestSetup) { EXPECT_EQ(caffe_net.layer_names().size(), 9); EXPECT_EQ(caffe_net.blob_names().size(), 10); + // Print a few statistics to see if things are correct for (int i = 0; i < caffe_net.blobs().size(); ++i) { LOG(ERROR) << "Blob: " << caffe_net.blob_names()[i]; LOG(ERROR) << "size: " << caffe_net.blobs()[i]->num() << ", " @@ -50,6 +59,13 @@ TYPED_TEST(NetProtoTest, TestSetup) { << caffe_net.blobs()[i]->height() << ", " << caffe_net.blobs()[i]->width(); } + // Run the network without training. + vector*> top_vec; + LOG(ERROR) << "Performing Forward"; + caffe_net.Forward(bottom_vec, &top_vec); + LOG(ERROR) << "Performing Backward"; + LOG(ERROR) << caffe_net.Backward(); + } } // namespace caffe