未验证 提交 f6d20d56 编写于 作者: Y Yang Zhang 提交者: GitHub

Update `no_grad` doc examples (#26481)

test=document_fix
上级 61273973
...@@ -182,19 +182,19 @@ class no_grad: ...@@ -182,19 +182,19 @@ class no_grad:
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle
paddle.enable_imperative() paddle.disable_static()
# use as generator # use as generator
data = np.array([[2, 3], [4, 5]]).astype('float32') data = np.array([[2, 3], [4, 5]]).astype('float32')
l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None l0 = paddle.nn.Linear(2, 2) # l0.weight.gradient() is None
l1 = fluid.Linear(2, 2) l1 = paddle.nn.Linear(2, 2)
with fluid.no_grad(): with paddle.no_grad():
# l1.weight.stop_gradient is False # l1.weight.stop_gradient is False
tmp = l1.weight * 2 # tmp.stop_gradient is True tmp = l1.weight * 2 # tmp.stop_gradient is True
x = fluid.dygraph.to_variable(data) x = paddle.to_tensor(data)
y = l0(x) + tmp y = l0(x) + tmp
o = l1(y) o = l1(y)
o.backward() o.backward()
...@@ -203,12 +203,12 @@ class no_grad: ...@@ -203,12 +203,12 @@ class no_grad:
# use as decorator # use as decorator
@fluid.no_grad() @paddle.no_grad()
def test_layer(): def test_layer():
inp = np.ones([3, 1024], dtype='float32') inp = np.ones([3, 1024], dtype='float32')
t = fluid.dygraph.base.to_variable(inp) t = paddle.to_tensor(inp)
linear1 = fluid.Linear(1024, 4, bias_attr=False) linear1 = paddle.nn.Linear(1024, 4, bias_attr=False)
linear2 = fluid.Linear(4, 4) linear2 = paddle.nn.Linear(4, 4)
ret = linear1(t) ret = linear1(t)
dy_ret = linear2(ret) dy_ret = linear2(ret)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册