Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
FluidDoc
提交
c27c202c
F
FluidDoc
项目概览
PaddlePaddle
/
FluidDoc
通知
7
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
23
列表
看板
标记
里程碑
合并请求
111
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
FluidDoc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
23
Issue
23
列表
看板
标记
里程碑
合并请求
111
合并请求
111
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
c27c202c
编写于
8月 22, 2020
作者:
Z
Zhen Wang
提交者:
GitHub
8月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the example codes of paddle.grad (#2444)
上级
49369fb9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
35 addition
and
36 deletion
+35
-36
doc/fluid/api/dygraph/grad.rst
doc/fluid/api/dygraph/grad.rst
+2
-2
doc/fluid/api_cn/dygraph_cn/grad_cn.rst
doc/fluid/api_cn/dygraph_cn/grad_cn.rst
+33
-34
未找到文件。
doc/fluid/api/dygraph/grad.rst
浏览文件 @
c27c202c
.. THIS FILE IS GENERATED BY `gen_doc.{py|sh}`
!DO NOT EDIT THIS FILE MANUALLY!
.. _api_
fluid_dygraph
_grad:
.. _api_
paddle
_grad:
grad
----
.. autofunction:: paddle.
fluid.dygraph.
grad
.. autofunction:: paddle.grad
:noindex:
doc/fluid/api_cn/dygraph_cn/grad_cn.rst
浏览文件 @
c27c202c
.. _cn_api_
fluid_dygraph
_grad:
.. _cn_api_
paddle
_grad:
grad
-------------------------------
**注意:该API仅支持【动态图】模式**
.. py:method:: paddle.
fluid.dygraph.
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, backward_strategy=None)
对于每个 `inputs` ,计算所有 `outputs` 相对于其的梯度和。
...
...
@@ -27,34 +27,34 @@ grad
**示例代码 1**
.. code-block:: python
import paddle.fluid as fluid
import paddle
paddle.disable_static()
def test_dygraph_grad(create_graph):
with fluid.dygraph.guard():
x = fluid.layers.ones(shape=[1], dtype='float32')
x.stop_gradient = False
y = x * x
x = paddle.ones(shape=[1], dtype='float32')
x.stop_gradient = False
y = x * x
# Since y = x * x, dx = 2 * x
dx = fluid.dygraph
.grad(
outputs=[y],
inputs=[x],
create_graph=create_graph,
retain_graph=True)[0]
# Since y = x * x, dx = 2 * x
dx = paddle
.grad(
outputs=[y],
inputs=[x],
create_graph=create_graph,
retain_graph=True)[0]
z = y + dx
z = y + dx
# If create_graph = False, the gradient of dx
# would not be backpropagated. Therefore,
# z = x * x + dx, and x.gradient() = 2 * x = 2.0
# If create_graph = False, the gradient of dx
# would not be backpropagated. Therefore,
# z = x * x + dx, and x.gradient() = 2 * x = 2.0
# If create_graph = True, the gradient of dx
# would be backpropagated. Therefore,
# z = x * x + dx = x * x + 2 * x, and
# x.gradient() = 2 * x + 2 = 4.0
# If create_graph = True, the gradient of dx
# would be backpropagated. Therefore,
# z = x * x + dx = x * x + 2 * x, and
# x.gradient() = 2 * x + 2 = 4.0
z.backward()
return x.gradient()
z.backward()
return x.gradient()
print(test_dygraph_grad(create_graph=False)) # [2.]
print(test_dygraph_grad(create_graph=True)) # [4.]
...
...
@@ -62,16 +62,15 @@ grad
**示例代码 2**
.. code-block:: python
import paddle.fluid as fluid
fluid.enable_dygraph()
import paddle
paddle.disable_static()
def test_dygraph_grad(grad_outputs=None):
x =
fluid.layers
.fill_constant(shape=[1], value=2.0, dtype='float32')
x =
paddle
.fill_constant(shape=[1], value=2.0, dtype='float32')
x.stop_gradient = False
y1 = x * x
y2 = x * 3
y2 = x * 3
# If grad_outputs=None, dy1 = [1], dy2 = [1].
# If grad_outputs=[g1, g2], then:
...
...
@@ -83,24 +82,24 @@ grad
# Therefore, the final result would be:
# dx = 2 * x * dy1 + 3 * dy2 = 4 * dy1 + 3 * dy2.
dx =
fluid.dygraph
.grad(
dx =
paddle
.grad(
outputs=[y1, y2],
inputs=[x],
grad_outputs=grad_outputs)[0]
return dx.numpy()
THREE = fluid.layers.fill_constant(shape=[1], value=3.0, dtype='float32')
FOUR = fluid.layers.fill_constant(shape=[1], value=4.0, dtype='float32')
grad_value = paddle.fill_constant(shape=[1], value=4.0, dtype='float32')
# dy1 = [1], dy2 = [1]
print(test_dygraph_grad(None)) # [7.]
# dy1 = [1], dy2 = [4]
print(test_dygraph_grad([None,
FOUR
])) # [16.]
print(test_dygraph_grad([None,
grad_value
])) # [16.]
# dy1 = [4], dy2 = [1]
print(test_dygraph_grad([
FOUR
, None])) # [19.]
print(test_dygraph_grad([
grad_value
, None])) # [19.]
# dy1 = [3], dy2 = [4]
print(test_dygraph_grad([THREE, FOUR])) # [24.]
\ No newline at end of file
grad_y1 = paddle.fill_constant(shape=[1], value=3.0, dtype='float32')
print(test_dygraph_grad([grad_y1, grad_value])) # [24.]
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录