Skip to main content

{KFAC,EKFAC,Diagonal,Implicit} Fisher Matrices and finite width NTKs in PyTorch

Project description

NNGeometry

Build Status codecov DOI PyPI version

NNGeometry allows you to:

  • compute Gauss-Newton or Fisher Information Matrices (FIM and FIM_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

nngeometry-0.4.tar.gz (957.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nngeometry-0.4-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

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

Hashes for nngeometry-0.4.tar.gz
Algorithm Hash digest
SHA256 ce441ba2e1dba60b494b00698eb986409e3fcb26b02699e23e508261ebd691ed
MD5 0620c63b7e692e92f3406166cce12c04
BLAKE2b-256 faafacedd531bfe195998e589311cbff4b0c559ffebfbef51101f15c9180c7a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for nngeometry-0.4.tar.gz:

Publisher: release.yml on tfjgeorge/nngeometry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for nngeometry-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e397827c16bce93d69817a8f5635543bafa9766855761cdaa3559f7060927697
MD5 6bc3a2b789df05913b996c8aa961e105
BLAKE2b-256 e4966d9ef1286ada9e9343c7a4111c9885b1ee2fcd6da3316cafdee872f3a565

See more details on using hashes here.

Provenance

The following attestation bundles were made for nngeometry-0.4-py3-none-any.whl:

Publisher: release.yml on tfjgeorge/nngeometry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page