Skip to content

Commit

Permalink
Lil’ more docstring, and cosmetic change in EuclideanLossLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyk committed May 19, 2014
1 parent d01c6ed commit efeaf17
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
37 changes: 28 additions & 9 deletions include/caffe/loss_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ namespace caffe {

const float kLOG_THRESHOLD = 1e-20;

// LossLayer takes two inputs of same num, and has no output.
/* LossLayer
Takes two inputs of same num (a and b), and has no output.
The gradient is propagated to a.
*/
template <typename Dtype>
class LossLayer : public Layer<Dtype> {
public:
Expand All @@ -34,7 +37,8 @@ class LossLayer : public Layer<Dtype> {
const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {}
};

// SigmoidCrossEntropyLossLayer
/* SigmoidCrossEntropyLossLayer
*/
template <typename Dtype>
class SigmoidCrossEntropyLossLayer : public LossLayer<Dtype> {
public:
Expand Down Expand Up @@ -63,12 +67,17 @@ class SigmoidCrossEntropyLossLayer : public LossLayer<Dtype> {
vector<Blob<Dtype>*> sigmoid_top_vec_;
};

// EuclideanLossLayer: compute y = 1/2 \sum_i (x_i - x'_i)^2
/* 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)
*/
template <typename Dtype>
class EuclideanLossLayer : public LossLayer<Dtype> {
public:
explicit EuclideanLossLayer(const LayerParameter& param)
: LossLayer<Dtype>(param), difference_() {}
: LossLayer<Dtype>(param), diff_() {}
virtual void FurtherSetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);

Expand All @@ -78,10 +87,11 @@ class EuclideanLossLayer : public LossLayer<Dtype> {
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);

Blob<Dtype> difference_;
Blob<Dtype> diff_;
};


/* InfogainLossLayer
*/
template <typename Dtype>
class InfogainLossLayer : public LossLayer<Dtype> {
public:
Expand All @@ -99,6 +109,8 @@ class InfogainLossLayer : public LossLayer<Dtype> {
Blob<Dtype> infogain_;
};

/* HingeLossLayer
*/
template <typename Dtype>
class HingeLossLayer : public LossLayer<Dtype> {
public:
Expand All @@ -112,6 +124,8 @@ class HingeLossLayer : public LossLayer<Dtype> {
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};

/* MultinomialLogisticLossLayer
*/
template <typename Dtype>
class MultinomialLogisticLossLayer : public LossLayer<Dtype> {
public:
Expand All @@ -127,8 +141,10 @@ class MultinomialLogisticLossLayer : public LossLayer<Dtype> {
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};

// AccuracyLayer: not an actual loss layer;
// computes the accuracy and logprob of x with respect to y'.
/* AccuracyLayer
Note: not an actual loss layer! Does not implement backwards step.
Computes the accuracy and logprob of a with respect to b.
*/
template <typename Dtype>
class AccuracyLayer : public Layer<Dtype> {
public:
Expand All @@ -140,13 +156,16 @@ class AccuracyLayer : public Layer<Dtype> {
protected:
virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
// The accuracy layer should not be used to compute backward operations.
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
NOT_IMPLEMENTED;
}
};

/* Also see
- SoftmaxWithLossLayer in vision_layers.hpp
*/

} // namespace caffe

#endif // CAFFE_LOSS_LAYERS_HPP_
36 changes: 30 additions & 6 deletions include/caffe/vision_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

namespace caffe {

/*
ConcatLayer
Takes at least two blobs and concatenates them along either num or
channel dim, outputting the result.
*/
template <typename Dtype>
class ConcatLayer : public Layer<Dtype> {
public:
Expand Down Expand Up @@ -44,6 +49,8 @@ class ConcatLayer : public Layer<Dtype> {
int concat_dim_;
};

/* ConvolutionLayer
*/
template <typename Dtype>
class ConvolutionLayer : public Layer<Dtype> {
public:
Expand Down Expand Up @@ -79,7 +86,8 @@ class ConvolutionLayer : public Layer<Dtype> {
int N_;
};


/* EltwiseProductLayer
*/
template <typename Dtype>
class EltwiseProductLayer : public Layer<Dtype> {
public:
Expand Down Expand Up @@ -120,6 +128,8 @@ class FlattenLayer : public Layer<Dtype> {
int count_;
};

/* Im2colLayer
*/
template <typename Dtype>
class Im2colLayer : public Layer<Dtype> {
public:
Expand All @@ -146,6 +156,8 @@ class Im2colLayer : public Layer<Dtype> {
int pad_;
};

/* InnerProductLayer
*/
template <typename Dtype>
class InnerProductLayer : public Layer<Dtype> {
public:
Expand Down Expand Up @@ -175,6 +187,9 @@ class InnerProductLayer : public Layer<Dtype> {
template <typename Dtype> class PoolingLayer;
template <typename Dtype> class SplitLayer;

/* LRNLayer
Local Response Normalization
*/
template <typename Dtype>
class LRNLayer : public Layer<Dtype> {
public:
Expand Down Expand Up @@ -238,6 +253,8 @@ class LRNLayer : public Layer<Dtype> {
vector<Blob<Dtype>*> product_bottom_vec_;
};

/* PoolingLayer
*/
template <typename Dtype>
class MemoryDataLayer : public Layer<Dtype> {
public:
Expand Down Expand Up @@ -301,6 +318,8 @@ class PoolingLayer : public Layer<Dtype> {
Blob<Dtype> rand_idx_;
};

/* SoftmaxLayer
*/
template <typename Dtype>
class SoftmaxLayer : public Layer<Dtype> {
public:
Expand All @@ -325,11 +344,14 @@ class SoftmaxLayer : public Layer<Dtype> {
Blob<Dtype> scale_;
};

// SoftmaxWithLossLayer is a layer that implements softmax and then computes
// the loss - it is preferred over softmax + multinomiallogisticloss in the
// sense that during training, this will produce more numerically stable
// gradients. During testing this layer could be replaced by a softmax layer
// to generate probability outputs.
/* 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 <typename Dtype>
class SoftmaxWithLossLayer : public Layer<Dtype> {
public:
Expand All @@ -356,6 +378,8 @@ class SoftmaxWithLossLayer : public Layer<Dtype> {
vector<Blob<Dtype>*> softmax_top_vec_;
};

/* SplitLayer
*/
template <typename Dtype>
class SplitLayer : public Layer<Dtype> {
public:
Expand Down
25 changes: 14 additions & 11 deletions src/caffe/layers/euclidean_loss_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@ void EuclideanLossLayer<Dtype>::FurtherSetUp(
CHECK_EQ(bottom[0]->channels(), bottom[1]->channels());
CHECK_EQ(bottom[0]->height(), bottom[1]->height());
CHECK_EQ(bottom[0]->width(), bottom[1]->width());
difference_.Reshape(bottom[0]->num(), bottom[0]->channels(),
diff_.Reshape(bottom[0]->num(), bottom[0]->channels(),
bottom[0]->height(), bottom[0]->width());
}

template <typename Dtype>
Dtype EuclideanLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
int count = bottom[0]->count();
int num = bottom[0]->num();
caffe_sub(count, bottom[0]->cpu_data(), bottom[1]->cpu_data(),
difference_.mutable_cpu_data());
Dtype loss = caffe_cpu_dot(
count, difference_.cpu_data(), difference_.cpu_data()) / num / Dtype(2);
caffe_sub(
count,
bottom[0]->cpu_data(),
bottom[1]->cpu_data(),
diff_.mutable_cpu_data());
Dtype dot = caffe_cpu_dot(count, diff_.cpu_data(), diff_.cpu_data());
Dtype loss = dot / bottom[0]->num() / Dtype(2);
return loss;
}

template <typename Dtype>
void EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
int count = (*bottom)[0]->count();
int num = (*bottom)[0]->num();
// Compute the gradient
caffe_cpu_axpby(count, Dtype(1) / num, difference_.cpu_data(), Dtype(0),
(*bottom)[0]->mutable_cpu_diff());
caffe_cpu_axpby(
(*bottom)[0]->count(), // count
Dtype(1) / (*bottom)[0]->num(), // alpha
diff_.cpu_data(), // a
Dtype(0), // beta
(*bottom)[0]->mutable_cpu_diff()); // b
}

INSTANTIATE_CLASS(EuclideanLossLayer);
Expand Down

0 comments on commit efeaf17

Please sign in to comment.