未验证 提交 34241dd1 编写于 作者: S Sing_chan 提交者: GitHub

change vjp to paddle.grad (#41231)

* change vjp to paddle.grad

* use grad and gradients api

* fix preprocess for x

* fix a bug, val_and_grad should return a Tensor

* detach value and grad to avoid assign error
Co-authored-by: Nlevi131 <limaolin01@baidu.com>
上级 f1c5815e
......@@ -13,7 +13,6 @@
# limitations under the License.
import paddle
from paddle.autograd.functional import vjp, Jacobian
from paddle.fluid.framework import Variable
from paddle.fluid.data_feeder import check_type, check_dtype
......@@ -86,11 +85,14 @@ def _value_and_gradient(f, x, v=None):
value: a tensor that holds the function value.
gradient: a tensor that holds the function gradients.
"""
# use detach to cut off relation between x and original graph
x = x.detach()
x.stop_gradient = False
value = f(x)
if paddle.in_dynamic_mode():
value, gradient = vjp(f, x, v=v)
gradient = gradient[0]
# only need to compute first order derivative, and some op dont support high order derivative.
gradient = paddle.grad([value], [x], create_graph=False)[0]
else:
JJ = Jacobian(f, x)
gradient = JJ[:][0]
value = f(x)
return value, gradient
gradient = paddle.static.gradients([value], [x])[0]
# use detach to make results real number without grad to avoid assign error
return value.detach(), gradient.detach()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册