Skip to content

Commit d827359

Browse files
authored
Merge pull request #4098 from kuke/rank_loss_op_dev
Add rank loss operator
2 parents da2aabb + 1f6b909 commit d827359

4 files changed

Lines changed: 260 additions & 0 deletions

File tree

paddle/operators/rank_loss_op.cc

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/operators/rank_loss_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class RankLossOp : public framework::OperatorWithKernel {
21+
public:
22+
RankLossOp(const std::string &type, const framework::VariableNameMap &inputs,
23+
const framework::VariableNameMap &outputs,
24+
const framework::AttributeMap &attrs)
25+
: OperatorWithKernel(type, inputs, outputs, attrs) {}
26+
27+
protected:
28+
void InferShape(const framework::InferShapeContext &ctx) const override {
29+
// input check
30+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Label"),
31+
"Input(Label) shouldn't be null");
32+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Left"),
33+
"Input(Left) shouldn't be null");
34+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Right"),
35+
"Input(Right) shouldn't be null");
36+
auto label_dims = ctx.Input<framework::Tensor>("Label")->dims();
37+
auto left_dims = ctx.Input<framework::Tensor>("Left")->dims();
38+
auto right_dims = ctx.Input<framework::Tensor>("Right")->dims();
39+
PADDLE_ENFORCE((label_dims == left_dims) && (left_dims == right_dims),
40+
"All inputs must have the same size");
41+
PADDLE_ENFORCE((label_dims.size() == 2) && (label_dims[1] == 1),
42+
"All inputs must be row vector with size batch_size x 1.");
43+
ctx.Output<framework::LoDTensor>("Out")->Resize(label_dims);
44+
}
45+
};
46+
47+
class RankLossOpMaker : public framework::OpProtoAndCheckerMaker {
48+
public:
49+
RankLossOpMaker(framework::OpProto *proto,
50+
framework::OpAttrChecker *op_checker)
51+
: OpProtoAndCheckerMaker(proto, op_checker) {
52+
AddInput("Label",
53+
"The label indicating A ranked higher than B or not, row vector.");
54+
AddInput("Left", "The output of RankNet for doc A, vector.");
55+
AddInput("Right", "The output of RankNet for doc B, vetor");
56+
AddOutput("Out", "The output loss of RankLoss operator, vector.");
57+
AddComment(R"DOC(RankLoss operator
58+
59+
Rank loss operator for RankNet[1]. RankNet is a pairwise ranking model with
60+
one training sample consisting of a pair of doc A and B, and the label P
61+
indicating that A is ranked higher than B or not:
62+
63+
P = {0, 1} or {0, 0.5, 1}, where 0.5 means no information about the rank of
64+
the input pair.
65+
66+
The RankLoss operator contains three inputs: Left (o_i), Right (o_j) and Label
67+
(P_{i,j}), which represent the output of RankNet for two docs and the label
68+
respectively, and yields the rank loss C_{i,j} by following the expression
69+
70+
\f[
71+
C_{i,j} = -\tilde{P_{ij}} * o_{i,j} + log(1 + e^{o_{i,j}}) \\
72+
o_{i,j} = o_i - o_j \\
73+
\tilde{P_{i,j}} = \left \{0, 0.5, 1 \right \} \ or \ \left \{0, 1 \right \}
74+
\f]
75+
76+
The operator can take inputs of one sample or in batch.
77+
78+
[1]. Chris Burges, Tal Shaked, Erin Renshaw, et al. Learning to
79+
Rank using Gradient Descent.
80+
http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf
81+
)DOC");
82+
}
83+
};
84+
85+
class RankLossGradOp : public framework::OperatorWithKernel {
86+
public:
87+
RankLossGradOp(const std::string &type,
88+
const framework::VariableNameMap &inputs,
89+
const framework::VariableNameMap &outputs,
90+
const framework::AttributeMap &attrs)
91+
: OperatorWithKernel(type, inputs, outputs, attrs) {}
92+
93+
protected:
94+
void InferShape(const framework::InferShapeContext &ctx) const override {
95+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Label"),
96+
"Input(Label) shouldn't be null.");
97+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Left"),
98+
"Input(Left) shouldn't be null.");
99+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Right"),
100+
"Input(Right) shouldn't be null.");
101+
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
102+
"Input(Out@GRAD) shouldn't be null.");
103+
auto dims = ctx.Input<framework::Tensor>("Left")->dims();
104+
auto *left_grad =
105+
ctx.Output<framework::LoDTensor>(framework::GradVarName("Left"));
106+
auto *right_grad =
107+
ctx.Output<framework::LoDTensor>(framework::GradVarName("Right"));
108+
if (left_grad) {
109+
left_grad->Resize(dims);
110+
}
111+
if (right_grad) {
112+
right_grad->Resize(dims);
113+
}
114+
}
115+
};
116+
117+
} // namespace operators
118+
} // namespace paddle
119+
namespace ops = paddle::operators;
120+
121+
REGISTER_OP(rank_loss, ops::RankLossOp, ops::RankLossOpMaker, rank_loss_grad,
122+
ops::RankLossGradOp);
123+
REGISTER_OP_CPU_KERNEL(rank_loss,
124+
ops::RankLossKernel<paddle::platform::CPUPlace, float>);
125+
REGISTER_OP_CPU_KERNEL(
126+
rank_loss_grad, ops::RankLossGradKernel<paddle::platform::CPUPlace, float>);

