Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Aug 27, 2020
Merged

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

merged 5 commits into from
Aug 27, 2020

Conversation

wzzju
Copy link
Contributor

@wzzju wzzju commented Aug 21, 2020

Update the demo code and the doc of varbase.backward. The pr of English doc about backward.

Variable.backward

image

paddle.grad

180 76 141 178_documentation_docs_zh_api_cn_dygraph_cn_grad_cn html (1)

@wzzju wzzju changed the title update the demo code and the doc of varbase.backward. Update the demo code and the doc of varbase.backward. Aug 21, 2020

**属性:**

.. py:attribute:: sort_sum_gradient

是否按照前向执行的逆序加和多个梯度,例如当 x_var( :ref:`api_guide_Variable` )作为多个OP(这里以 :ref:`cn_api_fluid_layers_scale` 为例)的输入时,其产生的梯度是否按照前向书写时的
逆序加和,默认为False
逆序加和,默认为False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22行和21行为什么增加了换行?如果要换行,21行那么长也换了吧。如果不换行就都不换

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

预览图中,该OP只在动态图的属性提到了两次,可以删掉一个

Done.

# 这里x_var将作为多个输入scale的输入
for _ in range(10):
sums_inputs.append(paddle.scale(x_var))
ret2 = paddle.sums(sums_inputs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

都没有ret1,为什么有ret2?同样loss2,改成ret,loss?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

backward_strategy = fluid.dygraph.BackwardStrategy()
backward_strategy.sort_sum_gradient = True
loss2.backward(backward_strategy)
inputs2 = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上面comment,为什么这些变量命名末尾加个2啊?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,之前的代码是这样的,我改一下哈。

backward_strategy = fluid.dygraph.BackwardStrategy()
backward_strategy.sort_sum_gradient = True
loss2.backward(backward_strategy)
x_var = paddle.to_variable(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle.to_variable->paddle.to_tensor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

loss2.backward(backward_strategy)
inputs2 = []
for _ in range(10):
tmp = paddle.to_variable(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle.to_variable->paddle.to_tensor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@swtkiwi swtkiwi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

预览图中,该OP只在动态图的属性提到了两次,可以删掉一个

Copy link
Collaborator

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM




**注意:该API只在动态图下生效**

BackwardStrategy是描述动态图反向执行的策略,主要功能是定义动态图反向执行时的不同策略
BackwardStrategy是描述动态图反向执行时采用的不同策略,包括梯度聚合的顺序等。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

什么情况下用户需要设置这个BackwardStrategy属性呢,还是这个设置只用于内部调试?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢。这个基本只用于内部调试。


**属性:**

.. py:attribute:: sort_sum_gradient

是否按照前向执行的逆序加和多个梯度,例如当 x_var( :ref:`api_guide_Variable` )作为多个OP(这里以 :ref:`cn_api_fluid_layers_scale` 为例)的输入时,其产生的梯度是否按照前向书写时的
逆序加和,默认为False
是否按照前向执行的逆序加和多个梯度,例如当 x_var( :ref:`api_guide_Variable` )作为多个OP(这里以 :ref:`cn_api_fluid_layers_scale` 为例)的输入时,其产生的梯度是否按照前向书写时的逆序加和,默认为False。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“逆序加和”有什么好处和坏处呢?什么情况下使用呢?
数值更加稳定?避免随机性?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为静态图var的梯度是按照前向书写时的逆序加和的,所以若是希望相同模型的动态图loss可以和静态图对齐,需要设置这里的sort_sum_gradient值为Ture,避免一些随机性。

@wzzju
Copy link
Contributor Author

wzzju commented Aug 21, 2020

预览图中,该OP只在动态图的属性提到了两次,可以删掉一个

Done.

Copy link
Collaborator

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM~

Copy link
Collaborator

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@zhhsplendid zhhsplendid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wzzju wzzju merged commit acb42ff into PaddlePaddle:develop Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants