Skip to content

Commit

Permalink
TransRate script
Browse files Browse the repository at this point in the history
  • Loading branch information
Tech-Netiums committed Nov 16, 2022
1 parent 6f69981 commit 4cc0d91
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tllib/ranking/transrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
@author: Louis Fouquet
@contact: [email protected]
"""
import numpy as np


__all__ = ['transrate']

def coding_rate(features : np.ndarray, eps = 1e-4):
f = features
n, d = f.shape
(_, rate) = np.linalg.slogdet((np.eye(d) + 1 / (n * eps) * f.transpose() @ f))
return 0.5 * rate


def transrate(features: np.ndarray, labels: np.ndarray, eps = 1e-4):
r"""
TransRate in `Frustratingly easy transferability estimation (ICML 2022)
<https://proceedings.mlr.press/v162/huang22d/huang22d.pdf>`_.
The TransRate :math:`TrR` can be described as:
.. math::
TrR= R\left(f, \espilon \right) - R\left(f, \espilon \mid y \right)
where :math:`f` is the features extracted by the model to be ranked, :math:`y` is the groud-truth label vector,
:math:`R` is the coding rate with distortion rate :math:`\epsilon`
Args:
features (np.ndarray):features extracted by pre-trained model.
labels (np.ndarray): groud-truth labels.
eps (float, optional): distortion rare (Default: 1e-4)
Shape:
- features: (N, F), with number of samples N and feature dimension F.
- labels: (N, ) elements in [0, :math:`C_t`), with target class number :math:`C_t`.
- score: scalar.
"""
f = features
y = labels
f = f - np.mean(f, axis = 0, keepdims = True)
Rf = coding_rate(f, eps)
Rfy = 0.0
C = int(y.max() + 1)
for i in range(C):
Rfy += coding_rate(f[(y==i).flatten()], eps)
return Rf - Rfy / C

0 comments on commit 4cc0d91

Please sign in to comment.