xiaobaoonline / pytorch-in-action

MIT License
173 stars 79 forks source link

2.4.2 Variable简介代码更新 #4

Open JimXiongGM opened 5 years ago

JimXiongGM commented 5 years ago

应该更新为:

from torch import tensor x = torch.rand(4) x = x.clone().detach().requiresgrad(True) y= x * 3 grad_variables = torch.FloatTensor([1,2,3,4]) y.backward(grad_variables) x.grad tensor([ 3., 6., 9., 12.])

jiangyangbo commented 5 years ago

谢谢,原来代码是0.3版本的。

from torch import tensor import torch x = torch.rand(4, requires_grad=True) y= x * 3 grad_variables = torch.FloatTensor([1,2,3,4]) y.backward(grad_variables) print(x.grad)