PyHessian icon indicating copy to clipboard operation
PyHessian copied to clipboard

edits for PINN/custom loss functions

Open rmojgani opened this issue 2 years ago • 0 comments

How can I use the code for the case where I have a custom loss function, and not a torch.nn."LOSS"? I'm specifically trying to reproduce the results in https://arxiv.org/abs/2109.01050 , and cannot get the PyHessian class to work.

Also, in the PINN case, network does not have a data=(inputs, targets), can I use the same code/class or I need further edits?

I'm thinking of changing the loss in line 92 https://github.com/amirgholami/PyHessian/blob/1a42737552d0879b115789e4af577f821a913e0c/pyhessian/hessian.py#L92 with my custom loss (from the network)

wondering if that's all the changes I need


PS: Also since I'm only interested in the eigenvectors, tried to exctract parameters and gradients, i.e.,
https://github.com/amirgholami/PyHessian/blob/1a42737552d0879b115789e4af577f821a913e0c/pyhessian/utils.py#L61 and pass it to https://github.com/amirgholami/PyHessian/blob/1a42737552d0879b115789e4af577f821a913e0c/pyhessian/hessian.py#L111

Though it's throwing some error at me, would appreciate any insights,

`Traceback (most recent call last):

File "/tmp/ipykernel_709492/339026100.py", line 1, in hv = torch.autograd.grad(gradsH,

File "/home/xxx/.local/lib/python3.8/site-packages/torch/autograd/init.py", line 229, in grad grad_outputs_ = make_grads(outputs, grad_outputs)

File "/home/xxx/.local/lib/python3.8/site-packages/torch/autograd/init.py", line 33, in _make_grads if not out.shape == grad.shape:

AttributeError: 'float' object has no attribute 'shape'`

rmojgani avatar Mar 28 '22 04:03 rmojgani