-
Notifications
You must be signed in to change notification settings - Fork 3
Implementation of the NTK in Colibri #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
achiefa
wants to merge
32
commits into
main
Choose a base branch
from
ntk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
25b2bc8
Save parameters of the PDF model to disk
achiefa 5926a45
First implementation of the ntk routine
achiefa 232ae72
Add example runcard
achiefa ac19ea0
restore example runcard
vschutze-alt 658da29
Store parameters in GradientDescentResult
achiefa 959123f
Add new example runcard
achiefa d12ec10
enable ntk computation from a separate dedicated runcard
vschutze-alt 042f1b1
Apply black formatting
achiefa 168cd71
add example runcard to compute ntk
vschutze-alt 75df249
add more options to the runcard, add plotting function
vschutze-alt 0cd0214
Documentation
ecole41 7123ce0
Documentation tree
ecole41 7ad5e46
populate docs
vschutze-alt e007004
remove runcards from example, as they are in the docs
vschutze-alt 6aedfb3
populate docs, plots in pdf format
vschutze-alt 8327586
add ntk analysis plots and docs
vschutze-alt a5a9a58
NTK analysis + plot utilities
achiefa 6cfdc5e
Refactoring ntk analysis
achiefa 8c0226c
mark max_epochs as Optional
achiefa e92df47
Implementing plot utilities for NTK + eigenvectors + refactoring
achiefa d6c6a77
Adding example notebook; shows how to use the NTK funcionalities thro…
achiefa 00b7cc6
Workaround to use nnpdf model oob from n3fit + correct misspelling
achiefa 72e2635
Allow additional kwargs (for layer selector in n3fit see https://gith…
achiefa 7c6161b
Add temporary fix for compatibility with nnpdf fits
achiefa 3138708
Use frozensets instead of plain dictionaries
achiefa 1a551d1
Convert frozenset to dict inside function
achiefa 8ca4175
Add getters and make flavor ordering more explicit + allow NTKutils t…
achiefa df4a23a
Remove unused imports
achiefa 20b16d5
Update __rmatmul__ to match with __matmul__ in NTKStats
achiefa bcac320
Correct spectrum indexing + serialization NTKGrid
achiefa c5edcff
Add reshape method to NTKStats
achiefa 6d8011f
Add ntk module
achiefa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .. _ntk_theory: | ||
|
|
||
| =========================== | ||
| Neural Tangent Kernel (NTK) | ||
| =========================== | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| .. _computing_ntk: | ||
|
|
||
| ===================================== | ||
| Computing Neural Tangent Kernel (NTK) | ||
| ===================================== | ||
|
|
||
| This section describes how to compute and analyse this Neural Tangent Kernel (NTK) | ||
| during the training of a given fit. For information on what the NTK is, please | ||
| refer to the :ref:`NTK theory section <ntk_theory>`. | ||
|
|
||
| The NTK can be computed on any fit where the parameter values have been saved during | ||
| training. To produce these saved parameter values during a fit set the | ||
| `record_parameters` argument to `True` in the fit runcard. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| record_parameters: True | ||
| record_every: 100 | ||
|
|
||
| This above example will save the parameter values every 100 epochs. | ||
|
|
||
| Once the parameter values have been saved, the NTK can be computed using the `compute_ntk` | ||
| action. An example runcard to compute the NTK is shown below. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| meta: | ||
| title: Example NTK determination for a Les Houches fit | ||
| author: Colibri User | ||
| keywords: [example, PDF plots, NTK] | ||
|
|
||
| pdf: {id: "test_ntk_fit"} # generated by the colibri fit | ||
|
|
||
| ntk_plots_settings: | ||
| ntk_plots: True | ||
| n_top_eigenvalues: 10 # Number of top eigenvalues for which evolution is plotted | ||
| y_scale: log # Sets the scale for eigenvalue evolution plot. | ||
| plot_n_epochs: 5 # The number of epochs for which the eigenvector plots will be plotted. | ||
| # These will be equally spaced among all epochs. Default 6. | ||
| plot_n_eigenvectors: [1, 2] # Eigenvector plots will be produced for these. Default 1. | ||
|
|
||
| actions_: | ||
| - compute_ntk | ||
|
|
||
| This runcard can be run with the command | ||
| ``colibri_model_exe compute_ntk.yaml -r replica_n``, where the ``colibri_model_exe`` | ||
| is the Colibri executable, and ``n`` is the replica number for which the NTK | ||
| should be computed. This will produce a folder called ``compute_ntk``. The NTK values | ||
| are stored in npz file format in ``compute_ntk/ntk_replicas/replica_n``. | ||
|
|
||
| ``ntk_plots_settings`` | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| * ``ntk_plots``: | ||
| Whether analytic plots are produced with the ``compute_ntk`` command. For details on | ||
| the plots produced, see the :ref:`analytic NTK plots section below <analytic_ntk_plots>`. | ||
| * ``n_top_eigenvalues``: | ||
| This is the number of top eigenvalues that will be plotted when ``ntk_plots`` is | ||
| ``True``. If a number is not specified, a default of 1 eigenvalue will be plotted. | ||
| * ``x_scale`` / ``y_scale``: | ||
| If set to ``log``, the eigenvalue evolution plot will be produced with a logarithmic | ||
| scale on the respective axis. Otherwise a linear scale will be used. | ||
| * ``plot_n_epochs``: | ||
| The number of epochs for which eigenvector plots will be produced. | ||
| These will be equally spaced among all epochs. Default 6. | ||
| * ``plot_n_eigenvectors``: | ||
| The eigenvectors for which eigenvector plots will be produced. Default 1. | ||
|
|
||
| .. _analytic_ntk_plots: | ||
|
|
||
| Analytic NTK plots | ||
| ================== | ||
| If the flag ``ntk_plots`` is set to ``True``, the following analytic plots will be | ||
| produced and stored in .pdf format in ``compute_ntk/ntk_plots/``. | ||
|
|
||
| * **NTK eigenvalue evolution:** Value of the NTK eigenvalues for increasing number of recorded epochs. | ||
|
|
||
| * **NTK eigenvector evolution:** Eigenvector evolution for a given number of recorded epochs, for all flavours. | ||
|
|
||
| * **Eigenvector heatmaps:** Density of NTK eigenvectors across PDF flavours and x-regions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """ | ||
| colibri.ntk | ||
|
|
||
| NTK (Neural Tangent Kernel) analysis module for colibri. | ||
|
|
||
| This module provides tools for computing and analyzing the Neural Tangent Kernel | ||
| of PDF fits, including eigenvalue/eigenvector computation and plotting utilities. | ||
| """ | ||
|
|
||
| from colibri.ntk.ntkutils import NTKGrid, NTKStats | ||
| from colibri.ntk.eigenvalues import ( | ||
| EigenvalueGrid, | ||
| eigenvalue_grid, | ||
| eigenvalues_ensemble, | ||
| ) | ||
| from colibri.ntk.eigenvector import ( | ||
| EigenvectorGrid, | ||
| eigenvector_grid, | ||
| eigenvectors_ensemble_at_epoch, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| # Abstract interface | ||
| "NTKGrid", | ||
| "NTKStats", | ||
| # Eigenvalue classes and functions | ||
| "EigenvalueGrid", | ||
| "eigenvalue_grid", | ||
| "eigenvalues_ensemble", | ||
| # Eigenvector classes and functions | ||
| "EigenvectorGrid", | ||
| "eigenvector_grid", | ||
| "eigenvectors_ensemble_at_epoch", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @achiefa , thanks for starting this. Just from a quick look, may I suggest to not have this write stuff directly, but rather add things in the GradientDescentResult? This way the writing is delegated to other dedicated functions and you don't need to modify much here. Similarly for the MonteCarloFit class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @LucaMantani, thanks for your comment. Indeed, we have considered this option, which I agree has a more solid design principle. However, I was worried that storing the parameters for all recorded epochs could yield memory issues during training. If instead we use a buffer that is saved on disk and freed at the end of each epoch, then we avoid any potential memory issue. Maybe this is not a problem at all, and we can simply store all parameters in a big array and then add it to
GradientDescentResult.Just to quantify the
problem: For a neural network with 763 parameters (float64), a single array is about 0.01 MB. This is then multiplied by the number of epochs for which we want to save the parameters. For instance, if we have 100 epochs, this adds up to ~1MB for one replica. Again, probably we can afford this in favour of a better code design. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think 1 MB is nothing, we load in memory several Gb due to the data and FK tables. Even if one had a model with 1000 parameters, saving it 1000 times would be 8 Mb. So I would say memory is far from being an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Let's put it in
GradientDescentResultthen.