如何使用backward求梯度
收藏
在线环境中使用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
收藏
请登录后评论
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()