I want to compute the gradient of a network with respect to its inputs. The network takes a 1-feature size-N batch of shape (N,1) and returns an output of the same shape.
If I pass in a (1,1) input inp, I can do it like this:
out = net(inp)
dNetdInp = torch.autograd.grad(out, inp, create_graph=True)[0]

But what if my input is (N,1)? If I try the same thing, I get a RuntimeError: grad can be implicitly created only for scalar outputs. What I want is a (N,1) tensor of the gradients, with the ith element in the tensor corresponding to the gradient of the net with respect to the ith input. How might I do this elegantly?