Metrics Similarity#
similarity
#
| FUNCTION | DESCRIPTION |
|---|---|
cca |
Compute CCA similarity between two embeddings. |
pwcca |
Compute Projection-Weighted CCA (PWCCA) similarity. |
rsa_correlation |
Compute Representational Similarity Analysis (RSA) correlation. |
Functions#
cca(X: torch.Tensor, Y: torch.Tensor, n_eigen: int | None = None, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Compute CCA similarity between two embeddings.
CCA (Canonical Correlation Analysis) finds linear projections of X and Y
that maximize correlation. The CCA similarity is the mean of the top n_eigen
canonical correlations [1].
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, x_features).
TYPE:
|
Y
|
Input tensor of shape (n_samples, y_features).
TYPE:
|
n_eigen
|
Number of CCA components to use. If None, uses min(x_features, y_features).
TYPE:
|
eps
|
Regularization term added to covariance matrices for numerical stability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Mean canonical correlation (CCA similarity) in [0, 1]. |
Examples:
Perfect correlation:
Source code in spectre/metrics/similarity.py
pwcca(X: torch.Tensor, Y: torch.Tensor, n_eigen: int | None = None, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Compute Projection-Weighted CCA (PWCCA) similarity.
PWCCA extends CCA by weighting canonical correlations by their importance to the original representations. This addresses the known limitation that standard CCA can give pessimistic similarity scores for neural network representations [2].
The weighting is based on how well each canonical direction explains variance in the original data, providing a more interpretable measure of representational similarity.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, x_features).
TYPE:
|
Y
|
Input tensor of shape (n_samples, y_features).
TYPE:
|
n_eigen
|
Number of CCA components to use. If None, uses min(x_features, y_features).
TYPE:
|
eps
|
Regularization term added to covariance matrices for numerical stability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
PWCCA similarity score in [0, 1]. |
Examples:
Perfect correlation:
Source code in spectre/metrics/similarity.py
rsa_correlation(X: torch.Tensor, Y: torch.Tensor, distance_fn: str = 'euclidean', correlation_type: str = 'spearman') -> torch.Tensor
#
Compute Representational Similarity Analysis (RSA) correlation.
RSA compares second-order similarity by constructing Representational Dissimilarity Matrices (RDMs) for each representation, then computing their correlation. This captures how samples relate to each other rather than absolute activation values.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First feature matrix, shape (n_samples, x_features).
TYPE:
|
Y
|
Second feature matrix, shape (n_samples, y_features).
TYPE:
|
distance_fn
|
Distance metric from
TYPE:
|
correlation_type
|
Type of correlation: "spearman", "pearson".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Correlation coefficient in [-1, 1]. Higher values indicate greater similarity in how representations organize samples relationally. |
Examples:
>>> X = torch.randn(50, 20)
>>> Y = torch.randn(50, 15)
>>> rsa_correlation(X, Y, correlation_type="spearman")
0.32...
Use Pearson correlation:
Source code in spectre/metrics/similarity.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |