From 3354d5569870b8c4b008a72c97da072c183f88e9 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 21 Aug 2020 13:08:34 +0800 Subject: [PATCH] Update `no_grad` examples (#2426) --- doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst b/doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst index b58357ae1..55fc4b932 100644 --- a/doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst +++ b/doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst @@ -21,19 +21,21 @@ no_grad .. code-block:: python import numpy as np - import paddle.fluid as fluid + import paddle + + paddle.disable_static() paddle.enable_imperative() # 用作生成器 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() @@ -41,13 +43,12 @@ no_grad print(l0.weight.gradient() is None) # False # 用作装饰器 - - @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) -- GitLab