Caffe2 - C++ API
A deep learning, cross platform ML framework
margin_ranking_criterion_op.cc
1 #include "caffe2/operators/margin_ranking_criterion_op.h"
2 
3 #include <algorithm>
4 
5 #include "caffe2/utils/math.h"
6 
7 namespace caffe2 {
8 
9 template <>
10 bool MarginRankingCriterionOp<CPUContext>::RunOnDevice() {
11  auto& X1 = Input(0);
12  auto& X2 = Input(1);
13  auto& Y = Input(2);
14 
15  CAFFE_ENFORCE_EQ(
16  X1.numel(),
17  X2.numel(),
18  "The two inputs for computing ranking loss should have the same size.");
19  CAFFE_ENFORCE_EQ(
20  X1.numel(), Y.numel(), "The input and label should have the same size.");
21  auto* loss = Output(0, X1.sizes(), at::dtype<float>());
22 
23  const float* X1data = X1.data<float>();
24  const float* X2data = X2.data<float>();
25  const int* Ydata = Y.data<int>();
26  float* output = loss->template mutable_data<float>();
27  for (int i = 0; i < X1.numel(); ++i) {
28  output[i] = std::max(-Ydata[i] * (X1data[i] - X2data[i]) + margin_, 0.f);
29  }
30  return true;
31 }
32 
33 template <>
34 bool MarginRankingCriterionGradientOp<CPUContext>::RunOnDevice() {
35  auto& X1 = Input(0);
36  auto& X2 = Input(1);
37  auto& Y = Input(2);
38  auto& dLoss = Input(3);
39 
40  auto* dX1 = Output(0, X1.sizes(), at::dtype<float>());
41  auto* dX2 = Output(1, X2.sizes(), at::dtype<float>());
42 
43  const float* X1data = X1.data<float>();
44  const float* X2data = X2.data<float>();
45  const int* Ydata = Y.data<int>();
46  const float* dLoss_data = dLoss.data<float>();
47 
48  float* dX1_data = dX1->template mutable_data<float>();
49  float* dX2_data = dX2->template mutable_data<float>();
50  for (int i = 0; i < X1.numel(); ++i) {
51  auto dist = -Ydata[i] * (X1data[i] - X2data[i]) + margin_;
52  if (dist < 0.f) {
53  dX1_data[i] = dX2_data[i] = 0.f;
54  } else {
55  dX1_data[i] = -Ydata[i] * dLoss_data[i];
56  dX2_data[i] = Ydata[i] * dLoss_data[i];
57  }
58  }
59  return true;
60 }
61 
62 REGISTER_CPU_OPERATOR(
63  MarginRankingCriterion,
64  MarginRankingCriterionOp<CPUContext>);
65 REGISTER_CPU_OPERATOR(
66  MarginRankingCriterionGradient,
67  MarginRankingCriterionGradientOp<CPUContext>);
68 
69 OPERATOR_SCHEMA(MarginRankingCriterion)
70  .NumInputs(3)
71  .NumOutputs(1)
72  .SetDoc(R"DOC(
73 MarginRankingCriterion takes two input data X1 (Tensor),
74 X2 (Tensor), and label Y (Tensor) to produce the
75 loss (Tensor) where the loss function,
76 loss(X1, X2, Y) = max(0, -Y * (X1 - X2) + margin), is applied to
77 the tensor elementwise.
78 
79 If y == 1 then it assumed the first input should be ranked higher
80 (have a larger value) than the second input, and vice-versa for
81 y == -1.
82 )DOC")
83  .Arg("margin", "The margin value as a float. Default is 1.0.")
84  .Input(0, "X1", "The left input vector as a 1-dim TensorCPU.")
85  .Input(1, "X2", "The right input vector as a 1-dim TensorCPU.")
86  .Input(2, "Y", "The label as a 1-dim TensorCPU with int value of 1 or -1.")
87  .Output(0, "loss", "The output loss with the same dimensionality as X1.");
88 
89 OPERATOR_SCHEMA(MarginRankingCriterionGradient)
90  .NumInputs(4)
91  .NumOutputs(2)
92  .SetDoc(R"DOC(
93 MarginRankingCriterionGradient takes both X1, X2, Y and dY and
94 uses them to update dX1, and dX2 according to the chain rule
95 and derivatives of the loss function.
96 )DOC");
97 
98 class GetMarginRankingCriterionGradient : public GradientMakerBase {
99  using GradientMakerBase::GradientMakerBase;
100  vector<OperatorDef> GetGradientDefs() override {
101  return SingleGradientDef(
102  "MarginRankingCriterionGradient",
103  "",
104  vector<string>{I(0), I(1), I(2), GO(0)},
105  vector<string>{GI(0), GI(1)});
106  }
107 };
108 REGISTER_GRADIENT(MarginRankingCriterion, GetMarginRankingCriterionGradient);
109 
110 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13