I am trying to get the gradients of two losses in the following code snippet but all I get is None (AttributeError: ‘NoneType’ object has no attribute ‘data’)
Also the gradient computed by the backward pass is for the model parameters (generally Tensors you create with requires_grad=True). So here you will get gradients for x_prime for example. Not the loss.
Thank you for your reply. I added l_target_loss.retain_grad() before calling l_target_loss.backward(retain_graph= True) and it worked. I can now get values for target_grad and l_argmax_grad respectively. However, both values are thesame. They are both 1’s. I feel something isn’t right
@albanD , I feel that I’m doing something wrong. What I actually want is the gradient of the target_loss with respect to the input (x) and gradient of the l_argmax_loss with respect to the input (x). The input x is img in the code snippet above. How do I go about it please?