在BP的时候,pytorch是将Variable的梯度放在Variable对象中的,我们随时都可以使用Variable.grad得到对应Variable的grad。刚创建Variable的时候,它的grad属性是初始化为0.0的。
>>> import torch
>>> from torch.autograd import Variable
>>> w1 = Variable(torch.Tensor([1.0,2.0,3.0]),requires_grad=True)#需要求导的话,requires_grad=True属性是必须的。
>>