Solved by ptrblck in post #2 
              
              
                As the warning explains you are trying to access the .grad attribute of a non-leaf tensor, which won’t be populated by default. 
You can call .retain_grad() on the non-leaf Tensor to access the gradient after the backward call. 
Also note that the usage of the .data attribute is not recommended, as …