{KFAC,EKFAC,Diagonal,Implicit} Fisher Matrices and finite width NTKs in PyTorch
Project description
NNGeometry
NNGeometry allows you to:
- compute Gauss-Newton or Fisher Information Matrices (
FIMandFIM_MonteCarlo), as well as any matrix that is written as the covariance of gradients w.r.t. parameters, using efficient approximations such as low-rank matrices, KFAC, EKFAC, diagonal and so on. Some of these representations also work for hessians (Hessian). - compute finite-width Neural Tangent Kernels evaluated on a set of examples (
GramMatrix), even for multiple output functions. - compute per-examples jacobians of the loss w.r.t network parameters (
Jacobian), or of any function such as the network's output. - easily and efficiently compute linear algebra operations involving these matrices regardless of their approximation.
- compute implicit operations on these matrices, that do not require explicitely storing large matrices that would not fit in memory.
It offers a high level abstraction over the parameter and function spaces described by neural networks. As a simple example, a parameter space vector PVector actually contains weight matrices, bias vectors, or convolutions kernels of the whole neural network (a set of tensors). Using NNGeometry's API, performing a step in parameter space (e.g. an update of your favorite optimization algorithm) is abstracted as a python addition: w_next = w_previous + epsilon * delta_w.
Example
In the Elastic Weight Consolidation continual learning technique, you want to compute $\left(\mathbf{w}-\mathbf{w}_{A}\right)^{\top}F\left(\mathbf{w}-\mathbf{w}_{A}\right)$. It can be achieved with a diagonal approximation for the FIM using:
F = FIM(model=model,
loader=loader,
representation=PMatDiag)
regularizer = F.vTMv(w - w_a)
The first statement instantiates a diagonal matrix, and populates it with the diagonal coefficients of the FIM of the model model computed using the examples from the dataloader loader.
If diagonal is not sufficiently accurate then you could instead choose a KFAC approximation, by just changing PMatDiag to PMatKFAC in the above. Note that it internally involves very different operations, depending on the chosen representation (e.g. KFAC, EKFAC, ...).
Documentation
You can visit the documentation at https://nngeometry.readthedocs.io.
More example usage are available in the repository https://github.com/tfjgeorge/nngeometry-examples.
Feature requests, bugs, contributions, or any kind of request
You are now many who are using NNGeometry in your work: do not hesitate to drop me a line (tfjgeorge@gmail.com) about your project so that I have a better understanding of your use cases or the current limitations of the library.
We welcome any feature request or bug report in the issue tracker.
We also welcome contributions, please submit your PRs!
Citation
If you use NNGeometry in a published project, please cite our work using the following bibtex entry
@software{george_nngeometry,
author = {Thomas George},
title = {{NNGeometry: Easy and Fast Fisher Information
Matrices and Neural Tangent Kernels in PyTorch}},
month = feb,
year = 2021,
publisher = {Zenodo},
version = {v0.3},
doi = {10.5281/zenodo.4532597},
url = {https://doi.org/10.5281/zenodo.4532597}
}
License
This project is distributed under the MIT license (see LICENSE file). This project also includes code licensed under the BSD 3 clause as it borrows some code from https://github.com/owkin/grad-cnns.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nngeometry-0.4.tar.gz.
File metadata
- Download URL: nngeometry-0.4.tar.gz
- Upload date:
- Size: 957.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce441ba2e1dba60b494b00698eb986409e3fcb26b02699e23e508261ebd691ed
|
|
| MD5 |
0620c63b7e692e92f3406166cce12c04
|
|
| BLAKE2b-256 |
faafacedd531bfe195998e589311cbff4b0c559ffebfbef51101f15c9180c7a0
|
Provenance
The following attestation bundles were made for nngeometry-0.4.tar.gz:
Publisher:
release.yml on tfjgeorge/nngeometry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nngeometry-0.4.tar.gz -
Subject digest:
ce441ba2e1dba60b494b00698eb986409e3fcb26b02699e23e508261ebd691ed - Sigstore transparency entry: 196095824
- Sigstore integration time:
-
Permalink:
tfjgeorge/nngeometry@0ba0636a13674f3d77f6f8a92798bb47d28329e1 -
Branch / Tag:
refs/tags/v0.4 - Owner: https://github.com/tfjgeorge
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0ba0636a13674f3d77f6f8a92798bb47d28329e1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file nngeometry-0.4-py3-none-any.whl.
File metadata
- Download URL: nngeometry-0.4-py3-none-any.whl
- Upload date:
- Size: 34.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e397827c16bce93d69817a8f5635543bafa9766855761cdaa3559f7060927697
|
|
| MD5 |
6bc3a2b789df05913b996c8aa961e105
|
|
| BLAKE2b-256 |
e4966d9ef1286ada9e9343c7a4111c9885b1ee2fcd6da3316cafdee872f3a565
|
Provenance
The following attestation bundles were made for nngeometry-0.4-py3-none-any.whl:
Publisher:
release.yml on tfjgeorge/nngeometry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nngeometry-0.4-py3-none-any.whl -
Subject digest:
e397827c16bce93d69817a8f5635543bafa9766855761cdaa3559f7060927697 - Sigstore transparency entry: 196095825
- Sigstore integration time:
-
Permalink:
tfjgeorge/nngeometry@0ba0636a13674f3d77f6f8a92798bb47d28329e1 -
Branch / Tag:
refs/tags/v0.4 - Owner: https://github.com/tfjgeorge
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0ba0636a13674f3d77f6f8a92798bb47d28329e1 -
Trigger Event:
release
-
Statement type: