PyHessian icon indicating copy to clipboard operation
PyHessian copied to clipboard

RuntimeError: element 0 of variables does not require grad and does not have a grad_fn

Open taeheej opened this issue 3 years ago • 2 comments

In hessian.py When I tried to run the following code, Hv = hessian_vector_product(gradsH, params, v)

The error comes out. Is there any idea how to resolve this issue?

taeheej avatar Dec 14 '21 01:12 taeheej

Instead of loss.backward(create_graph=True), which is called before hessian_vector_product use torch.autograd.grad(...). I am using Pytorch 1.10.0 and it works. (Also dont forget to filter out unused parameters-grads)

petrgeiger-incieve avatar May 31 '22 10:05 petrgeiger-incieve

I can confirm that the workaround suggested by @petrgeiger-incieve seems to be working on my end. I am using PyTorch 1.12 btw.

alvaro-budria avatar May 27 '23 07:05 alvaro-budria