From 97deb9101b5a555b135ddc9826ab2925536be69a Mon Sep 17 00:00:00 2001 From: Kai Li Date: Thu, 3 Jul 2014 20:17:41 +0800 Subject: [PATCH] Organize the loss layers in alphabetical order --- include/caffe/loss_layers.hpp | 206 +++++++++++++++++++++--------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/include/caffe/loss_layers.hpp b/include/caffe/loss_layers.hpp index 3a4d416..fb7f553 100644 --- a/include/caffe/loss_layers.hpp +++ b/include/caffe/loss_layers.hpp @@ -22,6 +22,36 @@ namespace caffe { const float kLOG_THRESHOLD = 1e-20; +/* AccuracyLayer + Note: not an actual loss layer! Does not implement backwards step. + Computes the accuracy of argmax(a) with respect to b. +*/ +template +class AccuracyLayer : public Layer { + public: + explicit AccuracyLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_ACCURACY; + } + + virtual inline int ExactNumBottomBlobs() const { return 2; } + virtual inline int ExactNumTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom) { + NOT_IMPLEMENTED; + } + + int top_k_; +}; + /* LossLayer Takes two inputs of same num (a and b), and has no output. The gradient is propagated to a. @@ -45,67 +75,27 @@ class LossLayer : public Layer { } }; -// 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. +/* EuclideanLossLayer + Compute the L_2 distance between the two inputs. - In test, this layer could be replaced by simple softmax layer. + loss = (1/2 \sum_i (a_i - b_i)^2) + a' = 1/I (a - b) */ template -class SoftmaxWithLossLayer : public Layer { +class EuclideanLossLayer : public LossLayer { public: - explicit SoftmaxWithLossLayer(const LayerParameter& param) - : Layer(param), softmax_layer_(new SoftmaxLayer(param)) {} - virtual void SetUp(const vector*>& bottom, + explicit EuclideanLossLayer(const LayerParameter& param) + : LossLayer(param), diff_() {} + virtual void FurtherSetUp(const vector*>& bottom, vector*>* top); virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_SOFTMAX_LOSS; + return LayerParameter_LayerType_EUCLIDEAN_LOSS; } - virtual inline int MaxTopBlobs() const { return 2; } - // We cannot backpropagate to the labels; ignore force_backward for these - // inputs. + // Unlike most loss layers, in the EuclideanLossLayer we can backpropagate + // to both inputs. virtual inline bool AllowForceBackward(const int bottom_index) const { - return bottom_index != 1; - } - - 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 vector& propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& 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 -class SigmoidCrossEntropyLossLayer : public LossLayer { - public: - explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param) - : LossLayer(param), - sigmoid_layer_(new SigmoidLayer(param)), - sigmoid_output_(new Blob()) {} - virtual void FurtherSetUp(const vector*>& bottom, - vector*>* top); - - virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_SIGMOID_CROSS_ENTROPY_LOSS; + return true; } protected: @@ -118,48 +108,26 @@ class SigmoidCrossEntropyLossLayer : public LossLayer { virtual void Backward_gpu(const vector*>& top, const vector& propagate_down, vector*>* bottom); - shared_ptr > sigmoid_layer_; - // sigmoid_output stores the output of the sigmoid layer. - shared_ptr > sigmoid_output_; - // Vector holders to call the underlying sigmoid layer forward and backward. - vector*> sigmoid_bottom_vec_; - vector*> sigmoid_top_vec_; + Blob diff_; }; -/* EuclideanLossLayer - Compute the L_2 distance between the two inputs. - - loss = (1/2 \sum_i (a_i - b_i)^2) - a' = 1/I (a - b) +/* HingeLossLayer */ template -class EuclideanLossLayer : public LossLayer { +class HingeLossLayer : public LossLayer { public: - explicit EuclideanLossLayer(const LayerParameter& param) - : LossLayer(param), diff_() {} - virtual void FurtherSetUp(const vector*>& bottom, - vector*>* top); + explicit HingeLossLayer(const LayerParameter& param) + : LossLayer(param) {} virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_EUCLIDEAN_LOSS; - } - // Unlike most loss layers, in the EuclideanLossLayer we can backpropagate - // to both inputs. - virtual inline bool AllowForceBackward(const int bottom_index) const { - return true; + return LayerParameter_LayerType_HINGE_LOSS; } 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 vector& propagate_down, vector*>* bottom); - virtual void Backward_gpu(const vector*>& top, - const vector& propagate_down, vector*>* bottom); - - Blob diff_; }; /* InfogainLossLayer @@ -185,16 +153,18 @@ class InfogainLossLayer : public LossLayer { Blob infogain_; }; -/* HingeLossLayer +/* MultinomialLogisticLossLayer */ template -class HingeLossLayer : public LossLayer { +class MultinomialLogisticLossLayer : public LossLayer { public: - explicit HingeLossLayer(const LayerParameter& param) + explicit MultinomialLogisticLossLayer(const LayerParameter& param) : LossLayer(param) {} + virtual void FurtherSetUp(const vector*>& bottom, + vector*>* top); virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_HINGE_LOSS; + return LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS; } protected: @@ -204,55 +174,85 @@ class HingeLossLayer : public LossLayer { const vector& propagate_down, vector*>* bottom); }; -/* MultinomialLogisticLossLayer +/* SigmoidCrossEntropyLossLayer */ template -class MultinomialLogisticLossLayer : public LossLayer { +class SigmoidCrossEntropyLossLayer : public LossLayer { public: - explicit MultinomialLogisticLossLayer(const LayerParameter& param) - : LossLayer(param) {} + explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param) + : LossLayer(param), + sigmoid_layer_(new SigmoidLayer(param)), + sigmoid_output_(new Blob()) {} virtual void FurtherSetUp(const vector*>& bottom, vector*>* top); virtual inline LayerParameter_LayerType type() const { - return LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS; + return LayerParameter_LayerType_SIGMOID_CROSS_ENTROPY_LOSS; } 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 vector& propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); + + shared_ptr > sigmoid_layer_; + // sigmoid_output stores the output of the sigmoid layer. + shared_ptr > sigmoid_output_; + // Vector holders to call the underlying sigmoid layer forward and backward. + vector*> sigmoid_bottom_vec_; + vector*> sigmoid_top_vec_; }; -/* AccuracyLayer - Note: not an actual loss layer! Does not implement backwards step. - Computes the accuracy of argmax(a) with respect to b. +// 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 AccuracyLayer : public Layer { +class SoftmaxWithLossLayer : public Layer { public: - explicit AccuracyLayer(const LayerParameter& param) - : Layer(param) {} + 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_ACCURACY; + return LayerParameter_LayerType_SOFTMAX_LOSS; + } + virtual inline int MaxTopBlobs() const { return 2; } + // We cannot backpropagate to the labels; ignore force_backward for these + // inputs. + virtual inline bool AllowForceBackward(const int bottom_index) const { + return bottom_index != 1; } - - virtual inline int ExactNumBottomBlobs() const { return 2; } - virtual inline int ExactNumTopBlobs() const { return 1; } 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 vector& propagate_down, vector*>* bottom) { - NOT_IMPLEMENTED; - } + const vector& propagate_down, vector*>* bottom); + virtual void Backward_gpu(const vector*>& top, + const vector& propagate_down, vector*>* bottom); - int top_k_; + 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_; }; } // namespace caffe -- 2.7.4