-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Update the demo of paddle.grad #26498
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -375,47 +375,46 @@ def grad(outputs, | |
Examples 1: | ||
.. code-block:: python | ||
|
||
import paddle.fluid as fluid | ||
import paddle | ||
paddle.disable_static() | ||
|
||
def test_dygraph_grad(create_graph): | ||
with fluid.dygraph.guard(): | ||
x = fluid.layers.ones(shape=[1], dtype='float32') | ||
x.stop_gradient = False | ||
y = x * x | ||
|
||
# Since y = x * x, dx = 2 * x | ||
dx = fluid.dygraph.grad( | ||
outputs=[y], | ||
inputs=[x], | ||
create_graph=create_graph, | ||
retain_graph=True)[0] | ||
|
||
z = y + dx | ||
|
||
# If create_graph = False, the gradient of dx | ||
# would not be backpropagated. Therefore, | ||
# z = x * x + dx, and x.gradient() = 2 * x = 2.0 | ||
|
||
# If create_graph = True, the gradient of dx | ||
# would be backpropagated. Therefore, | ||
# z = x * x + dx = x * x + 2 * x, and | ||
# x.gradient() = 2 * x + 2 = 4.0 | ||
|
||
z.backward() | ||
return x.gradient() | ||
|
||
print(test_dygraph_grad(create_graph=False)) # [2.] | ||
x = paddle.ones(shape=[1], dtype='float32') | ||
x.stop_gradient = False | ||
y = x * x | ||
|
||
# Since y = x * x, dx = 2 * x | ||
dx = paddle.grad( | ||
outputs=[y], | ||
inputs=[x], | ||
create_graph=create_graph, | ||
retain_graph=True)[0] | ||
|
||
z = y + dx | ||
|
||
# If create_graph = False, the gradient of dx | ||
# would not be backpropagated. Therefore, | ||
# z = x * x + dx, and x.gradient() = 2 * x = 2.0 | ||
|
||
# If create_graph = True, the gradient of dx | ||
# would be backpropagated. Therefore, | ||
# z = x * x + dx = x * x + 2 * x, and | ||
# x.gradient() = 2 * x + 2 = 4.0 | ||
|
||
z.backward() | ||
return x.gradient() | ||
|
||
print(test_dygraph_grad(create_graph=False)) # [2.] | ||
print(test_dygraph_grad(create_graph=True)) # [4.] | ||
|
||
Examples 2: | ||
.. code-block:: python | ||
|
||
import paddle.fluid as fluid | ||
|
||
fluid.enable_dygraph() | ||
import paddle | ||
paddle.disable_static() | ||
|
||
def test_dygraph_grad(grad_outputs=None): | ||
x = fluid.layers.fill_constant(shape=[1], value=2.0, dtype='float32') | ||
x = paddle.fill_constant(shape=[1], value=2.0, dtype='float32') | ||
x.stop_gradient = False | ||
|
||
y1 = x * x | ||
|
@@ -431,27 +430,27 @@ def test_dygraph_grad(grad_outputs=None): | |
# Therefore, the final result would be: | ||
# dx = 2 * x * dy1 + 3 * dy2 = 4 * dy1 + 3 * dy2. | ||
|
||
dx = fluid.dygraph.grad( | ||
dx = paddle.grad( | ||
outputs=[y1, y2], | ||
inputs=[x], | ||
grad_outputs=grad_outputs)[0] | ||
|
||
return dx.numpy() | ||
|
||
THREE = fluid.layers.fill_constant(shape=[1], value=3.0, dtype='float32') | ||
FOUR = fluid.layers.fill_constant(shape=[1], value=4.0, dtype='float32') | ||
grad_value = paddle.fill_constant(shape=[1], value=4.0, dtype='float32') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. THREE, FOUR是干嘛的? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have changed the var name. Thanks. |
||
# dy1 = [1], dy2 = [1] | ||
print(test_dygraph_grad(None)) # [7.] | ||
|
||
# dy1 = [1], dy2 = [4] | ||
print(test_dygraph_grad([None, FOUR])) # [16.] | ||
print(test_dygraph_grad([None, grad_value])) # [16.] | ||
|
||
# dy1 = [4], dy2 = [1] | ||
print(test_dygraph_grad([FOUR, None])) # [19.] | ||
print(test_dygraph_grad([grad_value, None])) # [19.] | ||
|
||
# dy1 = [3], dy2 = [4] | ||
print(test_dygraph_grad([THREE, FOUR])) # [24.] | ||
grad_y1 = paddle.fill_constant(shape=[1], value=3.0, dtype='float32') | ||
print(test_dygraph_grad([grad_y1, grad_value])) # [24.] | ||
''' | ||
|
||
def check_in_out(in_out_list, name): | ||
|
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.
exapmle2 里不需要再定义一个函数了吧。
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.
感谢。为了展示参数不同时不同的调用效果(代码复用),所以在example代码中定义了函数。