Skip to content
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 no_grad doc examples #26481

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions python/paddle/fluid/dygraph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,19 @@ class no_grad:
.. code-block:: python

import numpy as np
import paddle.fluid as fluid
import paddle

paddle.enable_imperative()
paddle.disable_static()

# use as generator

data = np.array([[2, 3], [4, 5]]).astype('float32')
l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None
l1 = fluid.Linear(2, 2)
with fluid.no_grad():
l0 = paddle.nn.Linear(2, 2) # l0.weight.gradient() is None
l1 = paddle.nn.Linear(2, 2)
with paddle.no_grad():
# l1.weight.stop_gradient is False
tmp = l1.weight * 2 # tmp.stop_gradient is True
x = fluid.dygraph.to_variable(data)
x = paddle.to_tensor(data)
y = l0(x) + tmp
o = l1(y)
o.backward()
Expand All @@ -203,12 +203,12 @@ class no_grad:

# use as decorator

@fluid.no_grad()
@paddle.no_grad()
def test_layer():
inp = np.ones([3, 1024], dtype='float32')
t = fluid.dygraph.base.to_variable(inp)
linear1 = fluid.Linear(1024, 4, bias_attr=False)
linear2 = fluid.Linear(4, 4)
t = paddle.to_tensor(inp)
linear1 = paddle.nn.Linear(1024, 4, bias_attr=False)
linear2 = paddle.nn.Linear(4, 4)
ret = linear1(t)
dy_ret = linear2(ret)

Expand Down