-
Notifications
You must be signed in to change notification settings - Fork 762
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
Conversation
|
||
**属性:** | ||
|
||
.. py:attribute:: sort_sum_gradient | ||
|
||
是否按照前向执行的逆序加和多个梯度,例如当 x_var( :ref:`api_guide_Variable` )作为多个OP(这里以 :ref:`cn_api_fluid_layers_scale` 为例)的输入时,其产生的梯度是否按照前向书写时的 | ||
逆序加和,默认为False | ||
逆序加和,默认为False。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
22行和21行为什么增加了换行?如果要换行,21行那么长也换了吧。如果不换行就都不换
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上面comment,为什么这些变量命名末尾加个2啊?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
预览图中,该OP只在动态图的属性提到了两次,可以删掉一个
There was a problem hiding this 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是描述动态图反向执行时采用的不同策略,包括梯度聚合的顺序等。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
什么情况下用户需要设置这个BackwardStrategy属性呢,还是这个设置只用于内部调试?
There was a problem hiding this comment.
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。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“逆序加和”有什么好处和坏处呢?什么情况下使用呢?
数值更加稳定?避免随机性?
There was a problem hiding this comment.
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,避免一些随机性。
Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Update the demo code and the doc of varbase.backward. The pr of English doc about backward.
Variable.backward
paddle.grad