未验证 提交 acb42ffb 编写于 作者: Z Zhen Wang 提交者: GitHub

Update the demo code and the doc of varbase.backward. (#2448)

* update the demo code and the doc of varbase.backward.

* remove the dependence of  BackwardStrategy
上级 2ed3ccb1
...@@ -5,7 +5,6 @@ fluid.dygraph ...@@ -5,7 +5,6 @@ fluid.dygraph
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1
dygraph/BackwardStrategy.rst
dygraph/BatchNorm.rst dygraph/BatchNorm.rst
dygraph/BilinearTensorProduct.rst dygraph/BilinearTensorProduct.rst
dygraph/Conv2D.rst dygraph/Conv2D.rst
......
.. THIS FILE IS GENERATED BY `gen_doc.{py|sh}`
!DO NOT EDIT THIS FILE MANUALLY!
.. _api_fluid_dygraph_BackwardStrategy:
BackwardStrategy
----------------
.. autoclass:: paddle.fluid.dygraph.BackwardStrategy
:members:
:noindex:
...@@ -8,7 +8,6 @@ fluid.dygraph ...@@ -8,7 +8,6 @@ fluid.dygraph
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1
dygraph_cn/BackwardStrategy_cn.rst
dygraph_cn/BatchNorm_cn.rst dygraph_cn/BatchNorm_cn.rst
dygraph_cn/BilinearTensorProduct_cn.rst dygraph_cn/BilinearTensorProduct_cn.rst
dygraph_cn/Conv2D_cn.rst dygraph_cn/Conv2D_cn.rst
......
.. _cn_api_fluid_dygraph_BackwardStrategy:
BackwardStrategy
-------------------------------
.. py:class:: paddle.fluid.dygraph.BackwardStrategy
:api_attr: 命令式编程模式(动态图)
**注意:该API只在动态图下生效**
BackwardStrategy是描述动态图反向执行的策略,主要功能是定义动态图反向执行时的不同策略
**属性:**
.. py:attribute:: sort_sum_gradient
是否按照前向执行的逆序加和多个梯度,例如当 x_var( :ref:`api_guide_Variable` )作为多个OP(这里以 :ref:`cn_api_fluid_layers_scale` 为例)的输入时,其产生的梯度是否按照前向书写时的
逆序加和,默认为False
**代码示例**
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
x = np.ones([2, 2], np.float32)
with fluid.dygraph.guard():
x_var = fluid.dygraph.to_variable(x)
sums_inputs = []
# 这里x_var将作为多个输入scale的输入
for _ in range(10):
sums_inputs.append(fluid.layers.scale(x_var))
ret2 = fluid.layers.sums(sums_inputs)
loss2 = fluid.layers.reduce_sum(ret2)
backward_strategy = fluid.dygraph.BackwardStrategy()
backward_strategy.sort_sum_gradient = True
loss2.backward(backward_strategy)
...@@ -5,24 +5,21 @@ grad ...@@ -5,24 +5,21 @@ grad
**注意:该API仅支持【动态图】模式** **注意:该API仅支持【动态图】模式**
.. py:method:: paddle.grad(outputs, inputs, grad_outputs=None, retain_graph=None, create_graph=False, only_inputs=True, allow_unused=False, no_grad_vars=None, backward_strategy=None) .. py:method:: paddle.grad(outputs, inputs, grad_outputs=None, retain_graph=None, create_graph=False, only_inputs=True, allow_unused=False, no_grad_vars=None)
对于每个 `inputs` ,计算所有 `outputs` 相对于其的梯度和。 对于每个 `inputs` ,计算所有 `outputs` 相对于其的梯度和。
参数: 参数:
- **outputs** (Variable|list(Variable)|tuple(Variable)) – 用于计算梯度的图的输出变量,或多个输出变量构成的list/tuple。 - **outputs** (Tensor|list(Tensor)|tuple(Tensor)) – 用于计算梯度的图的输出变量,或多个输出变量构成的list/tuple。
- **inputs** (Variable|list(Variable)|tuple(Variable)) - 用于计算梯度的图的输入变量,或多个输入变量构成的list/tuple。该API的每个返回值对应每个 `inputs` 的梯度。 - **inputs** (Tensor|list(Tensor)|tuple(Tensor)) - 用于计算梯度的图的输入变量,或多个输入变量构成的list/tuple。该API的每个返回值对应每个 `inputs` 的梯度。
- **grad_outputs** (Variable|list(Variable|None)|tuple(Variable|None), 可选) - `outputs` 变量梯度的初始值。若 `grad_outputs` 为None,则 `outputs` 梯度的初始值均为全1的Tensor。若 `grad_outputs` 不为None,它必须与 `outputs` 的长度相等,此时,若 `grad_outputs` 的第i个元素为None,则第i个 `outputs` 的梯度初始值为全1的Tensor;若 `grad_outputs` 的第i个元素为Variable,则第i个 `outputs` 的梯度初始值为 `grad_outputs` 的第i个元素。默认值为None。 - **grad_outputs** (Tensor|list(Tensor|None)|tuple(Tensor|None), 可选) - `outputs` 变量梯度的初始值。若 `grad_outputs` 为None,则 `outputs` 梯度的初始值均为全1的Tensor。若 `grad_outputs` 不为None,它必须与 `outputs` 的长度相等,此时,若 `grad_outputs` 的第i个元素为None,则第i个 `outputs` 的梯度初始值为全1的Tensor;若 `grad_outputs` 的第i个元素为Tensor,则第i个 `outputs` 的梯度初始值为 `grad_outputs` 的第i个元素。默认值为None。
- **retain_graph** (bool, 可选) - 是否保留计算梯度的前向图。若值为True,则前向图会保留,用户可对同一张图求两次反向。若值为False,则前向图会释放。默认值为None,表示值与 `create_graph` 相等。 - **retain_graph** (bool, 可选) - 是否保留计算梯度的前向图。若值为True,则前向图会保留,用户可对同一张图求两次反向。若值为False,则前向图会释放。默认值为None,表示值与 `create_graph` 相等。
- **create_graph** (bool, 可选) - 是否创建计算过程中的反向图。若值为True,则可支持计算高阶导数。若值为False,则计算过程中的反向图会释放。默认值为False。 - **create_graph** (bool, 可选) - 是否创建计算过程中的反向图。若值为True,则可支持计算高阶导数。若值为False,则计算过程中的反向图会释放。默认值为False。
- **only_inputs** (bool, 可选) - 是否只计算 `inputs` 的梯度。若值为False,则图中所有叶节点变量的梯度均会计算,并进行累加。若值为True,则只会计算 `inputs` 的梯度。默认值为True。only_inputs=False功能正在开发中,目前尚不支持。 - **only_inputs** (bool, 可选) - 是否只计算 `inputs` 的梯度。若值为False,则图中所有叶节点变量的梯度均会计算,并进行累加。若值为True,则只会计算 `inputs` 的梯度。默认值为True。only_inputs=False功能正在开发中,目前尚不支持。
- **allow_unused** (bool, 可选) - 决定当某些 `inputs` 变量不在计算图中时抛出错误还是返回None。若某些 `inputs` 变量不在计算图中(即它们的梯度为None),则当allowed_unused=False时会抛出错误,当allow_unused=True时会返回None作为这些变量的梯度。默认值为False。 - **allow_unused** (bool, 可选) - 决定当某些 `inputs` 变量不在计算图中时抛出错误还是返回None。若某些 `inputs` 变量不在计算图中(即它们的梯度为None),则当allowed_unused=False时会抛出错误,当allow_unused=True时会返回None作为这些变量的梯度。默认值为False。
- **no_grad_vars** (Variable|list(Variable)|tuple(Variable)|set(Variable), 可选) - 指明不需要计算梯度的变量。默认值为None。 - **no_grad_vars** (Tensor|list(Tensor)|tuple(Tensor)|set(Tensor), 可选) - 指明不需要计算梯度的变量。默认值为None。
- **backward_strategy** (BackwardStrategy, 可选) - 计算梯度的策略。详见 :ref:`cn_api_fluid_dygraph_BackwardStrategy` 。默认值为None。
返回: 变量构成的tuple,其长度等于 `inputs` 中的变量个数,且第i个返回的变量是所有 `outputs` 相对于第i个 `inputs` 的梯度之和。 返回: tuple(Tensor),其长度等于 `inputs` 中的变量个数,且第i个返回的变量是所有 `outputs` 相对于第i个 `inputs` 的梯度之和。
返回类型: tuple
**示例代码 1** **示例代码 1**
.. code-block:: python .. code-block:: python
......
...@@ -145,7 +145,7 @@ Variable ...@@ -145,7 +145,7 @@ Variable
**参数:** **参数:**
- **backward_strategy**: ( :ref:`cn_api_fluid_dygraph_BackwardStrategy` ) 使用何种 :ref:`cn_api_fluid_dygraph_BackwardStrategy` 聚合反向的梯度 - **retain_graph** (bool,可选) – 该参数用于确定反向梯度更新完成后反向梯度计算图是否需要保留(retain_graph为True则保留反向梯度计算图)。若用户打算在执行完该方法( :code:`backward` )后,继续向之前已构建的计算图中添加更多的Op,则需要设置 :code:`retain_graph` 值为True(这样才会保留之前计算得到的梯度)。可以看出,将 :code:`retain_graph` 设置为False可降低内存的占用。默认值为False。
返回:无 返回:无
...@@ -153,23 +153,20 @@ Variable ...@@ -153,23 +153,20 @@ Variable
**示例代码** **示例代码**
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid
import numpy as np import numpy as np
import paddle
paddle.disable_static()
x = np.ones([2, 2], np.float32) x = np.ones([2, 2], np.float32)
with fluid.dygraph.guard(): inputs = []
inputs2 = [] for _ in range(10):
for _ in range(10): tmp = paddle.to_tensor(x)
tmp = fluid.dygraph.base.to_variable(x) # 如果这里我们不为输入tmp设置stop_gradient=False,那么后面loss也将因为这个链路都不需要梯度
# 如果这里我们不为输入tmp设置stop_gradient=False,那么后面loss2也将因为这个链路都不需要梯度 # 而不产生梯度
# 而不产生梯度 tmp.stop_gradient=False
tmp.stop_gradient=False inputs.append(tmp)
inputs2.append(tmp) ret = paddle.sums(inputs)
ret2 = fluid.layers.sums(inputs2) loss = paddle.reduce_sum(ret)
loss2 = fluid.layers.reduce_sum(ret2) loss.backward()
backward_strategy = fluid.dygraph.BackwardStrategy()
backward_strategy.sort_sum_gradient = True
loss2.backward(backward_strategy)
.. py:method:: gradient() .. py:method:: gradient()
...@@ -202,9 +199,7 @@ Variable ...@@ -202,9 +199,7 @@ Variable
inputs2.append(tmp) inputs2.append(tmp)
ret2 = fluid.layers.sums(inputs2) ret2 = fluid.layers.sums(inputs2)
loss2 = fluid.layers.reduce_sum(ret2) loss2 = fluid.layers.reduce_sum(ret2)
backward_strategy = fluid.dygraph.BackwardStrategy() loss2.backward()
backward_strategy.sort_sum_gradient = True
loss2.backward(backward_strategy)
print(loss2.gradient()) print(loss2.gradient())
# example2: 返回tuple of ndarray # example2: 返回tuple of ndarray
...@@ -248,9 +243,7 @@ Variable ...@@ -248,9 +243,7 @@ Variable
inputs2.append(tmp) inputs2.append(tmp)
ret2 = fluid.layers.sums(inputs2) ret2 = fluid.layers.sums(inputs2)
loss2 = fluid.layers.reduce_sum(ret2) loss2 = fluid.layers.reduce_sum(ret2)
backward_strategy = fluid.dygraph.BackwardStrategy() loss2.backward()
backward_strategy.sort_sum_gradient = True
loss2.backward(backward_strategy)
print(loss2.gradient()) print(loss2.gradient())
loss2.clear_gradient() loss2.clear_gradient()
print("After clear {}".format(loss2.gradient())) print("After clear {}".format(loss2.gradient()))
...@@ -351,6 +344,7 @@ Variable ...@@ -351,6 +344,7 @@ Variable
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np
with fluid.dygraph.guard(): with fluid.dygraph.guard():
value0 = np.arange(26).reshape(2, 13).astype("float32") value0 = np.arange(26).reshape(2, 13).astype("float32")
...@@ -366,9 +360,9 @@ Variable ...@@ -366,9 +360,9 @@ Variable
out1.stop_gradient = True out1.stop_gradient = True
out = fluid.layers.concat(input=[out1, out2, c], axis=1) out = fluid.layers.concat(input=[out1, out2, c], axis=1)
out.backward() out.backward()
# 可以发现这里linear的参数变成了 # 可以发现这里linear的参数梯度变成了None
assert (linear.weight.gradient() == 0).all() assert linear.weight.gradient() is None
assert (out1.gradient() == 0).all() assert out1.gradient() is None
.. py:attribute:: persistable .. py:attribute:: persistable
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册