commit
d827359c71
@ -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>);
|
@ -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>);
|
@ -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
|
@ -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()
|
Loading…
Reference in new issue