已解决
自动求导,计算图示意图及pytorch实现
来自网友在路上 162862提问 提问时间:2023-10-10 20:38:30阅读次数: 62
最佳答案 问答题库628位专家为你答疑解惑
pytorch实现
x1 = torch.tensor(3.0, requires_grad=True)
y1 = torch.tensor(2.0, requires_grad=True)
a = x1 ** 2
b = 3 * a
c = b * y1
c.backward()
print(x1.grad)
print(y1.grad)
print(x1.grad == 6 * x1 * y1)
print(y1.grad == 3 * (x1 ** 2))
输出为:
tensor(36.)
tensor(27.)
tensor(True)
tensor(True)
默认情况下,pytorch会累加梯度,每次backward()前,需要进行梯度清零
x.grad.zero_()
查看全文
99%的人还看了
猜你感兴趣
版权申明
本文"自动求导,计算图示意图及pytorch实现":http://eshow365.cn/6-18547-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!