首页 Paddle框架 帖子详情
如何使用backward求梯度 已解决
收藏
快速回复
Paddle框架 问答模型训练 673 1
如何使用backward求梯度 已解决
收藏
快速回复
Paddle框架 问答模型训练 673 1
在线环境中使用backward()求梯度出错,即使是复制例子里的代码也出错
 
错误信息:RuntimeError: (AlreadyExists) Accumulators are not empty before preparing it for backward network execution. [Hint: Expected accumulators_.empty() == true, but received accumulators_.empty():0 != true:1.] (at /paddle/paddle/fluid/imperative/basic_engine.cc:55
import paddle

a = paddle.to_tensor(2.0, stop_gradient=False)
b = paddle.to_tensor(5.0, stop_gradient=False)
c = a * b

d = paddle.to_tensor(4.0, stop_gradient=False)
e = c * d
e.backward()
print("Tensor a's grad is: {}".format(a.grad))
print("Tensor b's grad is: {}".format(b.grad))
print("Tensor c's grad is: {}".format(c.grad))
print("Tensor d's grad is: {}".format(d.grad))​
2# 回复于2022-01
Warning: tensor.grad will return the tensor value of the gradient. This is an incompatible upgrade for tensor.grad API. It's return type changes from numpy.ndarray in version 2.0 to paddle.Tensor in version 2.1.0. If you want to get the numpy value of the gradient, you can use :code:`x.grad.numpy()`   我运行了你的代码,NOTEBOOK运行通过了,报了个版本的警告。你可以再重启下GPU环境尝试一下,警告的意思是让你把 grad换成grad.numpy()
展开
0
收藏
回复
全部评论(1)
时间顺序
十进制到二进制
#2 回复于2022-01

Warning:
tensor.grad will return the tensor value of the gradient. This is an incompatible upgrade for tensor.grad API. It's return type changes from numpy.ndarray in version 2.0 to paddle.Tensor in version 2.1.0. If you want to get the numpy value of the gradient, you can use :code:`x.grad.numpy()`

 

我运行了你的代码,NOTEBOOK运行通过了,报了个版本的警告。你可以再重启下GPU环境尝试一下,警告的意思是让你把 grad换成grad.numpy()

0
回复
需求/bug反馈?一键提issue告诉我们
发现bug?如果您知道修复办法,欢迎提pr直接参与建设飞桨~
在@后输入用户全名并按空格结束,可艾特全站任一用户