Solved by ptrblck in post #2 
              
              
                You could probably use grads = torch.autograd.grads which would return the gradients directly instead of using reg.backwar().