metrics
shrinkai.analysis.metrics
Classes:
| Name | Description |
|---|---|
BaseFeatureMetric |
Abstract base class for all feature alignment metrics. |
CKAMetric |
Linear Centered Kernel Alignment (CKA) (Kornblith et al. (2019), building on the |
RSAMetric |
Representational Similarity Analysis (RSA) using Pearson correlation |
SpatialAttentionMetric |
Spatial Attention Transfer similarity (Zagoruyko & Komodakis (2017)). |
Classes
BaseFeatureMetric
Bases: ABC
Abstract base class for all feature alignment metrics.
Methods:
| Name | Description |
|---|---|
compute |
Computes the alignment score between two feature maps. |
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Display name of the metric (e.g., 'CKA', 'RSA'). |
Source code in src/shrinkai/analysis/metrics.py
Attributes
Methods:
compute
abstractmethod
Computes the alignment score between two feature maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student_features
|
Tensor
|
Tensor of shape [Batch, ...]. |
required |
teacher_features
|
Tensor
|
Tensor of shape [Batch, ...]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
A score indicating representation similarity. |
Source code in src/shrinkai/analysis/metrics.py
CKAMetric
Bases: BaseFeatureMetric
Linear Centered Kernel Alignment (CKA) (Kornblith et al. (2019), building on the Hilbert-Schmidt Independence Criterion of Gretton et al. (2005)).
Measures the similarity of representations across models with different architectures or channel dimensions. A score of 1.0 means identical representational geometry; 0.0 means completely orthogonal.
Equation
where \(K = X_c X_c^\top\) and \(L = Y_c Y_c^\top\) are the Gram matrices of the (already mean-centered) student and teacher activations \(X_c, Y_c\), and \(HSIC(K, L) \propto \langle K, L \rangle_F = \text{tr}(KL)\) for linear kernels on centered data. The shared normalization constant of the HSIC estimator cancels out in the ratio, so this implementation computes it directly as \(\text{tr}(KL) / (\|K\|_F \|L\|_F)\).
Source code in src/shrinkai/analysis/metrics.py
RSAMetric
Bases: BaseFeatureMetric
Representational Similarity Analysis (RSA) using Pearson correlation (Kriegeskorte et al. (2008)).
Measures if the relative distances between samples in a batch are preserved between the teacher and the student, regardless of their hidden dimension sizes.
Equation
where \(R_S\) and \(R_T\) are the upper-triangular entries of the student's and teacher's Representational (Dis)similarity Matrices, here, pairwise cosine similarities between samples in the batch, and the RSA score is their Pearson correlation across the batch.
Source code in src/shrinkai/analysis/metrics.py
SpatialAttentionMetric
Bases: BaseFeatureMetric
Spatial Attention Transfer similarity (Zagoruyko & Komodakis (2017)).
Collapses the channel dimension to measure if the student and teacher activate on the same spatial regions of the input (e.g., the foreground object).
Equation
where \(f_c\) is the activation map of channel \(c\), \(A\) is the resulting spatial attention map (summed absolute activations across channels, the \(\mathcal{F}_{sum}^{p=1}\) mapping of the original paper), and the metric is the cosine similarity between the student's and teacher's (L2-normalized, spatially-resized-if-needed) attention maps.