Skip to content

Support backward of backward for Relu and add a new gradient checker by comparing theoretical and numerical Jacobian.#16862

Merged
qingqing01 merged 8 commits intoPaddlePaddle:developfrom
qingqing01:backward_of_backward
Apr 23, 2019
Merged

Support backward of backward for Relu and add a new gradient checker by comparing theoretical and numerical Jacobian.#16862
qingqing01 merged 8 commits intoPaddlePaddle:developfrom
qingqing01:backward_of_backward

Conversation

@qingqing01
Copy link
Contributor

@qingqing01 qingqing01 commented Apr 15, 2019

  1. Use Relu to get through the whole process from C++ to Python.
    • Add double backward op for Relu (named relu_grad_grad op).
    • Add register for relu_grad_grad.

Add ReluDoubleGradMaker when register relu_grad:

REGISTER_OPERATOR(relu_grad, ops::ActivationOpGrad,
                  paddle::framework::SingleOpInplaceInToOut,
                  ops::ReluDoubleGradMaker);
  1. Add a new gradient checker by comparing theoretical and numerical Jacobian.

    • support to check program, not one op.
    • support to check gradients by API grad_check.
    • support to check double gradients by API double_grad_check.
  2. Add gradients testing for relu_grad_grad.

  3. fluid.backward.calc_gradient is used to append the backward pass, which is similar interface to tf.gradients.

For example:

import paddle.fluid as pd
from paddle.fluid.backward import calc_gradient

# relu
x = pd.layers.data(name='x', shape=[2,4], dtype='float32', append_batch_size=False)
x.stop_gradient=False
y = pd.layers.relu(x)
z = calc_gradient([y], x)
c = calc_gradient(z, x)

print(pd.default_main_program())

Note, this PR doest not use macro to register double grad for activations since only add one for relu. If adding more in the future, will use macro for more general.

@qingqing01 qingqing01 changed the title Support backward of backward for Relu and add a new gradient checker by comparing theoretical and numerical Jacobian.. Support backward of backward for Relu and add a new gradient checker by comparing theoretical and numerical Jacobian. Apr 15, 2019
@qingqing01 qingqing01 force-pushed the backward_of_backward branch from 3bd23f8 to 7c8818d Compare April 15, 2019 07:16
from paddle.fluid.backward import _append_grad_suffix_, _as_list


def _product(t):
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么不用np.product呢?

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, Thanks!

Each Jacobian gradients is a 2-D array with shape [xi_size, yi_size].

Args:
program (Program): a Program with forward pass.
Copy link
Contributor

Choose a reason for hiding this comment

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

这行应该删掉,和下面一行重复了。

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. Thanks!

eps = 0.005
dtype = np.float64

#x = layers.data('x', shape, False, dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

这个也可以删掉了吧?

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.

wanghaoshuang
wanghaoshuang previously approved these changes Apr 15, 2019
test=develop
@qingqing01 qingqing01 force-pushed the backward_of_backward branch from 6a395ae to 2be620a Compare April 16, 2019 05:12
@qingqing01
Copy link
Contributor Author

@sneaxiy I have deleted DoubleGradVarName.
@wanghaoshuang Update code according your comments.

Thanks!

wanghaoshuang
wanghaoshuang previously approved these changes Apr 16, 2019
@qingqing01
Copy link
Contributor Author

[16:03:43]W:	 [Step 1/1] + API_CHANGE=python/paddle/fluid/tests/unittests/CMakeLists.txt
[16:03:43]W:	 [Step 1/1] + echo 'checking CMakeLists.txt change, PR: 16862, changes: python/paddle/fluid/tests/unittests/CMakeLists.txt'
[16:03:43]W:	 [Step 1/1] + '[' python/paddle/fluid/tests/unittests/CMakeLists.txt ']'
[16:03:43]W:	 [Step 1/1] + '[' 16862 '!=' '' ']'
[16:03:43]W:	 [Step 1/1] + '[' CMakeLists.txt == paddle/fluid/API.spec ']'
[16:03:43]W:	 [Step 1/1] + '[' CMakeLists.txt == CMakeLists.txt ']'
[16:03:43] :	 [Step 1/1] checking CMakeLists.txt change, PR: 16862, changes: python/paddle/fluid/tests/unittests/CMakeLists.txt
[16:03:43]W:	 [Step 1/1] ++ python /paddle/tools/check_pr_approval.py 1 6836917 46782768 30176695
[16:03:43]W:	 [Step 1/1] ++ curl -H 'Authorization: token 5ae37427a95d9e51fb0ea3c71121b75d5c2ebefa' 'https://api.github.com/repos/PaddlePaddle/Paddle/pulls/16862/reviews?per_page=10000'
[16:03:43]W:	 [Step 1/1]   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
[16:03:43]W:	 [Step 1/1]                                  Dload  Upload   Total   Spent    Left  Speed
[16:03:43]W:	 [Step 1/1] 
[16:03:44]W:	 [Step 1/1]   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
[16:03:45]W:	 [Step 1/1]   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
[16:03:45]W:	 [Step 1/1]   1  9156    1   125    0     0     64      0  0:02:23  0:00:01  0:02:22    64
[16:03:45]W:	 [Step 1/1] 100  9156  100  9156    0     0   4742      0  0:00:01  0:00:01 --:--:--  4744
[16:03:45] :	 [Step 1/1] current pr 16862 got approvals: FALSE
[16:03:45] :	 [Step 1/1] You must have one RD (luotao1 or chengduoZH or XiaoguangHu01) approval for the cmakelist change! CMakeLists.txt
[16:03:45]W:	 [Step 1/1] + APPROVALS=FALSE

The unit testing failed since Python on Windows CI have a same python package. Rename decorators to decorator_helper. So change the python/paddle/fluid/tests/unittests/CMakeLists.txt.

Please help to review. Thanks! @chengduoZH @luotao1 @XiaoguangHu01

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

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

LGTM for cmakelist.txt

@luotao1
Copy link
Contributor

luotao1 commented Apr 22, 2019

@tianshuo78520a It seems #16944 don't fix this problem #16862 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants