Now Loss layers would return the loss in the top blob if requested
authorSergio <sguada@gmail.com>
Thu, 19 Jun 2014 23:40:32 +0000 (16:40 -0700)
committerSergio <sguada@gmail.com>
Thu, 19 Jun 2014 23:40:32 +0000 (16:40 -0700)
include/caffe/loss_layers.hpp
src/caffe/layers/euclidean_loss_layer.cpp
src/caffe/layers/infogain_loss_layer.cpp
src/caffe/layers/loss_layer.cpp
src/caffe/layers/multinomial_logistic_loss_layer.cpp
src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp
src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu
src/caffe/layers/softmax_loss_layer.cpp

index 381bf0f..dd06ca0 100644 (file)
@@ -37,7 +37,7 @@ class LossLayer : public Layer<Dtype> {
       const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {}
 
   virtual inline int ExactNumBottomBlobs() const { return 2; }
-  virtual inline int ExactNumTopBlobs() const { return 0; }
+  virtual inline int MaxTopBlobs() const { return 1; }
 };
 
 /* SigmoidCrossEntropyLossLayer
@@ -166,7 +166,7 @@ class MultinomialLogisticLossLayer : public LossLayer<Dtype> {
 
 /* AccuracyLayer
   Note: not an actual loss layer! Does not implement backwards step.
-  Computes the accuracy and logprob of a with respect to b.
+  Computes the accuracy of a with respect to b.
 */
 template <typename Dtype>
 class AccuracyLayer : public Layer<Dtype> {
index a894d47..7662949 100644 (file)
@@ -35,6 +35,9 @@ Dtype EuclideanLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
       diff_.mutable_cpu_data());
   Dtype dot = caffe_cpu_dot(count, diff_.cpu_data(), diff_.cpu_data());
   Dtype loss = dot / bottom[0]->num() / Dtype(2);
+  if (top->size() == 1) {
+    (*top)[0]->mutable_cpu_data()[0] = loss;
+  }
   return loss;
 }
 
index ab6e67d..3e7fc4f 100644 (file)
@@ -48,6 +48,9 @@ Dtype InfogainLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
       loss -= infogain_mat[label * dim + j] * log(prob);
     }
   }
+  if (top->size() == 1) {
+    (*top)[0]->mutable_cpu_data()[0] = loss / num;
+  }
   return loss / num;
 }
 
index 14ea975..ac8ad21 100644 (file)
@@ -20,6 +20,10 @@ void LossLayer<Dtype>::SetUp(
   Layer<Dtype>::SetUp(bottom, top);
   CHECK_EQ(bottom[0]->num(), bottom[1]->num())
       << "The data and label should have the same number.";
+  if (top->size() == 1) {
+  // Layers should copy the loss in the top blob
+    (*top)[0]->Reshape(1, 1, 1, 1);
+  }
   FurtherSetUp(bottom, top);
 }
 
index 6486621..5a40879 100644 (file)
@@ -35,6 +35,9 @@ Dtype MultinomialLogisticLossLayer<Dtype>::Forward_cpu(
     Dtype prob = max(bottom_data[i * dim + label], Dtype(kLOG_THRESHOLD));
     loss -= log(prob);
   }
+  if (top->size() == 1){
+    (*top)[0]->mutable_cpu_data()[0] = loss / num;
+  }
   return loss / num;
 }
 
index a638684..955581d 100644 (file)
@@ -41,6 +41,9 @@ Dtype SigmoidCrossEntropyLossLayer<Dtype>::Forward_cpu(
     loss -= input_data[i] * (target[i] - (input_data[i] >= 0)) -
         log(1 + exp(input_data[i] - 2 * input_data[i] * (input_data[i] >= 0)));
   }
+  if (top->size() == 1) {
+    (*top)[0]->mutable_cpu_data()[0] = loss / num;
+  }
   return loss / num;
 }
 
index 6100454..0caed2b 100644 (file)
@@ -29,6 +29,9 @@ Dtype SigmoidCrossEntropyLossLayer<Dtype>::Forward_gpu(
     loss -= input_data[i] * (target[i] - (input_data[i] >= 0)) -
         log(1 + exp(input_data[i] - 2 * input_data[i] * (input_data[i] >= 0)));
   }
+  if (top->size() == 1) {
+    (*top)[0]->mutable_cpu_data()[0] = loss / num;
+  }
   return loss / num;
 }
 
index ef6eeba..8a37efe 100644 (file)
@@ -37,6 +37,9 @@ Dtype SoftmaxWithLossLayer<Dtype>::Forward_cpu(
     loss += -log(max(prob_data[i * dim + static_cast<int>(label[i])],
                      Dtype(FLT_MIN)));
   }
+  if (top->size() == 1) {
+    (*top)[0]->mutable_cpu_data()[0] = loss / num;
+  }
   return loss / num;
 }