Centered Kernel Alignment (CKA) - PyTorch Implementation
A PyTorch implementation of Centered Kernel Alignment (CKA) with GPU support for fast and efficient computation.
[!WARNING] This project is for educational and academic purposes (and for fun 🤷🏻).
Features
- GPU Accelerated: Leverages the power of GPUs for significantly faster CKA calculations compared to NumPy-based implementations.
- On-the-Fly Calculation: Computes CKA on-the-fly using mini-batches, avoiding the need to cache large intermediate feature representations.
- Easy to Use: Simple and intuitive API for calculating the CKA matrix between two models.
- Flexible: Can be used with any PyTorch models and dataloaders.
Installation
pip install cka-pytorch
Usage
import torch
from torchvision.models import resnet18
from torch.utils.data import DataLoader
from cka_pytorch.cka import CKACalculator
# 1. Define your models and dataloader
model1 = resnet18(pretrained=True).cuda()
model2 = resnet18(pretrained=True).cuda() # Or a different model
# Create a dummy dataloader for demonstration
dummy_data = torch.randn(100, 3, 224, 224)
dummy_labels = torch.randint(0, 10, (100,))
dummy_dataset = torch.utils.data.TensorDataset(dummy_data, dummy_labels)
dataloader = DataLoader(dummy_dataset, batch_size=32)
# 2. Initialize the CKACalculator
# By default, we will calculate CKA across all layers of the two models
calculator = CKACalculator(
model1=model1,
model2=model2,
model1_name="ResNet18",
model2_name="ResNet18",
batched_feature_size=256,
verbose=True,
)
# 3. Calculate the CKA matrix
cka_matrix = calculator.calculate_cka_matrix(dataloader)
# 4. Plot the CKA Matrix as heatmap
calculator.plot_cka_matrix(title="CKA between ResNet18 and ResNet18")
Contributing
- If you find this repository helpful, please give it a :star:.
- If you encounter any bugs or have suggestions for improvements, feel free to open an issue.
- This implementation has been primarily tested with ResNet architectures.
Acknowledgement
This project is based on:
Release files for cka-pytorch 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cka_pytorch-1.1.3.tar.gz | 16.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cka_pytorch-1.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 35.1 kB
Release files / cka_pytorch-1.1.3.tar.gz
| Download URL | cka_pytorch-1.1.3.tar.gz |
|---|---|
| Size | 16.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1ca08b2fa414c8f273d4ac724732611d500db5dd826a8b0a6b36666c4af0ce05
|
|
BLAKE2b-256 checksum How to use checksums |
3075d6c5e7ff18fdde717022b95549ce4a1c1c5e8be125d0d223cdc9bbe359cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2025.
Transparency logRelease files / cka_pytorch-1.1.3-py3-none-any.whl
| Download URL | cka_pytorch-1.1.3-py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d70a23772c50a5e3fcc29561936ded5ab0a629167c88b12c2da8ba613d9281b9
|
|
BLAKE2b-256 checksum How to use checksums |
41f6be736a66615e66f774e5677792628bfbcdb4b8ce4d1baffb63f8abe0cf90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2025.
Transparency log