|
| 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>); |
0 commit comments