1 #include "caffe2/operators/margin_ranking_criterion_op.h" 5 #include "caffe2/utils/math.h" 10 bool MarginRankingCriterionOp<CPUContext>::RunOnDevice() {
18 "The two inputs for computing ranking loss should have the same size.");
20 X1.numel(), Y.numel(),
"The input and label should have the same size.");
21 auto* loss = Output(0, X1.sizes(), at::dtype<float>());
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);
34 bool MarginRankingCriterionGradientOp<CPUContext>::RunOnDevice() {
38 auto& dLoss = Input(3);
40 auto* dX1 = Output(0, X1.sizes(), at::dtype<float>());
41 auto* dX2 = Output(1, X2.sizes(), at::dtype<float>());
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>();
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_;
53 dX1_data[i] = dX2_data[i] = 0.f;
55 dX1_data[i] = -Ydata[i] * dLoss_data[i];
56 dX2_data[i] = Ydata[i] * dLoss_data[i];
62 REGISTER_CPU_OPERATOR(
63 MarginRankingCriterion,
64 MarginRankingCriterionOp<CPUContext>);
65 REGISTER_CPU_OPERATOR(
66 MarginRankingCriterionGradient,
67 MarginRankingCriterionGradientOp<CPUContext>);
69 OPERATOR_SCHEMA(MarginRankingCriterion)
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. 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 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.");
89 OPERATOR_SCHEMA(MarginRankingCriterionGradient)
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. 98 class GetMarginRankingCriterionGradient :
public GradientMakerBase {
99 using GradientMakerBase::GradientMakerBase;
100 vector<OperatorDef> GetGradientDefs()
override {
101 return SingleGradientDef(
102 "MarginRankingCriterionGradient",
104 vector<string>{I(0), I(1), I(2), GO(0)},
105 vector<string>{GI(0), GI(1)});
108 REGISTER_GRADIENT(MarginRankingCriterion, GetMarginRankingCriterionGradient);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...