Solved by ptrblck in post #2 
              
              
                PyTorch’s autograd is able to compute the gradients for most of the functions. 
Have a look at the Autograd Tutorial. 
It’s a nice explanation, how autograd works. Also, your code snippet is missing!