设置变量x1, x2, x3
设置函数
z = x1 * x2 + x3
并且对x1进行求导,所以求出的结果因该为x2
requires_grad=True //设置变量是可以求导的
z.backward()//是z进行反向传播
retain_graph = True//可以进行多次的求导
x1 = torch.randn(1, requires_grad=True)
print("x1", x1)
x2 = torch.randn(1, requires_grad=True)
print("x2", x2)
x3 = torch.randn(1, requires_grad=True)
print("x3", x3)
y = x1*x2
z = y+x3
print(z.requires_grad)
print('z', z)
# 梯度如果不清空,每次循环就会累加
z.backward()
# print(y.grad)
print(x1.grad)
x1 = torch.randn(1, requires_grad=True)
print("x1", x1)
x2 = torch.randn(1, requires_grad=True)
print("x2", x2)
x3 = torch.randn(1, requires_grad=True)
p