File size: 1,797 Bytes
eca7430 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | """Sanity checks on analyze.py.
(a) Decoding accuracy at the first layers: if layer 0 (embeddings only) already
separates the categories, the 4-way result is lexical, not "meaning".
(b) Shift caused by being told, scale-free: ||told - plain|| divided by the mean
pairwise distance between different texts in the plain condition. Euclidean
distance cancels the large component shared by all vectors.
"""
import json, pathlib, numpy as np
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])
S = json.loads((R / "summary.json").read_text())
print("perm 95% of max-over-layers acc:", S["perm_max_layer_acc_95pct"])
def unit(v): return v / (np.linalg.norm(v, axis=-1, keepdims=True) + 1e-9)
def loo(X):
n, h = len(y), 0
for i in range(n):
tr = np.arange(n) != i
Z = unit(X - X[tr].mean(0))
c = np.stack([unit(Z[tr][y[tr] == k].mean(0)) for k in range(4)])
h += int(np.argmax(c @ Z[i]) == y[i])
return h / n
print("acc by layer (plain):", {l: round(loo(A["mean_plain"][:, l]), 2) for l in (0, 1, 2, 4, 8, 16, 24, 36)})
T, P = A["mean_told"], A["mean_plain"]
n, L = T.shape[0], T.shape[1]
rows = []
for l in (0, 1, 4, 8, 16, 24, 32, 36):
own = np.linalg.norm(T[:, l] - P[:, l], axis=1)
D = np.linalg.norm(P[:, None, l] - P[None, :, l], axis=-1)
between = D[~np.eye(n, dtype=bool)].mean()
rows.append((l, round(float(between), 2), {c: round(float(own[cats == c].mean() / between), 3) for c in names}))
print("layer, mean between-text dist, told-shift / between by category")
for r in rows: print(r)
|