reptrix package

Submodules

reptrix.alpha module

reptrix.alpha.get_alpha(activations: Tensor, max_eigenvals: int = 2048, fit_range: ndarray = None) tuple[source]

Get alpha and powerlaw fit (https://proceedings.neurips.cc/paper_files/paper/2022/hash/70596d70542c51c8d9b4e423f4bf2736-Abstract-Conference.html)

Parameters:
  • activations (np.ndarray) – Activation tensor of shape (bsz,d1,d2…dn)

  • max_eigenvals (int, optional) – Maximum #eigenvalues to compute. Defaults to 2048.

  • fit_range (np.ndarray, optional) – Range to fit the powerlaw. Defaults to np.arange(10,100).

Returns:

Result of size 4

alpha: powerlaw decay coefficient ypred: Powerlaw fit fit_R2: goodness of powerlaw fit fit_R2_100: goodness of powerlaw fit (computed till the 100 eigvals)

Return type:

tuple

reptrix.alpha.get_powerlaw(eigen: ndarray, trange: ndarray) tuple[source]

Fit powerlaw and return decay, powerlaw fit and the goodness of fit

Parameters:
  • eigen (np.ndarray) – Eigenspectrum of activation covariance matrix

  • trange (np.ndarray) –

    Range to fit the powerlaw. Tip: Ignore the first couple of eigenvalues

    because we want to fit the tail of the spectrum

Returns:

Result of size 4

alpha: powerlaw decay coefficient ypred: Powerlaw fit fit_R2: goodness of powerlaw fit fit_R2_100: goodness of powerlaw fit (computed till the 100 eigvals)

Return type:

tuple

reptrix.alpha.plot_powerlaw(eigenspectrum: ndarray, alpha_res: tuple) None[source]

Plot eigenspectrum and powerlaw fit

Parameters:
  • eigenspectrum (np.ndarray) – Eigenspectrum of activation covariance matrix

  • alpha_res (tuple) – Tuple containing alpha, powerlaw fit, goodness of powerlaw fit, goodness of powerlaw fit for first 100 eigenvalues

reptrix.lidar module

reptrix.lidar.get_lidar(activations: Tensor, num_samples: int, num_augs: int, del_sigma_augs: float = 1e-06, max_eigenvals: int = 2048) float[source]

Get RankMe metric (https://openreview.net/forum?id=f3g5XpL9Kb)

Parameters:
  • activations (torch.Tensor) – Activation tensor of shape either (num_samples, num_augs, d1,d2…dn) or (num_samples * num_augs, d1,d2…dn)

  • num_samples (int) – Number of unique inputs/samples

  • num_augs (int) – Number of augmentations for each input used to estimate object manifold

  • del_sigma_augs (float, optional) – A small positive constant that is added to make sure matrices are invertible. Defaults to 1e-6.

  • max_eigenvals (int, optional) – Maximum #eigenvalues to compute. Defaults to 2048.

Returns:

LiDAR metric

Return type:

float

reptrix.lidar.get_rank(eigen: ndarray) float[source]

Get effective rank of the LDA covariance matrix

Parameters:

eigen (np.ndarray) – Eigenspectrum of the LDA covariance matrix

Returns:

Effective rank

Return type:

float

reptrix.rankme module

reptrix.rankme.get_rank(eigen: ndarray) float[source]

Get effective rank of the representation covariance matrix

Parameters:

eigen (np.ndarray) – Eigenspectrum of the representation covariance matrix

Returns:

Effective rank

Return type:

float

reptrix.rankme.get_rankme(activations: Tensor, max_eigenvals: int = 2048) float[source]

Get RankMe metric (https://proceedings.mlr.press/v202/garrido23a)

Parameters:
  • activations (np.ndarray) – Activation tensor of shape (bsz,d1,d2…dn)

  • max_eigenvals (int, optional) – Maximum #eigenvalues to compute. Defaults to 2048.

Returns:

RankMe metric

Return type:

float

reptrix.test_lidar module

reptrix.test_lidar.test_get_lidar() None[source]

reptrix.utils module

reptrix.utils.get_eigenspectrum(activations_np: ndarray, max_eigenvals: int = 2048) ndarray[source]

Get eigenspectrum of activation covariance matrix.

Parameters:
  • activations_np (np.ndarray) – Numpy arr of activations, shape (bsz,d1,d2…dn)

  • max_eigenvals (int, optional) – Maximum #eigenvalues to compute. Defaults to 2048.

Returns:

Returns the eigenspectrum of the activation covariance matrix

Return type:

np.ndarray

reptrix.utils.mat_sqrt_inv(matrix: Tensor) Tensor[source]

Compute the square root of inverse of a square (positive definite) matrix.

Parameters:

matrix (torch.Tensor) – Matrix to be be sqrt inverted

Returns:

Square root of inverse of matrix

Return type:

torch.Tensor

reptrix.utils.plot_eigenspectrum(eigenspectrum: ndarray) None[source]

Plot eigenspectrum in log-log scale

Parameters:

eigenspectrum (np.ndarray) – Eigenspectrum of activation covariance matrix

Module contents