I'm trying to implement a simple minimizer in PyTorch, here is the code:
for i in range(10):
print('i =', i, ' q =', q)
v_trans = transform_dq(v, q)
loss = mse(v_trans, v_target)
loss.backward()
q = q - eta * q.grad
print('Final q = ', q)
Where v and q are tensors, and eta = 0.01.
The problem is that at the line q = q - eta * q.grad
I get an error on the second iteration of the loop:
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
It looks like the update for q changes the graph in an unwanted way (q is not a leaf of the graph anymore and hence it doesn't have a grad). If that is the case, then how to implement this simple minimizer?
First, you need to reset q
's gradients before each iteration.
Second, you should update q
outside "gradient scope":
with torch.no_grad():
q = q - eta * q.grad