title: PyTorch second derivative is zero everywhere
body: <p>I'm working on a custom loss function that requires me to compute the trace of the hessian matrix of a neural network with respect to its inputs.</p>
  <p>In one dimension, this reduces to the second derivative of the network with respect to its input. Suppose my network is <code>u</code> and my input is <code>x</code>. The problem is completely one-dimensional, so the relationship I'm trying to model is <code>u(x)</code> where <code>u</code> has one input and one output, and <code>x</code> is one-dimensional. However, I'm running with batches, so <code>x</code> &quot;comes in&quot; as a column vector and I'm expecting a column vector as output.</p>
  <p>If we label the samples in the batch as <code>x_1, x_2, ..., x_n</code>, I'm thus interested in the following vectors:</p>
  <p><a href="https://i.stack.imgur.com/NU4qZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NU4qZ.png" alt="enter image description here" /></a> <code>   </code> <a href="https://i.stack.imgur.com/63qKc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/63qKc.png" alt="enter image description here" /></a></p>
  <p>In PyTorch, I have tried the following:</p>
  <pre><code>u = model(x)
  d = grad(u, x, grad_outputs=torch.ones_like(u), create_graph=True)[0]
  dd = grad(d, x, grad_outputs=torch.ones_like(d), retain_graph=True, create_graph=False)[0]
  </code></pre>
  <p>This works well for u' but u'' comes out as being zero everywhere:</p>
  <p><a href="https://i.stack.imgur.com/GuTUv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GuTUv.png" alt="enter image description here" /></a></p>
  <p>I'm a bit confused on the whole notion of needing a vector jacobian product here to begin with. Should I view the computation as <code>u</code> mapping from <code>R^n</code> to <code>R^n</code>, where <code>n</code> is the size of my batch?</p>
  <p>Any help is appreciated!</p>