Skip to content

Commit abdae59

Browse files
KonstiNikknikolaou
and
knikolaou
authored
Put gram matrix normalization into entropy computation. (#7)
Co-authored-by: knikolaou <[email protected]>
1 parent 4290300 commit abdae59

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

papyrus/measurements/measurements.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def __init__(
315315
rank: int = 1,
316316
normalize_eigenvalues: bool = True,
317317
effective: bool = False,
318+
normalize_matrix: bool = False,
318319
):
319320
"""
320321
Constructor method of the NTKCrossEntropy class.
@@ -332,11 +333,15 @@ def __init__(
332333
effective : bool (default=False)
333334
If true, the entropy is divided by the theoretical maximum entropy of
334335
the system thereby returning the effective entropy / entropy density.
335-
336+
normalize_matrix : bool (default=False)
337+
If true, the NTK is normalized by the square root of the product of the
338+
corresponding diagonal elements. This is equivalent to normalizing the
339+
gradient vectors forming the NTK.
336340
"""
337341
super().__init__(name=name, rank=rank)
338342
self.normalize_eigenvalues = normalize_eigenvalues
339343
self.effective = effective
344+
self.normalize_matrix = normalize_matrix
340345

341346
def apply(self, ntk: np.ndarray) -> np.ndarray:
342347
"""
@@ -373,6 +378,7 @@ def apply(self, ntk: np.ndarray) -> np.ndarray:
373378
ntk,
374379
effective=self.effective,
375380
normalize_eig=self.normalize_eigenvalues,
381+
normalize_matrix=self.normalize_matrix,
376382
)
377383

378384

papyrus/utils/analysis_utils.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323

2424
import numpy as np
2525

26-
from papyrus.utils.matrix_utils import compute_hermitian_eigensystem
26+
from papyrus.utils.matrix_utils import (
27+
compute_hermitian_eigensystem,
28+
normalize_gram_matrix,
29+
)
2730

2831

2932
def compute_trace(matrix: np.ndarray, normalize: bool = False) -> np.ndarray:
@@ -77,7 +80,10 @@ def compute_shannon_entropy(dist: np.ndarray, effective: bool = False) -> float:
7780

7881

7982
def compute_von_neumann_entropy(
80-
matrix: np.ndarray, effective: bool = True, normalize_eig: bool = True
83+
matrix: np.ndarray,
84+
effective: bool = True,
85+
normalize_eig: bool = True,
86+
normalize_matrix: bool = False,
8187
) -> float:
8288
"""
8389
Compute the von-Neumann entropy of a matrix.
@@ -91,12 +97,19 @@ def compute_von_neumann_entropy(
9197
the system thereby returning the effective entropy.
9298
normalize_eig : bool (default = True)
9399
If true, the eigenvalues are scaled to look like probabilities.
100+
normalize_matrix : bool (default=False)
101+
If true, the NTK is normalized by the square root of the product of the
102+
corresponding diagonal elements. This is equivalent to normalizing the
103+
gradient vectors forming the NTK.
94104
95105
Returns
96106
-------
97107
entropy : float
98108
Von-Neumann entropy of the matrix.
99109
"""
110+
if normalize_matrix:
111+
matrix = normalize_gram_matrix(matrix)
112+
100113
eigvals, _ = compute_hermitian_eigensystem(matrix, normalize=normalize_eig)
101114

102115
entropy = compute_shannon_entropy(eigvals)

0 commit comments

Comments
 (0)