Skip to content

Commit 5647c68

Browse files
authored
Implement effective rank calculation in effective_dim.py (#153)
Adds a function to calculate the effective rank of a code matrix using Shannon entropy.
1 parent b8015fc commit 5647c68

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

ngclearn/utils/analysis/effective_dim.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ def participation_ratio(latent_codes):
2020

2121
return tr2_cov / cov2_tr if cov2_tr > 0 else float("nan")
2222

23+
24+
25+
26+
def rankme(Z, eps=1e-7):
27+
"""
28+
Calculates the effective rank of for a code matrix Z
29+
effective rank = exp(Shannon entropy), from Garrido, Balestriero,
30+
Najman & LeCun, "RankMe: Assessing the Downstream Performance of Pretrained
31+
Self-Supervised Representations by Their Rank" (ICML 2023, arXiv:2210.02885).
32+
33+
Args:
34+
latent_codes: a set of (N x D) latent code vectors (one row per vector code)
35+
36+
Returns:
37+
scalar measurement of the effective dimension
38+
"""
39+
40+
singular_values = jnp.linalg.svd(Z, compute_uv=False) ## singular values of Z
41+
sum_singular_vals = jnp.sum(singular_values) ## L1
42+
if sum_singular_vals <= 0:
43+
return float("nan")
44+
p = singular_values / sum_singular_vals + eps ## L1-normalized singular value
45+
shannon_entropy = -jnp.sum(p * jnp.log(p)) ## Shannon entropy
46+
return jnp.exp(shannon_entropy) ## exp(Shannon entropy) = effective rank

0 commit comments

Comments
 (0)