analysis
shrinkai.analysis
Representation-alignment analysis between a teacher and a student model.
FeatureAnalyzer runs both models (wrapped in shrinkai.adapters.FeatureExtractor)
over a dataloader and scores, layer by layer, how well the student's internal
representations match the teacher's using metrics such as CKAMetric (Centered
Kernel Alignment), RSAMetric (Representational Similarity Analysis), and
SpatialAttentionMetric. This is diagnostic tooling: it helps decide where a
feature-based distillation loss would help most, rather than being a loss itself.
Modules:
| Name | Description |
|---|---|
analyzer |
|
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 |
FeatureAnalyzer |
Evaluates how well a student model mimics the teacher's internal hidden states. |
FeatureAnalyzerReport |
Holds representation analysis results and renders a dashboard. |
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
FeatureAnalyzer
Evaluates how well a student model mimics the teacher's internal hidden states.
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the FeatureAnalyzer. |
evaluate |
Runs the dataset through both models and computes alignment metrics per layer. |
Source code in src/shrinkai/analysis/analyzer.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
Methods:
__init__
__init__(
teacher_extractor: FeatureExtractor,
student_extractor: FeatureExtractor,
device: device | str = "auto",
) -> None
Initializes the FeatureAnalyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
teacher_extractor
|
FeatureExtractor
|
Teacher model wrapped in FeatureExtractor. |
required |
student_extractor
|
FeatureExtractor
|
Student model wrapped in FeatureExtractor. |
required |
device
|
device | str
|
Computing target. |
'auto'
|
Source code in src/shrinkai/analysis/analyzer.py
evaluate
evaluate(
dataloader: DataLoader,
metrics: list[str | BaseFeatureMetric] | None = None,
) -> FeatureAnalyzerReport
Runs the dataset through both models and computes alignment metrics per layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataloader
|
DataLoader
|
Dataloader yielding validation batches. |
required |
metrics
|
list[str | BaseFeatureMetric] | None
|
List of metric strings ('cka') or instantiated BaseFeatureMetric objects. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FeatureAnalyzerReport |
FeatureAnalyzerReport
|
Formatted report ready for |
Source code in src/shrinkai/analysis/analyzer.py
FeatureAnalyzerReport
Holds representation analysis results and renders a dashboard.
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the report. |
show |
Renders an interactive formatted analysis table in the console. |
Source code in src/shrinkai/analysis/analyzer.py
Methods:
__init__
Initializes the report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_scores
|
dict[str, dict[str, float]]
|
Nested dict |
required |
show
Renders an interactive formatted analysis table in the console.
Source code in src/shrinkai/analysis/analyzer.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.