Skip to main content

pytorch-cka

PyPI Python PyPI Downloads

The Fastest, Memory-efficient Python Library for computing layer-wise similarity between neural network models

A bar chart with benchmark results in light mode

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)

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 heatmap Cross-model comparison heatmap
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
CKA Score Trend Across Epochs CKA Score Trend Across Layers

📚 References

Kornblith, Simon, et al. "Similarity of Neural Network Representations Revisited." ICML 2019.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytorch_cka-1.0.2.tar.gz (265.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytorch_cka-1.0.2-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file pytorch_cka-1.0.2.tar.gz.

File metadata

  • Download URL: pytorch_cka-1.0.2.tar.gz
  • Upload date:
  • Size: 265.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytorch_cka-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d3e1c31475b7d2af29764951f87056c5e8b452b3877fbb5c511856fad9606536
MD5 c5a4eddeddbeaec3e13337250a19e61a
BLAKE2b-256 aa7e17f0449f9bb42e7787df45e33ece7d5ecd4316e66d26a4b65d18831c7155

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytorch_cka-1.0.2.tar.gz:

Publisher: publish.yaml on ryusudol/Centered-Kernel-Alignment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytorch_cka-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytorch_cka-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytorch_cka-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 67d659bb2c05181d5b894650ff91d89df651018ba2f97011b655517642b20a45
MD5 27744f36d8383ec784367d1d91d2bfe6
BLAKE2b-256 bb9e4cbdca09e6789945d4396968d0e071a48c39bcf0e3f61cca98770b3a4d1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytorch_cka-1.0.2-py3-none-any.whl:

Publisher: publish.yaml on ryusudol/Centered-Kernel-Alignment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

This release

1.0.2 This release

2 files

1.0.1

2 files

1.0.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page