pytorch-cka
The Fastest, Memory-efficient Python Library for computing layer-wise similarity between neural network models
44x faster CKA computation across 18 representational layers of ResNet-18 models on CIFAR-10 using NVIDIA H100 GPUs
- ⚡️ Fastest among CKA libraries thanks to vectorized ops & GPU acceleration
- 📦 Efficient memory management with explicit deallocation
- 🧠 Supports HuggingFace models, DataParallel, and DDP
- 🎨 Customizable visualizations: heatmaps and line charts
📦 Installation
Requires Python 3.10+
# Using pip
pip install pytorch-cka
# Using uv
uv add pytorch-cka
👟 Quick Start
Basic Usage
from cka import compute_cka
from torch.utils.data import DataLoader
from torchvision.models import resnet18, resnet34
resnet_18 = resnet18(pretrained=True)
resnet_34 = resnet34(pretrained=True)
dataloader1 = Dataloader(your_dataset1, batch_size=bach_size, shuffle=False, num_workers=4)
dataloader2 = Dataloader(your_dataset2, batch_size=bach_size, shuffle=False, num_workers=4)
dataloader3 = Dataloader(your_dataset3, batch_size=bach_size, shuffle=False, num_workers=4)
dataloaders = [dataloader1, dataloader2, dataloader3]
layers = [
'conv1',
'layer1.0.conv1',
'layer2.0.conv1',
'layer3.0.conv1',
'layer4.0.conv1',
'fc',
]
cka_matrices = compute_cka(
resnet_18,
resnet_34,
dataloaders,
layers=layers,
device=device,
)
for cka_matrix in cka_matrices:
print(cka_matrix)
From Pre-extracted Features
If you already have feature matrices, compute CKA without models or dataloaders:
from cka import cka_from_features
# Single layer: (n_samples, feature_dim)
cka_matrix = cka_from_features(features_x, features_y)
# Multi-layer: (n_layers, n_samples, feature_dim)
cka_matrix = cka_from_features(multi_layer_x, multi_layer_y)
# Varying feature dims: list of 2D tensors
cka_matrix = cka_from_features(
[layer1_x, layer2_x],
[layer1_y, layer2_y, layer3_y],
)
Visualization
Heatmap
from cka import plot_cka_heatmap
fig, ax = plot_cka_heatmap(
cka_matrix,
layers1=layers,
layers2=layers,
model1_name="ResNet-18 (pretrained)",
model2_name="ResNet-18 (random init)",
annot=False, # Show values in cells
cmap="inferno", # Colormap
)
|
|
|
| Self-comparison | Cross-model |
Trend Plot
from cka import plot_cka_trend
# Plot diagonal (self-similarity across layers)
diagonal = torch.diag(matrix)
fig, ax = plot_cka_trend(
layer_trends,
x_values=epochs,
labels=RESNET18_LAYERS,
markers=['o'],
xlabel='Epoch',
ylabel='CKA Score',
title='Pretrained vs. Fine-tuned Across Epochs (ResNet-18)',
legend=True,
)
fig, ax = plot_cka_layer_trend(
cka_matrices,
layers=RESNET18_LAYERS,
labels=cka_loader_names,
ylabel='CKA Score',
title='Pretrained vs. Fine-tuned Across Layers (ResNet-18)',
legend=True,
)
|
|
|
| CKA Score Trend Across Epochs | CKA Score Trend Across Layers |
📚 References
Kornblith, Simon, et al. "Similarity of Neural Network Representations Revisited." ICML 2019.
Metadata
Release files for pytorch-cka 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 | Provenance | Actions |
|---|---|---|---|---|
| pytorch_cka-1.1.3.tar.gz | 266.4 kB | With provenance | Details for pytorch_cka-1.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytorch_cka-1.1.3-py3-none-any.whl | Python 3 | none | any | Details for pytorch_cka-1.1.3-py3-none-any.whl |
Total release size: 282.4 kB
Release files / pytorch_cka-1.1.3.tar.gz
| Download URL | pytorch_cka-1.1.3.tar.gz |
|---|---|
| Size | 266.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c7ddf3a8ee5153ed4ec2567bbd0ef3719e8e3d1cebb4335bd8a1253961744d7
|
|
BLAKE2b-256 checksum How to use checksums |
b7f3425960d69fb84d9d45882ec0535d6aed4d0eb309fa5f79d0ce4255f2605d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 9, 2026.
Transparency logRelease files / pytorch_cka-1.1.3-py3-none-any.whl
| Download URL | pytorch_cka-1.1.3-py3-none-any.whl |
|---|---|
| Size | 15.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
540ff28c75094733e3043f0f0b09cd6595aaf6b75aa57d03e4ea386d317bfb0f
|
|
BLAKE2b-256 checksum How to use checksums |
f94e27662253bce7f50c142610184998ee1fe2d6e3106cb4de3a069b7d26d650
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 9, 2026.
Transparency logSecurity for pytorch-cka 1.1.3
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pytorch_cka-1.1.3.tar.gz
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 Aug 9, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.