
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)
x.grad.zero_()
