Skip to content

Conversation

kuke
Copy link
Contributor

@kuke kuke commented Sep 14, 2017

Resolve #4065

num = 5
# P = {0, 1.0} or {0, 0.5, 1.0}
P = np.random.randint(0, 2, size=(num, num)).astype("float32")
Oi = np.random.random((num, num)).astype("float32")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local variable name should be lower_with_under, https://google.github.io/styleguide/pyguide.html?showone=Naming#Naming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


A detailed explanation about these notations can be found in

[1]. Chris Burges, Tal Shaked, Erin Renshaw, et al. Learning to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add the link here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("P", "The desired target values for posteriors.");
AddInput("Oi", "The model output for item i.");
AddInput("Oj", "The model output for item j.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please illustrate dimensions of inputs and outputs in their comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auto dims = ctx.Input<framework::Tensor>("P")->dims();
ctx.Output<framework::Tensor>(framework::GradVarName("P"))->Resize(dims);
ctx.Output<framework::Tensor>(framework::GradVarName("Oi"))->Resize(dims);
ctx.Output<framework::Tensor>(framework::GradVarName("Oj"))->Resize(dims);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gradient Op's output(the gradient of forward op's inputs) can be nullptr, which means they are not necessary for backward. So we shall assert it is not nullptr before Resize.

See https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/cos_sim_op.cc#L146

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auto* oi_t = ctx.Input<framework::Tensor>("Oi");
auto* oj_t = ctx.Input<framework::Tensor>("Oj");

d_oi->mutable_data<T>(ctx.GetPlace());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outputs of gradient Op may be nullptr. If so, it means that they are useless for backward and we don't need to compute them.

see https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/cos_sim_op.h#L104 for an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

from op_test import OpTest


class TestReshapeOp(OpTest):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the class name is TestReshapeOp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A typo. Fixed

def test_check_output(self):
self.check_output()

def test_check_grad(self):
Copy link
Collaborator

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_XXX tests if posiible.
In check_grad_ignore_XXX tests, ignored variables' gradients will be set nullptr and your kernel should not compute it.

Example: https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/framework/tests/test_mul_op.py#L21

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

@kuke kuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine this operator by following all comments. Please continue to review.

: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("P", "The desired target values for posteriors.");
AddInput("Oi", "The model output for item i.");
AddInput("Oj", "The model output for item j.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


A detailed explanation about these notations can be found in

[1]. Chris Burges, Tal Shaked, Erin Renshaw, et al. Learning to
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auto dims = ctx.Input<framework::Tensor>("P")->dims();
ctx.Output<framework::Tensor>(framework::GradVarName("P"))->Resize(dims);
ctx.Output<framework::Tensor>(framework::GradVarName("Oi"))->Resize(dims);
ctx.Output<framework::Tensor>(framework::GradVarName("Oj"))->Resize(dims);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auto* oi_t = ctx.Input<framework::Tensor>("Oi");
auto* oj_t = ctx.Input<framework::Tensor>("Oj");

d_oi->mutable_data<T>(ctx.GetPlace());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

from op_test import OpTest


class TestReshapeOp(OpTest):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A typo. Fixed

num = 5
# P = {0, 1.0} or {0, 0.5, 1.0}
P = np.random.randint(0, 2, size=(num, num)).astype("float32")
Oi = np.random.random((num, num)).astype("float32")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

def test_check_output(self):
self.check_output()

def test_check_grad(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@kuke kuke requested review from lcy-seso and chengxiaohua1105 and removed request for chengxiaohua1105 September 20, 2017 09:27
JiayiFeng
JiayiFeng previously approved these changes Sep 21, 2017
Copy link
Collaborator

@JiayiFeng JiayiFeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JiayiFeng JiayiFeng dismissed their stale review September 21, 2017 01:29

@lcy-seeo 's review is needed

Copy link
Collaborator

@JiayiFeng JiayiFeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kuke kuke merged commit d827359 into PaddlePaddle:develop Sep 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants