{"id":40551029,"url":"https://github.com/ryusudol/centered-kernel-alignment","last_synced_at":"2026-02-01T23:03:25.017Z","repository":{"id":332210402,"uuid":"1117122687","full_name":"ryusudol/Centered-Kernel-Alignment","owner":"ryusudol","description":"CKA with Efficient Computation and Layer-wise Visualization for PyTorch","archived":false,"fork":false,"pushed_at":"2026-01-14T09:06:25.000Z","size":669,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-14T13:16:06.865Z","etag":null,"topics":["centered-kernel-alignment","cka","machine-learning","neural-networks","pytorch","representation","similarity"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryusudol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-15T21:36:56.000Z","updated_at":"2026-01-14T09:06:31.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/ryusudol/Centered-Kernel-Alignment","commit_stats":null,"previous_names":["ryusudol/pytorch-cka","ryusudol/centered-kernel-alignment"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/ryusudol/Centered-Kernel-Alignment","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryusudol%2FCentered-Kernel-Alignment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryusudol%2FCentered-Kernel-Alignment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryusudol%2FCentered-Kernel-Alignment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryusudol%2FCentered-Kernel-Alignment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryusudol","download_url":"https://codeload.github.com/ryusudol/Centered-Kernel-Alignment/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryusudol%2FCentered-Kernel-Alignment/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28618863,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T23:49:58.628Z","status":"ssl_error","status_checked_at":"2026-01-20T23:47:29.996Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["centered-kernel-alignment","cka","machine-learning","neural-networks","pytorch","representation","similarity"],"created_at":"2026-01-21T00:02:06.805Z","updated_at":"2026-02-01T23:03:25.007Z","avatar_url":"https://github.com/ryusudol.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# pytorch-cka\n\n[![PyPI](https://img.shields.io/pypi/v/pytorch-cka.svg)](https://pypi.org/project/pytorch-cka/)\n[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://pypi.org/project/pytorch-cka/)\n[![PyPI Downloads](https://static.pepy.tech/personalized-badge/pytorch-cka?period=total\u0026units=INTERNATIONAL_SYSTEM\u0026left_color=GREY\u0026right_color=RED\u0026left_text=downloads)](https://pepy.tech/projects/pytorch-cka)\n\n**The Fastest, Memory-efficient Python Library for computing layer-wise similarity between neural network models**\n\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cpicture align=\"center\"\u003e\n        \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://github.com/user-attachments/assets/a20bb8fb-9485-4259-8239-51ba66fcd49c\"\u003e\n        \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://github.com/user-attachments/assets/60a6dc50-6368-4eb9-9145-02a7d6b98961\"\u003e\n        \u003cimg alt=\"A bar chart with benchmark results in light mode\" src=\"https://github.com/user-attachments/assets/a20bb8fb-9485-4259-8239-51ba66fcd49c\" width=\"100%\" /\u003e\n    \u003c/picture\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ci\u003e\u003cb\u003e44x\u003c/b\u003e faster CKA computation across 18 representational layers of ResNet-18 models on CIFAR-10 using NVIDIA H100 GPUs\u003c/i\u003e\n\u003c/p\u003e\n\n- ⚡️ Fastest among CKA libraries thanks to **vectorized ops** \u0026 **GPU acceleration**\n- 📦 Efficient memory management with explicit deallocation\n- 🧠 Supports HuggingFace models, DataParallel, and DDP\n- 🎨 Customizable visualizations: heatmaps and line charts\n\n## 📦 Installation\n\nRequires `Python 3.10+`\n\n```bash\n# Using pip\npip install pytorch-cka\n\n# Using uv\nuv add pytorch-cka\n```\n\n## 👟 Quick Start\n\n### Basic Usage\n\n```python\nfrom cka import compute_cka\nfrom torch.utils.data import DataLoader\nfrom torchvision.models import resnet18, resnet34\n\nresnet_18 = resnet18(pretrained=True)\nresnet_34 = resnet34(pretrained=True)\n\ndataloader1 = Dataloader(your_dataset1, batch_size=bach_size, shuffle=False, num_workers=4)\ndataloader2 = Dataloader(your_dataset2, batch_size=bach_size, shuffle=False, num_workers=4)\ndataloader3 = Dataloader(your_dataset3, batch_size=bach_size, shuffle=False, num_workers=4)\ndataloaders = [dataloader1, dataloader2, dataloader3]\n\nlayers = [\n    'conv1',\n    'layer1.0.conv1',\n    'layer2.0.conv1',\n    'layer3.0.conv1',\n    'layer4.0.conv1',\n    'fc',\n]\n\ncka_matrices = compute_cka(\n    resnet_18,\n    resnet_34,\n    dataloaders,\n    layers=layers,\n    device=device,\n)\n\nfor cka_matrix in cka_matrices:\n    print(cka_matrix)\n```\n\n### Visualization\n\n**Heatmap**\n\n```python\nfrom cka import plot_cka_heatmap\n\nfig, ax = plot_cka_heatmap(\n    cka_matrix,\n    layers1=layers,\n    layers2=layers,\n    model1_name=\"ResNet-18 (pretrained)\",\n    model2_name=\"ResNet-18 (random init)\",\n    annot=False,          # Show values in cells\n    cmap=\"inferno\",       # Colormap\n)\n```\n\n\u003ctable width=\"100%\"\u003e\n    \u003ctr\u003e\n      \u003ctd width=\"63.5%\" style=\"padding:0; vertical-align:top; line-height:0;\"\u003e\n          \u003cimg\n              src=\"https://github.com/user-attachments/assets/48bfd811-629e-483a-bc63-2a493872289c\"\n              alt=\"Self-comparison heatmap\"\n              style=\"display:block; width:100%;\"\n          /\u003e\n      \u003c/td\u003e\n      \u003ctd width=\"36.5%\" style=\"padding:0; vertical-align:top; line-height:0;\"\u003e\n          \u003cimg\n              src=\"https://github.com/user-attachments/assets/38ad963a-e8ce-4dc1-ab62-73d4f072ceca\"\n              alt=\"Cross-model comparison heatmap\"\n              style=\"display:block; width:100%;\"\n          /\u003e\n      \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\"\u003eSelf-comparison\u003c/td\u003e\n      \u003ctd align=\"center\"\u003eCross-model\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n**Trend Plot**\n\n```python\nfrom cka import plot_cka_trend\n\n# Plot diagonal (self-similarity across layers)\ndiagonal = torch.diag(matrix)\n\nfig, ax = plot_cka_trend(\n    layer_trends,\n    x_values=epochs,\n    labels=RESNET18_LAYERS,\n    markers=['o'],\n    xlabel='Epoch',\n    ylabel='CKA Score',\n    title='Pretrained vs. Fine-tuned Across Epochs (ResNet-18)',\n    legend=True,\n)\n\nfig, ax = plot_cka_layer_trend(\n    cka_matrices,\n    layers=RESNET18_LAYERS,\n    labels=cka_loader_names,\n    ylabel='CKA Score',\n    title='Pretrained vs. Fine-tuned Across Layers (ResNet-18)',\n    legend=True,\n)\n```\n\n\u003ctable\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cimg src=\"https://github.com/user-attachments/assets/7b029b61-dcd3-4461-849e-80546694e993\" alt=\"CKA Score Trend Across Epochs\" width=\"100%\"/\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003cimg src=\"https://github.com/user-attachments/assets/28dc7708-cede-4e0c-ab34-95fc7188ade7\" alt=\"CKA Score Trend Across Layers\" width=\"100%\"/\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\"\u003eCKA Score Trend Across Epochs\u003c/td\u003e\n      \u003ctd align=\"center\"\u003eCKA Score Trend Across Layers\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n## 📚 References\n\nKornblith, Simon, et al. [\"Similarity of Neural Network Representations Revisited.\"](https://arxiv.org/abs/1905.00414) _ICML 2019._\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryusudol%2Fcentered-kernel-alignment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryusudol%2Fcentered-kernel-alignment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryusudol%2Fcentered-kernel-alignment/lists"}