Solved by albanD in post #2 
              
              
                Hi, 
First, You seem to assume that N is a batch size that should be considered specially? But the autograd does not know about that. It will compute the gradient considering N as any other dimension. 
Also autograd.grad() does a vector jacobian product (when grad_outputs is provided) and so will re…