Thanks for your reply @alexis-jacq .
I tried your code, but still get unexpected result on my machine (pytorch0.4):

image.png1101×307 25.9 KB

Besides, after autograd.grad() is called, x.grad is None.
However, gradients must have been accumulated somewhere somehow during the calculation, but I don’t know how it is done (my second example in the original post is more tricky to find out how gradient is accumulated).
I know torch.autograd.backward() will accumulate gradients, should autograd.grad() also accumulate gradients? Apparently it does during the call, but the gradients won’t be save to variables.
Besides, my original goal of using autograd.grad is to calculate Hessian matrix (not a Hessian vector: https://github.com/pytorch/pytorch/blob/master/test/test_autograd.py#L195 4). Any suggestion on this is appreciated.