-
Notifications
You must be signed in to change notification settings - Fork 0
CKA for representation similarity #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ instance/ | |
|
|
||
| # Sphinx documentation | ||
| docs/_build/ | ||
| docs/source/api/ | ||
|
|
||
| # PyBuilder | ||
| target/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| :sd_hide_title: | ||
|
|
||
| `tdhook` | ||
| ===== | ||
| ======== | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 1 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
docs/source/notebooks/methods/representation-similarity.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Representation Similarity\n", | ||
| "\n", | ||
| "This notebook introduces representation similarity methods in `tdhook`.\n", | ||
| "\n", | ||
| "It currently starts with centered kernel alignment (CKA) through `tdhook.latent.representation_similarity.CkaEstimator`. More similarity methods can be added here later as the module grows." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Setup" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import importlib.util\n", | ||
| "\n", | ||
| "DEV = True\n", | ||
| "\n", | ||
| "if importlib.util.find_spec(\"google.colab\") is not None:\n", | ||
| " MODE = \"colab-dev\" if DEV else \"colab\"\n", | ||
| "else:\n", | ||
| " MODE = \"local\"" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "if MODE == \"colab\":\n", | ||
| " %pip install -q tdhook\n", | ||
| "elif MODE == \"colab-dev\":\n", | ||
| " !rm -rf tdhook\n", | ||
| " !git clone https://github.com/Xmaster6y/tdhook -b main\n", | ||
| " %pip install -q ./tdhook" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Imports" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import torch\n", | ||
| "from tensordict import TensorDict\n", | ||
| "\n", | ||
| "from tdhook.latent.representation_similarity import CkaEstimator" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Synthetic Example\n", | ||
| "\n", | ||
| "We build a few pairs of representations with known relationships:\n", | ||
| "\n", | ||
| "- `same`: identical representations, so CKA should be close to `1`\n", | ||
| "- `rotated`: an orthogonal transform of the same representation, which linear CKA should also score near `1`\n", | ||
| "- `random`: an unrelated representation, which should typically score much lower" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 4, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "torch.manual_seed(0)\n", | ||
| "\n", | ||
| "x = torch.randn(256, 32)\n", | ||
| "q, _ = torch.linalg.qr(torch.randn(32, 32))\n", | ||
| "\n", | ||
| "examples = {\n", | ||
| " \"same\": (x, x.clone()),\n", | ||
| " \"rotated\": (x, x @ q),\n", | ||
| " \"random\": (x, torch.randn(256, 24)),\n", | ||
| "}\n", | ||
| "\n", | ||
| "estimator = CkaEstimator(kernel=\"linear\")\n", | ||
| "\n", | ||
| "\n", | ||
| "def run_cka(x, y):\n", | ||
| " td = TensorDict({\"data_a\": x, \"data_b\": y}, batch_size=[])\n", | ||
| " return estimator(td.clone())[\"cka\"].item()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 5, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "{'same': 1.0, 'rotated': 1.0, 'random': 0.10095701366662979}" | ||
| ] | ||
| }, | ||
| "execution_count": 5, | ||
| "metadata": {}, | ||
| "output_type": "execute_result" | ||
| } | ||
| ], | ||
| "source": [ | ||
| "scores = {name: run_cka(a, b) for name, (a, b) in examples.items()}\n", | ||
| "scores" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Batched Inputs\n", | ||
| "\n", | ||
| "Like the dimension-estimation modules, `CkaEstimator` accepts either `(N, D)` or batched `(..., N, D)` inputs and returns one scalar score per batch item." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 6, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "tensor([0.9979, 0.9978, 0.9976])" | ||
| ] | ||
| }, | ||
| "execution_count": 6, | ||
| "metadata": {}, | ||
| "output_type": "execute_result" | ||
| } | ||
| ], | ||
| "source": [ | ||
| "batched_x = torch.randn(3, 128, 16)\n", | ||
| "batched_y = batched_x + 0.05 * torch.randn(3, 128, 16)\n", | ||
| "\n", | ||
| "td = TensorDict({\"data_a\": batched_x, \"data_b\": batched_y}, batch_size=[3])\n", | ||
| "CkaEstimator()(td.clone())[\"cka\"]" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## API Notes\n", | ||
| "\n", | ||
| "- The estimator is named `CkaEstimator` and already exposes a `kernel` argument.\n", | ||
| "- At the moment only `kernel=\"linear\"` is implemented.\n", | ||
| "- Degenerate inputs with zero variance return `nan` instead of raising.\n", | ||
| "\n", | ||
| "Future methods can extend this notebook with additional sections, comparisons, and visualizations." | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": ".venv", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.11.10" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 4 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """ | ||
| Representation similarity methods. | ||
| """ | ||
|
|
||
| from .cka import CkaEstimator | ||
|
|
||
| __all__ = ["CkaEstimator"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.