title: Getting the output's grad with respect to the input
body: <p>I'm currently trying to implement an ODE Solver with Pytorch, my solution requires computing the gradient of each output wtr to its input.</p>
  <pre><code>y = model(x)
  
  for i in range(len(y)): #compute output grad wrt input
         y[i].backward(retain_graph=True)
      
  ydx=x.grad 
  </code></pre>
  <p>I was wondering if there is a more elegant way to compute the gradients for each output in the batch, since the code gets messy for higher order ODEs and PDEs.
  I tried using:</p>
  <pre><code>torch.autograd.backward(x,y,retain_graph=True)
  </code></pre>
  <p>without much success.</p>