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

Revert `no_grad` change and add new implementation (#2513)

上级 d95c24a0
...@@ -7,8 +7,6 @@ no_grad ...@@ -7,8 +7,6 @@ no_grad
.. py:class:: paddle.fluid.dygraph.no_grad .. py:class:: paddle.fluid.dygraph.no_grad
:api_attr: 命令式编程模式(动态图) :api_attr: 命令式编程模式(动态图)
:alias_main: paddle.no_grad
:alias: paddle.no_grad
:old_api: paddle.fluid.dygraph.no_grad :old_api: paddle.fluid.dygraph.no_grad
...@@ -21,21 +19,19 @@ no_grad ...@@ -21,21 +19,19 @@ no_grad
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle import paddle.fluid as fluid
paddle.disable_static()
paddle.enable_imperative() paddle.enable_imperative()
# 用作生成器 # 用作生成器
data = np.array([[2, 3], [4, 5]]).astype('float32') data = np.array([[2, 3], [4, 5]]).astype('float32')
l0 = paddle.nn.Linear(2, 2) # l0.weight.gradient() is None l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None
l1 = paddle.nn.Linear(2, 2) l1 = fluid.Linear(2, 2)
with paddle.no_grad(): with fluid.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 = paddle.to_tensor(data) x = fluid.dygraph.to_variable(data)
y = l0(x) + tmp y = l0(x) + tmp
o = l1(y) o = l1(y)
o.backward() o.backward()
...@@ -43,12 +39,13 @@ no_grad ...@@ -43,12 +39,13 @@ no_grad
print(l0.weight.gradient() is None) # False print(l0.weight.gradient() is None) # False
# 用作装饰器 # 用作装饰器
@paddle.no_grad()
@fluid.no_grad()
def test_layer(): def test_layer():
inp = np.ones([3, 1024], dtype='float32') inp = np.ones([3, 1024], dtype='float32')
t = paddle.to_tensor(inp) t = fluid.dygraph.base.to_variable(inp)
linear1 = paddle.nn.Linear(1024, 4, bias_attr=False) linear1 = fluid.Linear(1024, 4, bias_attr=False)
linear2 = paddle.nn.Linear(4, 4) linear2 = fluid.Linear(4, 4)
ret = linear1(t) ret = linear1(t)
dy_ret = linear2(ret) dy_ret = linear2(ret)
......
.. _cn_api_paddle_cn_name_scope: .. _cn_api_paddle_no_grad:
name_scope no_grad
------------------------------- -------------------------------
:doc_source: paddle.fluid.dygraph.no_grad
.. py:class:: paddle.fluid.dygraph.no_grad_
:api_attr: 命令式编程模式(动态图)
创建一个上下文来禁用动态图梯度计算。在此模式下,每次计算的结果都将具有stop_gradient=True。
也可以用作一个装饰器(需要创建实例对象作为装饰器)。
**代码示例**
.. code-block:: python
import numpy as np
import paddle
paddle.disable_static()
# 用作生成器
data = np.array([[2, 3], [4, 5]]).astype('float32')
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 = paddle.to_tensor(data)
y = l0(x) + tmp
o = l1(y)
o.backward()
print(tmp.gradient() is None) # True
print(l0.weight.gradient() is None) # False
# 用作装饰器
@paddle.no_grad()
def test_layer():
inp = np.ones([3, 1024], dtype='float32')
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)
test_layer()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册