问题 I am looking for the most efficient way to get the Jacobian of a function through Pytorch and have so far come up with the following solutions: def func(X): return torch.stack(( X.pow(2).sum(1), X.pow(3).sum(1), X.pow(4).sum(1) ),1) X = Variable(torch.ones(1,int(1e5))*2.00094, requires_grad=True).cuda() # Solution 1: t = time() Y = func(X) J = torch.zeros(3, int(1e5)) for i in range(3): J[i] = grad(Y[0][i], X, create_graph=True, retain_graph=True, allow_unused=True)[0] print(time()-t) Output: