-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add rank loss operator #4098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add rank loss operator #4098
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
96500af
add rank_loss operator
7c423e4
add unit test for rank_loss_op
87de31b
update doc information
36f349e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
f2442be
merge conflicts
26b393f
Merge branch 'develop' of upstream into rank_loss_op_dev
f2cfa32
Merge branch 'develop' of upstream into rank_loss_op_dev
ece3291
refine rank_loss_op
cf4b2db
change the dims of input of rank_loss_op
1f6b909
fix a typo in rank_loss_op
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #include "paddle/operators/rank_loss_op.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| class RankLossOp : public framework::OperatorWithKernel { | ||
| public: | ||
| RankLossOp(const std::string &type, const framework::VariableNameMap &inputs, | ||
| const framework::VariableNameMap &outputs, | ||
| const framework::AttributeMap &attrs) | ||
| : OperatorWithKernel(type, inputs, outputs, attrs) {} | ||
|
|
||
| protected: | ||
| void InferShape(const framework::InferShapeContext &ctx) const override { | ||
| // input check | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Label"), | ||
| "Input(Label) shouldn't be null"); | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Left"), | ||
| "Input(Left) shouldn't be null"); | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Right"), | ||
| "Input(Right) shouldn't be null"); | ||
| auto label_dims = ctx.Input<framework::Tensor>("Label")->dims(); | ||
| auto left_dims = ctx.Input<framework::Tensor>("Left")->dims(); | ||
| auto right_dims = ctx.Input<framework::Tensor>("Right")->dims(); | ||
| PADDLE_ENFORCE((label_dims == left_dims) && (left_dims == right_dims), | ||
| "All inputs must have the same size"); | ||
| PADDLE_ENFORCE((label_dims.size() == 2) && (label_dims[1] == 1), | ||
| "All inputs must be row vector with size batch_size x 1."); | ||
| ctx.Output<framework::LoDTensor>("Out")->Resize(label_dims); | ||
| } | ||
| }; | ||
|
|
||
| class RankLossOpMaker : public framework::OpProtoAndCheckerMaker { | ||
| public: | ||
| RankLossOpMaker(framework::OpProto *proto, | ||
| framework::OpAttrChecker *op_checker) | ||
| : OpProtoAndCheckerMaker(proto, op_checker) { | ||
| AddInput("Label", | ||
| "The label indicating A ranked higher than B or not, row vector."); | ||
| AddInput("Left", "The output of RankNet for doc A, vector."); | ||
| AddInput("Right", "The output of RankNet for doc B, vetor"); | ||
| AddOutput("Out", "The output loss of RankLoss operator, vector."); | ||
| AddComment(R"DOC(RankLoss operator | ||
|
|
||
| Rank loss operator for RankNet[1]. RankNet is a pairwise ranking model with | ||
| one training sample consisting of a pair of doc A and B, and the label P | ||
| indicating that A is ranked higher than B or not: | ||
|
|
||
| P = {0, 1} or {0, 0.5, 1}, where 0.5 means no information about the rank of | ||
| the input pair. | ||
|
|
||
| The RankLoss operator contains three inputs: Left (o_i), Right (o_j) and Label | ||
| (P_{i,j}), which represent the output of RankNet for two docs and the label | ||
| respectively, and yields the rank loss C_{i,j} by following the expression | ||
|
|
||
| \f[ | ||
| C_{i,j} = -\tilde{P_{ij}} * o_{i,j} + log(1 + e^{o_{i,j}}) \\ | ||
| o_{i,j} = o_i - o_j \\ | ||
| \tilde{P_{i,j}} = \left \{0, 0.5, 1 \right \} \ or \ \left \{0, 1 \right \} | ||
| \f] | ||
|
|
||
| The operator can take inputs of one sample or in batch. | ||
|
|
||
| [1]. Chris Burges, Tal Shaked, Erin Renshaw, et al. Learning to | ||
| Rank using Gradient Descent. | ||
| http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf | ||
| )DOC"); | ||
| } | ||
| }; | ||
|
|
||
| class RankLossGradOp : public framework::OperatorWithKernel { | ||
| public: | ||
| RankLossGradOp(const std::string &type, | ||
| const framework::VariableNameMap &inputs, | ||
| const framework::VariableNameMap &outputs, | ||
| const framework::AttributeMap &attrs) | ||
| : OperatorWithKernel(type, inputs, outputs, attrs) {} | ||
|
|
||
| protected: | ||
| void InferShape(const framework::InferShapeContext &ctx) const override { | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Label"), | ||
| "Input(Label) shouldn't be null."); | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Left"), | ||
| "Input(Left) shouldn't be null."); | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Right"), | ||
| "Input(Right) shouldn't be null."); | ||
| PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")), | ||
| "Input(Out@GRAD) shouldn't be null."); | ||
| auto dims = ctx.Input<framework::Tensor>("Left")->dims(); | ||
| auto *left_grad = | ||
| ctx.Output<framework::LoDTensor>(framework::GradVarName("Left")); | ||
| auto *right_grad = | ||
| ctx.Output<framework::LoDTensor>(framework::GradVarName("Right")); | ||
| if (left_grad) { | ||
| left_grad->Resize(dims); | ||
| } | ||
| if (right_grad) { | ||
| right_grad->Resize(dims); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
| namespace ops = paddle::operators; | ||
|
|
||
| REGISTER_OP(rank_loss, ops::RankLossOp, ops::RankLossOpMaker, rank_loss_grad, | ||
| ops::RankLossGradOp); | ||
| REGISTER_OP_CPU_KERNEL(rank_loss, | ||
| ops::RankLossKernel<paddle::platform::CPUPlace, float>); | ||
| REGISTER_OP_CPU_KERNEL( | ||
| rank_loss_grad, ops::RankLossGradKernel<paddle::platform::CPUPlace, float>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #include "paddle/operators/rank_loss_op.h" | ||
|
|
||
| REGISTER_OP_GPU_KERNEL( | ||
| rank_loss, | ||
| paddle::operators::RankLossKernel<paddle::platform::GPUPlace, float>); | ||
| REGISTER_OP_GPU_KERNEL( | ||
| rank_loss_grad, | ||
| paddle::operators::RankLossGradKernel<paddle::platform::GPUPlace, float>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "paddle/framework/eigen.h" | ||
| #include "paddle/framework/op_registry.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| template <typename Place, typename T> | ||
| class RankLossKernel : public framework::OpKernel { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const { | ||
| auto* out_t = ctx.Output<framework::LoDTensor>("Out"); | ||
| auto* label_t = ctx.Input<framework::Tensor>("Label"); | ||
| auto* left_t = ctx.Input<framework::Tensor>("Left"); | ||
| auto* right_t = ctx.Input<framework::Tensor>("Right"); | ||
| out_t->mutable_data<T>(ctx.GetPlace()); | ||
|
|
||
| auto out = framework::EigenVector<T>::Flatten(*out_t); | ||
| auto label = framework::EigenVector<T>::Flatten(*label_t); | ||
| auto left = framework::EigenVector<T>::Flatten(*left_t); | ||
| auto right = framework::EigenVector<T>::Flatten(*right_t); | ||
|
|
||
| auto& dev = ctx.GetEigenDevice<Place>(); | ||
| out.device(dev) = | ||
| (1. + (left - right).exp()).log() - label * (left - right); | ||
| } | ||
| }; | ||
|
|
||
| template <typename Place, typename T> | ||
| class RankLossGradKernel : public framework::OpKernel { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& ctx) const { | ||
| auto* d_left_t = | ||
| ctx.Output<framework::LoDTensor>(framework::GradVarName("Left")); | ||
| auto* d_right_t = | ||
| ctx.Output<framework::LoDTensor>(framework::GradVarName("Right")); | ||
|
|
||
| auto* d_out_t = ctx.Input<framework::Tensor>(framework::GradVarName("Out")); | ||
| auto* label_t = ctx.Input<framework::Tensor>("Label"); | ||
| auto* left_t = ctx.Input<framework::Tensor>("Left"); | ||
| auto* right_t = ctx.Input<framework::Tensor>("Right"); | ||
|
|
||
| auto& dev = ctx.GetEigenDevice<Place>(); | ||
| auto d_out = framework::EigenVector<T>::Flatten(*d_out_t); | ||
| auto label = framework::EigenVector<T>::Flatten(*label_t); | ||
| auto left = framework::EigenVector<T>::Flatten(*left_t); | ||
| auto right = framework::EigenVector<T>::Flatten(*right_t); | ||
|
|
||
| // compute d_left | ||
| if (d_left_t) { | ||
| d_left_t->mutable_data<T>(ctx.GetPlace()); | ||
| auto d_left = framework::EigenVector<T>::Flatten(*d_left_t); | ||
| d_left.device(dev) = d_out * (1. / (1. + (right - left).exp()) - label); | ||
| } | ||
| // compute d_right | ||
| if (d_right_t) { | ||
| d_right_t->mutable_data<T>(ctx.GetPlace()); | ||
| auto d_right = framework::EigenVector<T>::Flatten(*d_right_t); | ||
| d_right.device(dev) = | ||
| -d_out * (1.0 / (1. + (right - left).exp()) - label); | ||
| } | ||
| } | ||
| }; | ||
| } // namespace operators | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import unittest | ||
| import numpy as np | ||
| from op_test import OpTest | ||
|
|
||
|
|
||
| class TestRankLossOp(OpTest): | ||
| def setUp(self): | ||
| self.op_type = "rank_loss" | ||
| batch_size = 5 | ||
| # labels_{i} = {0, 1.0} or {0, 0.5, 1.0} | ||
| label = np.random.randint(0, 2, size=(batch_size, 1)).astype("float32") | ||
| left = np.random.random((batch_size, 1)).astype("float32") | ||
| right = np.random.random((batch_size, 1)).astype("float32") | ||
| loss = np.log(1.0 + np.exp(left - right)) - label * (left - right) | ||
| self.inputs = {'Label': label, 'Left': left, 'Right': right} | ||
| self.outputs = {'Out': loss} | ||
|
|
||
| def test_check_output(self): | ||
| self.check_output() | ||
|
|
||
| def test_check_grad(self): | ||
| self.check_grad(["Left", "Right"], "Out") | ||
|
|
||
| def test_check_grad_ignore_left(self): | ||
| self.check_grad(["Right"], "Out", no_grad_set=set('Left')) | ||
|
|
||
| def test_check_grad_ignore_right(self): | ||
| self.check_grad(["Left"], "Out", no_grad_set=set('Right')) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some
check_grad_ignore_XXXtests if posiible.In
check_grad_ignore_XXXtests, ignored variables' gradients will be setnullptrand your kernel should not compute it.Example: https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/framework/tests/test_mul_op.py#L21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done