From 887073169551614d482a7ac3aa0f7ac255a4a746 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 14 Aug 2020 13:36:52 +0800 Subject: [PATCH] Update `no_grad` docs (#2324) * Update `no_grad` docs add alias `paddle.no_grad` use `paddle.no_grad()` for decorator usage update code sample * Fix alias * Reword decorator usage note --- doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst | 53 ++++++++++++---------- doc/fluid/api_cn/paddle_cn.rst | 5 +- doc/fluid/api_cn/paddle_cn/no_grad_cn.rst | 5 ++ 3 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 doc/fluid/api_cn/paddle_cn/no_grad_cn.rst 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 e93287287..b58357ae1 100644 --- a/doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst +++ b/doc/fluid/api_cn/dygraph_cn/no_grad_cn.rst @@ -4,48 +4,51 @@ no_grad ------------------------------- -.. py:method:: paddle.fluid.dygraph.no_grad(func=None) +.. py:class:: paddle.fluid.dygraph.no_grad :api_attr: 命令式编程模式(动态图) - +:alias_main: paddle.no_grad +:alias: paddle.no_grad +:old_api: paddle.fluid.dygraph.no_grad 创建一个上下文来禁用动态图梯度计算。在此模式下,每次计算的结果都将具有stop_gradient=True。 -也可以用作一个装饰器(确保不要用括号来初始化)。 +也可以用作一个装饰器(需要创建实例对象作为装饰器)。 **代码示例** .. code-block:: python - import numpy as np import paddle.fluid as fluid + paddle.enable_imperative() + # 用作生成器 + data = np.array([[2, 3], [4, 5]]).astype('float32') - with fluid.dygraph.guard(): - l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None - l1 = fluid.Linear(2, 2) - with fluid.dygraph.no_grad(): - # l1.weight.stop_gradient is False - tmp = l1.weight * 2 # tmp.stop_gradient is True - x = fluid.dygraph.to_variable(data) - y = l0(x) + tmp - o = l1(y) - o.backward() - print(tmp.gradient() is None) # True - print(l0.weight.gradient() is None) # False - + l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None + l1 = fluid.Linear(2, 2) + with fluid.no_grad(): + # l1.weight.stop_gradient is False + tmp = l1.weight * 2 # tmp.stop_gradient is True + x = fluid.dygraph.to_variable(data) + y = l0(x) + tmp + o = l1(y) + o.backward() + print(tmp.gradient() is None) # True + print(l0.weight.gradient() is None) # False + # 用作装饰器 - @fluid.dygraph.no_grad + + @fluid.no_grad() def test_layer(): - with fluid.dygraph.guard(): - 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) - ret = linear1(t) - dy_ret = linear2(ret) + 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) + ret = linear1(t) + dy_ret = linear2(ret) test_layer() diff --git a/doc/fluid/api_cn/paddle_cn.rst b/doc/fluid/api_cn/paddle_cn.rst index b932542a0..7497eb21a 100644 --- a/doc/fluid/api_cn/paddle_cn.rst +++ b/doc/fluid/api_cn/paddle_cn.rst @@ -5,7 +5,7 @@ paddle .. toctree:: :maxdepth: 1 - paddle_cn/abs_cn.rst + paddle_cn/abs_cn.rst paddle_cn/acos_cn.rst paddle_cn/addcmul_cn.rst paddle_cn/addmm_cn.rst @@ -101,7 +101,8 @@ paddle paddle_cn/minimum_cn.rst paddle_cn/multiplex_cn.rst paddle_cn/mul_cn.rst - paddle_cn/name_scope_cn.rst + paddle_cn/name_scope_cn.rst + paddle_cn/no_grad_cn.rst paddle_cn/nonzero_cn.rst paddle_cn/not_equal_cn.rst paddle_cn/ones_cn.rst diff --git a/doc/fluid/api_cn/paddle_cn/no_grad_cn.rst b/doc/fluid/api_cn/paddle_cn/no_grad_cn.rst new file mode 100644 index 000000000..afd06deff --- /dev/null +++ b/doc/fluid/api_cn/paddle_cn/no_grad_cn.rst @@ -0,0 +1,5 @@ +.. _cn_api_paddle_cn_name_scope: + +name_scope +------------------------------- +:doc_source: paddle.fluid.dygraph.no_grad -- GitLab