paddle/operators/rank_loss_op.cu

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/operators/rank_loss_op.h"
16+
17+
REGISTER_OP_GPU_KERNEL(
18+
rank_loss,
19+
paddle::operators::RankLossKernel<paddle::platform::GPUPlace, float>);
20+
REGISTER_OP_GPU_KERNEL(
21+
rank_loss_grad,
22+
paddle::operators::RankLossGradKernel<paddle::platform::GPUPlace, float>);

paddle/operators/rank_loss_op.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include "paddle/framework/eigen.h"
18+
#include "paddle/framework/op_registry.h"
19+
20+
namespace paddle {
21+
namespace operators {
22+
23+
template <typename Place, typename T>
24+
class RankLossKernel : public framework::OpKernel {
25+
public:
26+
void Compute(const framework::ExecutionContext& ctx) const {
27+
auto* out_t = ctx.Output<framework::LoDTensor>("Out");
28+
auto* label_t = ctx.Input<framework::Tensor>("Label");
29+
auto* left_t = ctx.Input<framework::Tensor>("Left");
30+
auto* right_t = ctx.Input<framework::Tensor>("Right");
31+
out_t->mutable_data<T>(ctx.GetPlace());
32+
33+
auto out = framework::EigenVector<T>::Flatten(*out_t);
34+
auto label = framework::EigenVector<T>::Flatten(*label_t);
35+
auto left = framework::EigenVector<T>::Flatten(*left_t);
36+
auto right = framework::EigenVector<T>::Flatten(*right_t);
37+
38+
auto& dev = ctx.GetEigenDevice<Place>();
39+
out.device(dev) =
40+
(1. + (left - right).exp()).log() - label * (left - right);
41+
}
42+
};
43+
44+
template <typename Place, typename T>
45+
class RankLossGradKernel : public framework::OpKernel {
46+
public:
47+
void Compute(const framework::ExecutionContext& ctx) const {
48+
auto* d_left_t =
49+
ctx.Output<framework::LoDTensor>(framework::GradVarName("Left"));
50+
auto* d_right_t =
51+
ctx.Output<framework::LoDTensor>(framework::GradVarName("Right"));
52+
53+
auto* d_out_t = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
54+
auto* label_t = ctx.Input<framework::Tensor>("Label");
55+
auto* left_t = ctx.Input<framework::Tensor>("Left");
56+
auto* right_t = ctx.Input<framework::Tensor>("Right");
57+
58+
auto& dev = ctx.GetEigenDevice<Place>();
59+
auto d_out = framework::EigenVector<T>::Flatten(*d_out_t);
60+
auto label = framework::EigenVector<T>::Flatten(*label_t);
61+
auto left = framework::EigenVector<T>::Flatten(*left_t);
62+
auto right = framework::EigenVector<T>::Flatten(*right_t);
63+
64+
// compute d_left
65+
if (d_left_t) {
66+
d_left_t->mutable_data<T>(ctx.GetPlace());
67+
auto d_left = framework::EigenVector<T>::Flatten(*d_left_t);
68+
d_left.device(dev) = d_out * (1. / (1. + (right - left).exp()) - label);
69+
}
70+
// compute d_right
71+
if (d_right_t) {
72+
d_right_t->mutable_data<T>(ctx.GetPlace());
73+
auto d_right = framework::EigenVector<T>::Flatten(*d_right_t);
74+
d_right.device(dev) =
75+
-d_out * (1.0 / (1. + (right - left).exp()) - label);
76+
}
77+
}
78+
};
79+
} // namespace operators
80+
} // namespace paddle
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
import numpy as np
3+
from op_test import OpTest
4+
5+
6+
class TestRankLossOp(OpTest):
7+
def setUp(self):
8+
self.op_type = "rank_loss"
9+
batch_size = 5
10+
# labels_{i} = {0, 1.0} or {0, 0.5, 1.0}
11+
label = np.random.randint(0, 2, size=(batch_size, 1)).astype("float32")
12+
left = np.random.random((batch_size, 1)).astype("float32")
13+
right = np.random.random((batch_size, 1)).astype("float32")
14+
loss = np.log(1.0 + np.exp(left - right)) - label * (left - right)
15+
self.inputs = {'Label': label, 'Left': left, 'Right': right}
16+
self.outputs = {'Out': loss}
17+
18+
def test_check_output(self):
19+
self.check_output()
20+
21+
def test_check_grad(self):
22+
self.check_grad(["Left", "Right"], "Out")
23+
24+
def test_check_grad_ignore_left(self):
25+
self.check_grad(["Right"], "Out", no_grad_set=set('Left'))
26+
27+
def test_check_grad_ignore_right(self):
28+
self.check_grad(["Left"], "Out", no_grad_set=set('Right'))
29+
30+
31+
if __name__ == '__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)