I am trying to return the gradient of an output score w.r.t. the input image. While I am setting requires_grad()=True, the gradient is None. Here are two equivalent codes to get the gradients, Code1 raises the error RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior, and Code2 returns None. Does anybody have some ideas about what I missed?
assuming you are not detaching the computation graph.
If case you get an error after removing d_x.requires_grad_() then check where the computation graph is detached in your model (e.g by rewrapping an intermediate activation into a new tensor).
I loaded the model state_dict a line before net_d.eval():
‘net_d.load_state_dict(torch.load(PATH))’
Removing d_x.requires_grad_() raises error ‘element 0 of tensors does not require grad and does not have a grad_fn’. Shoud I calculate the saliency map from a model which weights are loaded by a saved state_dict weights in another way?
No, loading the state_dict should work and the error points to a detached computation graph.
Could you post the model definition as well as the input shape you are using so that I could reproduce the issue, please?