Hi all,
I’m a newbie of pytorch and I like it a lot! But I have a problem with gradient of re-used variable
here is my code:
import torch as t
from torch.autograd import Variable as v
step =2
x0 = t.FloatTensor([2, 1]).view(1, 2)
x0 = v(x0, requires_grad=True)
for _ in range(step):
x2 = t.FloatTensor([[1, 2], [1, 2]])
x2 = v(x2, requires_grad=True)
x0 = t.mm(x0, x2)
y = v(t.FloatTensor([[1, 2], [3, 4]]))
z = t.mm(x0, y)
z.backward(t.FloatTensor([[1, 1]]))
print(x2.grad)
print(x0.grad)
is gradient of x2 accumulated by every step? why gradient of x0 is None? and how can we get gradient of x0 for every step? Thanks a lot!