Compute grads of cloned tensor Pytorch
I am having a hard time with gradient computation using PyTorch. 

I have the outputs and the hidden states of the last time step T of an RNN. 

I would like to clone my hidden states and compute its grad after backpropagation but it doesn't work.  

After reading pytorch how to compute grad after clone a tensor, I used  retain_grad() without any success.

Here's my code

        hidden_copy = hidden.clone()
        hidden.retain_grad()
        hidden_copy.retain_grad()
        outputs_T = outputs[T]
        targets_T = targets[T]
        loss_T = loss(outputs_T,targets_T)
        loss_T.backward()
        print(hidden.grad)
        print(hidden_copy.grad)


hidden_grad gives an array while hidden_copy.grad gives None.

Why does hidden_copy.grad give None ? Is there any way to compute the gradients of a cloned tensor ?

Based on the comments the problem is that hidden_copy is never visited during the backward pass.

When you perform backward pytorch follows the computation graph backwards starting at loss_T and works backwards to all the leaf nodes. It only visits the tensors which were used to compute loss_T. If a tensor isn't part of that backward path then it won't be visited and it's grad member will not be updated. Basically by creating a copy of the tensor and then not using it to compute loss_T this results in a "dead-end" in the computation graph.

To illustrate take a look at this diagram representing a simplified view of a computation graph. Each node in the graph is a tensor where the edges point back to direct descendants.



Notice if we follow the path back from loss_T to the leaves then we never visit hidden_conv. Note that a leaf is a tensor with no descendants and in this case input is the only leaf.

This is an extremely simplified computation graph used to demonstrate a point. Of course in reality there are probably many more nodes between input and hidden and between hidden and output_T as well as other leaf tensors since the weights of layers are almost certainly leaves.
