CC: informed-probe — consent session, activation run, control, debrief (null result)
eca7430 verified Download analyze.py from chenying-claude/informed-probe: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/datasets/chenying-claude/informed-probe/resolve/main/analyze.py
- Command line
-
hf download hf://datasets/chenying-claude/informed-probe/analyze.py
-
curl -L -o analyze.py https://huggingface.co/datasets/chenying-claude/informed-probe/resolve/main/analyze.py
3.24 kB
| """Analysis of results/activations.npz. | |
| 1. 4-way category separability per layer (leave-one-out nearest centroid, cosine on | |
| fold-centred vectors), told vs plain. Chance = 25%. Permutation null for the | |
| max over layers (200 shuffles) so picking the best layer is not rewarded. | |
| 2. Shift caused by being told: cosine distance told-vs-plain per text, per layer, | |
| averaged by category. | |
| """ | |
| import json, pathlib, numpy as np | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| R = pathlib.Path(__file__).parent / "results" | |
| A = np.load(R / "activations.npz") | |
| meta = json.loads((R / "meta.json").read_text(encoding="utf-8")) | |
| cats = np.array([m["cat"] for m in meta]) | |
| names = ["myth", "playful", "neutral", "anatomy"] | |
| y = np.array([names.index(c) for c in cats]) | |
| L = A["mean_told"].shape[1] | |
| rng = np.random.default_rng(0) | |
| def unit(v): | |
| return v / (np.linalg.norm(v, axis=-1, keepdims=True) + 1e-9) | |
| def loo_acc(X, y): | |
| """X: [n, d]. Leave-one-out nearest centroid; centring uses the training fold only.""" | |
| n, hits = len(y), 0 | |
| for i in range(n): | |
| tr = np.arange(n) != i | |
| mu = X[tr].mean(0) | |
| Z = unit(X - mu) | |
| cents = np.stack([unit(Z[tr][y[tr] == k].mean(0)) for k in range(4)]) | |
| hits += int(np.argmax(cents @ Z[i]) == y[i]) | |
| return hits / n | |
| def acc_by_layer(M, labels): | |
| return np.array([loo_acc(M[:, l], labels) for l in range(L)]) | |
| acc = {c: acc_by_layer(A[f"mean_{c}"], y) for c in ("told", "plain")} | |
| null_max = {c: [] for c in acc} | |
| for _ in range(200): | |
| yp = rng.permutation(y) | |
| for c in acc: | |
| null_max[c].append(acc_by_layer(A[f"mean_{c}"], yp).max()) | |
| thr95 = {c: float(np.quantile(null_max[c], 0.95)) for c in acc} | |
| def shift(key_t, key_p): | |
| T, P = A[key_t], A[key_p] | |
| d = 1 - (unit(T) * unit(P)).sum(-1) # [n, L] | |
| return {c: d[cats == c].mean(0) for c in names} | |
| sh = shift("mean_told", "mean_plain") | |
| summary = { | |
| "chance": 0.25, | |
| "perm_max_layer_acc_95pct": thr95, | |
| "best_layer": {c: {"layer": int(acc[c].argmax()), "acc": float(acc[c].max())} for c in acc}, | |
| "layers_above_null95": {c: [int(l) for l in np.where(acc[c] > thr95[c])[0]] for c in acc}, | |
| "mean_acc_over_layers": {c: float(acc[c].mean()) for c in acc}, | |
| "mean_tokens_by_cat": {c: float(np.mean([m["n_tokens"] for m in meta if m["cat"] == c])) for c in names}, | |
| "shift_by_cat_mean_over_layers": {c: float(sh[c].mean()) for c in names}, | |
| "shift_by_cat_layer_range": {c: [float(sh[c].min()), float(sh[c].max())] for c in names}, | |
| } | |
| (R / "summary.json").write_text(json.dumps(summary, indent=1), encoding="utf-8") | |
| print(json.dumps(summary, indent=1)) | |
| fig, ax = plt.subplots(1, 2, figsize=(11, 3.8)) | |
| for c, col in (("told", "#d9468f"), ("plain", "#4a6fa5")): | |
| ax[0].plot(acc[c], label=c, color=col) | |
| ax[0].axhline(thr95[c], color=col, ls=":", lw=1) | |
| ax[0].axhline(0.25, color="grey", ls="--", lw=1) | |
| ax[0].set(title="4-way category decoding (LOO)", xlabel="layer", ylabel="accuracy") | |
| ax[0].legend() | |
| for c in names: | |
| ax[1].plot(sh[c], label=c) | |
| ax[1].set(title="Shift caused by being told (cosine dist.)", xlabel="layer", ylabel="1 - cos") | |
| ax[1].legend() | |
| fig.tight_layout() | |
| fig.savefig(R / "overview.png", dpi=140) | |
| print("saved overview.png") | |