I’m trying to get gradients of the loss function with respect to some variables in my code. I’m having trouble understanding why I can access the gradients for some variables and why not for some others.Here is a part of my code:
pert=I+delta
pert=torch.clamp(pert,0,1)
I=Variable(I.to(device), requires_grad = True)
delta=Variable(delta.to(device), requires_grad = True)
pert=Variable(pert.to(device), requires_grad = True)
outputs = net(pert)
targets=targets.long()
loss = criterion(outputs, targets)
loss.backward()
grad=pert.grad.data
graddel=delta.grad.data
gradI=I.grad.data
I can get the gradients for grad, but for graddel and gradI I get the error:
‘NoneType’ object has no attribute ‘data’
Any ideas?