导数
这段代码揭示了多个变量的微分以及如何求解loss为向量的导数
m1 = Variable(torch.ones((3,2)), requires_grad=True)
m2 = Variable(torch.ones((3,2))*2, requires_grad=True)
m3 = Variable(torch.ones((3,2))*4, requires_grad=True)
x1 = m1*m2
x2 = x1 *m3
y = x1 + x2
gradients= torch.ones((3,2))
y.backward(gradients)
print(f"m1 grad:{m1.grad}, \n m2 grad:{m2.grad}, \n m3 grad:{m3.grad}, \n x1 grad:{x1.grad}, \n x2 grad:{x2.grad}, \n y grad:{y.grad}")m1 = Varia