title: How to differentiate a gradient in Pytorch
body: <p>I'm trying to differentiate a gradient in PyTorch. I found <a href="https://stackoverflow.com/questions/49149699/differentiate-gradients">this</a> link but can't get it to work.</p>
  <p>My code looks as follows:</p>
  <pre><code>import torch
  from torch.autograd import grad
  import torch.nn as nn
  import torch.optim as optim
  
  class net_x(nn.Module): 
          def __init__(self):
              super(net_x, self).__init__()
              self.fc1=nn.Linear(2, 20) 
              self.fc2=nn.Linear(20, 20)
              self.out=nn.Linear(20, 4) 
  
          def forward(self, x):
              x=self.fc1(x)
              x=self.fc2(x)
              x=self.out(x)
              return x
  
  nx = net_x()
  r = torch.tensor([1.0,2.0])
  nx(r)
  &gt;&gt;&gt;tensor([-0.2356, -0.7315, -0.2100, -0.6741], grad_fn=&lt;AddBackward0&gt;)
  </code></pre>
  <p>But when I try to differentiate the function with respect to the first parameter</p>
  <pre><code>grad(nx, r[0])
  </code></pre>
  <p>I get the error</p>
  <pre><code>TypeError: 'net_x' object is not iterable
  </code></pre>
  <p><strong>Update</strong>
  Trying to extend this to tensors:<br />
  For some reason the gradient is the same for all inputs.</p>
  <pre><code>a = torch.rand((8,2), requires_grad=True)
  s = []
  s_t = []
  for input_tensor in a:
      output_tensor = nx(input_tensor)
      s.append(output_tensor[0])
      s_t_value = grad(output_tensor[0], input_tensor)[0][0]
      s_t.append(s_t_value)
  print(s_t)
  </code></pre>
  <p>But the output is:</p>
  <pre><code>[tensor(-0.1326), tensor(-0.1326), tensor(-0.1326), tensor(-0.1326), tensor(-0.1326), tensor(-0.1326), tensor(-0.1326), tensor(-0.1326)]
  </code></pre>