Hello. I was also working on the implementation of Jacobian in these days. The new pytorch API for Jacobian seems not a good choice, because its source code shows that this algorithm is still based on for loops. There is a package named backpack which implements the Jacobian in pytorch.
Here is the paper https://arxiv.org/pdf/1912.10985.pdf
and their website http://backpack.pt/
In fig.3 of that paper they show that the calculation of jacobian is as fast as a single backward pass, and of course much faster than for loops.
Thank you @ChenAo-Phys indeed the current implementation of the jacobian is not very efficient in pytorch, and most likely it will have the same performance of our already implemented for loop...
Would you be willing to do a PR to include this in our Torch machine?
Yes I can add this into Torch machine. I did a test today. The calculation of Jacobian in backpack is really efficient and consumes much less time compared with other parts like iterative solver.
@ChenAo-Phys just an implementation note: I saw that backpack does not support many layers (essentially it supports only 2 types of layers...) so I wouldn't make this the default setting to compute der_log in our machine, but maybe some opt-in solution for the user
@gcarleo I'm sorry that I only tested with Conv2d and didn't notice this problem. I agree that backpack should not be the default method. Or maybe we can judge whether to use backpack automatically, like
@gcarleo Based on the idea of backpack, I improve it to accommodate most pytorch layers including nn.Linear, nn.Conv1d/2d/3d, ... https://github.com/ChenAo-Phys/pytorch-Jacobian
This may be a better solution for the current problem.
Very nice @ChenAo-Phys , (and I guess the people developing backpack might be also interested in integrating this in their code). It would be good if you could do a PR to netket at some point (again, I would do this as an opt-in feature, i.e. use this fast jacobian only if your library is found installed? )
@gcarleo The library is no more than 200 lines so I think it's better to add it directly into netket/machine/torch.py to avoid the trouble of installing my library for users.
The problem is that sometimes the network is not written in a standard form to make my method work. I will do an automatic check and deactivate my method if that happens.
@gcarleo I ignored a problem in my method - it's too memory-consuming. It will probably fail in PC if one wants to calculate the Jacobian of a network with more than 3000 parameters. It's really a pity. Now I think it's still better to use backpack if it's checked to be importable.