Hello! I want to take the derivate of the jacobian using pytorch, but it seems like I am doing the wrong thing. Here is part of my code:
x_1 = np.arange(1,4,1)
x_1 = torch.from_numpy(x_1).reshape(len(x_1),1)
x_1 = x_1.float()
x_2 = np.arange(1,4,1)
x_2 = torch.from_numpy(x_2).reshape(len(x_2),1)
x_2 = x_2.float()
x = torch.cat((x_1,x_2),1)

x.requires_grad = True

w1 = nn.Linear(2, 2, bias=False)

y = w1(x)

def jacobian(inputs, outputs):
    return torch.stack([torch.autograd.grad([outputs[:, i].sum()], [inputs], create_graph=True)[0]
                        for i in range(outputs.size(1))], dim=-1)
jac = jacobian(x,y)

So at this point jac contains the jacobian matrix of my system, which works just fine. But I want to take the derivative of jac with respect to x (basically second order partial derivatives). But when I do this: d_jac = jacobian(x,jac) I get this error: RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior. I am not sure why, as obviously jac is derived from y, so it should still depend on x. Can someone help me? Thank you!