Hi albanD. Thank you for your reply.



 albanD:

In your first example, you function is 2D -> 2D. So the Jacobian is a 2x2 matrix. You thus need to provide the vector by which it should be multiplied. Since your Jacobian here is diagonal, you can give [1., 1.] as you did to get that diagonal.


Isn’t the f = 2*x a 1D tensor, since x is 1D? The Jacobian is then also 1D, i.e. J = tensor([2, 2]). So what is returned is J*grad_outputs where grad_outputs=torch.tensor([1.0, 1.0]), i.e. tensor([2., 2.]).
For a 2D case, e.g. J being a 2x2 tensor, we would have torch.mm(J, grad_outputs) (instead of simple multiplication) where grad_outputs=torch.tensor([[1.0, 1.0], [1.0, 1.0]]). Am I right?