From 61e1ef03537461f708bcb110e9decb69ca4de544 Mon Sep 17 00:00:00 2001 From: Evan Shelhamer Date: Thu, 26 Jun 2014 10:21:19 -0700 Subject: [PATCH] file SoftmaxWithLoss in with loss layers --- include/caffe/loss_layers.hpp | 43 +++++++++++++++++++++++++++++++++++++++++ include/caffe/vision_layers.hpp | 40 -------------------------------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/include/caffe/loss_layers.hpp b/include/caffe/loss_layers.hpp index 2f98a12..db7c63e 100644 --- a/include/caffe/loss_layers.hpp +++ b/include/caffe/loss_layers.hpp @@ -40,6 +40,49 @@ class LossLayer : public Layer { virtual inline int MaxTopBlobs() const { return 1; } }; +// Forward declare SoftmaxLayer for use in SoftmaxWithLossLayer. +template class SoftmaxLayer; + +/* SoftmaxWithLossLayer + Implements softmax and computes the loss. + + It is preferred over separate softmax + multinomiallogisticloss + layers due to more numerically stable gradients. + + In test, this layer could be replaced by simple softmax layer. +*/ +template +class SoftmaxWithLossLayer : public Layer { + public: + explicit SoftmaxWithLossLayer(const LayerParameter& param) + : Layer(param), softmax_layer_(new SoftmaxLayer(param)) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_SOFTMAX_LOSS; + } + virtual inline int ExactNumBottomBlobs() const { return 2; } + virtual inline int MaxTopBlobs() const { return 2; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual Dtype Forward_gpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom); + + shared_ptr > softmax_layer_; + // prob stores the output probability of the layer. + Blob prob_; + // Vector holders to call the underlying softmax layer forward and backward. + vector*> softmax_bottom_vec_; + vector*> softmax_top_vec_; +}; + /* SigmoidCrossEntropyLossLayer */ template diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index b7e1068..3fd7e2f 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -405,46 +405,6 @@ class SoftmaxLayer : public Layer { Blob scale_; }; -/* SoftmaxWithLossLayer - Implements softmax and computes the loss. - - It is preferred over separate softmax + multinomiallogisticloss - layers due to more numerically stable gradients. - - In test, this layer could be replaced by simple softmax layer. -*/ -template -class SoftmaxWithLossLayer : public Layer { - public: - explicit SoftmaxWithLossLayer(const LayerParameter& param) - : Layer(param), softmax_layer_(new SoftmaxLayer(param)) {} - virtual void SetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_SOFTMAX_LOSS; - } - virtual inline int ExactNumBottomBlobs() const { return 2; } - virtual inline int MaxTopBlobs() const { return 2; } - - protected: - virtual Dtype Forward_cpu(const vector*>& bottom, - vector*>* top); - virtual Dtype Forward_gpu(const vector*>& bottom, - vector*>* top); - virtual void Backward_cpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const bool propagate_down, vector*>* bottom); - - shared_ptr > softmax_layer_; - // prob stores the output probability of the layer. - Blob prob_; - // Vector holders to call the underlying softmax layer forward and backward. - vector*> softmax_bottom_vec_; - vector*> softmax_top_vec_; -}; - /* SplitLayer */ template -- 2.7.4