Caffe
softmax_loss_layer.hpp
1 #ifndef CAFFE_SOFTMAX_WITH_LOSS_LAYER_HPP_
2 #define CAFFE_SOFTMAX_WITH_LOSS_LAYER_HPP_
3 
4 #include <vector>
5 
6 #include "caffe/blob.hpp"
7 #include "caffe/layer.hpp"
8 #include "caffe/proto/caffe.pb.h"
9 
10 #include "caffe/layers/loss_layer.hpp"
11 #include "caffe/layers/softmax_layer.hpp"
12 
13 namespace caffe {
14 
43 template <typename Dtype>
44 class SoftmaxWithLossLayer : public LossLayer<Dtype> {
45  public:
54  explicit SoftmaxWithLossLayer(const LayerParameter& param)
55  : LossLayer<Dtype>(param) {}
56  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
57  const vector<Blob<Dtype>*>& top);
58  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
59  const vector<Blob<Dtype>*>& top);
60 
61  virtual inline const char* type() const { return "SoftmaxWithLoss"; }
62  virtual inline int ExactNumTopBlobs() const { return -1; }
63  virtual inline int MinTopBlobs() const { return 1; }
64  virtual inline int MaxTopBlobs() const { return 2; }
65 
66  protected:
67  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
68  const vector<Blob<Dtype>*>& top);
69  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
70  const vector<Blob<Dtype>*>& top);
98  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
99  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
100  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
101  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
102 
107  virtual Dtype get_normalizer(
108  LossParameter_NormalizationMode normalization_mode, int valid_count);
109 
111  shared_ptr<Layer<Dtype> > softmax_layer_;
115  vector<Blob<Dtype>*> softmax_bottom_vec_;
117  vector<Blob<Dtype>*> softmax_top_vec_;
123  LossParameter_NormalizationMode normalization_;
124 
125  int softmax_axis_, outer_num_, inner_num_;
126 };
127 
128 } // namespace caffe
129 
130 #endif // CAFFE_SOFTMAX_WITH_LOSS_LAYER_HPP_
virtual void Backward_gpu(const vector< Blob< Dtype > *> &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > *> &bottom)
Using the GPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
virtual void Forward_cpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Using the CPU device, compute the layer output.
Definition: softmax_loss_layer.cpp:89
shared_ptr< Layer< Dtype > > softmax_layer_
The internal SoftmaxLayer used to map predictions to a distribution.
Definition: softmax_loss_layer.hpp:111
virtual int MinTopBlobs() const
Returns the minimum number of top blobs required by the layer, or -1 if no minimum number is required...
Definition: softmax_loss_layer.hpp:63
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
virtual void Reshape(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs...
Definition: softmax_loss_layer.cpp:39
virtual int MaxTopBlobs() const
Returns the maximum number of top blobs required by the layer, or -1 if no maximum number is required...
Definition: softmax_loss_layer.hpp:64
virtual const char * type() const
Returns the layer type.
Definition: softmax_loss_layer.hpp:61
SoftmaxWithLossLayer(const LayerParameter &param)
Definition: softmax_loss_layer.hpp:54
vector< Blob< Dtype > * > softmax_top_vec_
top vector holder used in call to the underlying SoftmaxLayer::Forward
Definition: softmax_loss_layer.hpp:117
LossParameter_NormalizationMode normalization_
How to normalize the output loss.
Definition: softmax_loss_layer.hpp:123
Computes the multinomial logistic loss for a one-of-many classification task, passing real-valued pre...
Definition: softmax_loss_layer.hpp:44
vector< Blob< Dtype > * > softmax_bottom_vec_
bottom vector holder used in call to the underlying SoftmaxLayer::Forward
Definition: softmax_loss_layer.hpp:115
int ignore_label_
The label indicating that an instance should be ignored.
Definition: softmax_loss_layer.hpp:121
An interface for Layers that take two Blobs as input – usually (1) predictions and (2) ground-truth ...
Definition: loss_layer.hpp:23
virtual void Backward_cpu(const vector< Blob< Dtype > *> &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > *> &bottom)
Computes the softmax loss error gradient w.r.t. the predictions.
Definition: softmax_loss_layer.cpp:118
virtual Dtype get_normalizer(LossParameter_NormalizationMode normalization_mode, int valid_count)
Definition: softmax_loss_layer.cpp:59
virtual int ExactNumTopBlobs() const
Returns the exact number of top blobs required by the layer, or -1 if no exact number is required...
Definition: softmax_loss_layer.hpp:62
Blob< Dtype > prob_
prob stores the output probability predictions from the SoftmaxLayer.
Definition: softmax_loss_layer.hpp:113
virtual void LayerSetUp(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Does layer-specific setup: your layer should implement this function as well as Reshape.
Definition: softmax_loss_layer.cpp:11
virtual void Forward_gpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Using the GPU device, compute the layer output. Fall back to Forward_cpu() if unavailable.
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24
bool has_ignore_label_
Whether to ignore instances with a certain label.
Definition: softmax_loss_layer.hpp:119