Warning
This repository has been built mainly for personal and academic use since Captum still needs to implement its variant of CKA. As such, do not expect this project to work for every model.
Note
Centered Kernel Alignment (CKA) [1] is a similarity index between representations of features in neural networks, based on the Hilbert-Schmidt Independence Criterion (HSIC) [2]. Given a set of examples, CKA compares the representations of examples passed through the layers that we want to compare.
Given two matrices
Then, the
where
Note
However, naive computation of linear CKA (i.e.: the previous equation) requires maintaining the activations across the entire dataset in memory, which is challenging for wide and deep networks [3].
Therefore, we need to define the unbiased estimator of HSIC so that the value of CKA is independent of the batch size
where
with
This project requires python >= 3.10.
Note
This will create a new virtual environment in the working directory under .venv. If you create such venv with uv there will be no need to activate it since uv will find the env in the working directory or any parent directories.
# If you have uv installed
uv venv
# Otherwise
python -m venv .venv
source .venv/bin/activate # if you are on Linux
.\.venv\Scripts\activate.bat # if you are using the cmd on Windows
.\.venv\Scripts\Activate.ps1 # if you are using the PowerShell on Windows
You can install the package by either:
-
using pip
pip install git+https://github.com/RistoAle97/centered-kernel-alignment
This will not install the
dev
dependencies listed inpyproject.toml
. -
cloning the repository and installing the dependencies
git clone https://github.com/RistoAle97/centered-kernel-alignment # If you have uv installed uv pip install -e centered-kernel-alignment uv pip install ckatorch[dev] # if you want to commit something to the repo # Otherwise pip install -e centered-kernel-alignment pip install ckatorch[dev] # same as for uv, remember to open a pull request afterwards
Take a look at the examples
directory to understand how to compute CKA in two basic scenarios.
Model compared with itself | Different models compared |
---|---|
[1] Kornblith, Simon, et al. "Similarity of neural network representations revisited." International Conference on Machine Learning. PMLR, 2019.
[2] Wang, Tinghua, Xiaolu Dai, and Yuze Liu. "Learning with Hilbert–Schmidt independence criterion: A review and new perspectives." Knowledge-based systems 234 (2021): 107567.
[3] Nguyen, Thao, Maithra Raghu, and Simon Kornblith. "Do wide and deep networks learn the same things? uncovering how neural network representations vary with width and depth." arXiv preprint arXiv:2010.15327 (2020).
This project is also based on the following repositories:
- representation_similarity (original implementation).
- PyTorch-Model-Compare (nice PyTorch implementation that employs hooks).
-
CKA.pytorch (minibatch version of CKA and useful batched implementation of
$HSIC_1$ ).
This project is MIT licensed.