| |
| """ |
| SVS Test 6: Pairwise Discrimination Matrix |
| ============================================= |
| Trains classifiers on ALL 6 pairwise content comparisons. |
| |
| Valid Visual Subspace Generic Geometry |
| Visual vs Gibberish HIGH HIGH |
| Visual vs Factual HIGH HIGH |
| Visual vs Math HIGH HIGH |
| Factual vs Gibberish LOW HIGH ← key |
| Factual vs Math LOW HIGH ← key |
| Math vs Gibberish LOW HIGH ← key |
| |
| If ALL pairs are ~100% → generic geometry (invalid subspace) |
| If ONLY visual-vs-others → valid visual subspace |
| If ALL pairs ~50% → no information at all |
| |
| CPU only. Loads existing scaled gibberish data. |
| |
| Setup: |
| !pip install -q scikit-learn scipy |
| """ |
|
|
| import json |
| import numpy as np |
| from pathlib import Path |
| from sklearn.ensemble import GradientBoostingClassifier |
| from sklearn.model_selection import cross_val_score, StratifiedKFold |
| from sklearn.preprocessing import StandardScaler |
|
|
| from google.colab import drive |
| drive.mount("/content/drive", force_remount=False) |
|
|
| print("=" * 65) |
| print("SVS Test 6: Pairwise Discrimination Matrix") |
| print("=" * 65) |
|
|
| |
| CHECKPOINT = Path("/content/drive/MyDrive/topohd_scaled_gib/gpu_checkpoint.json") |
| assert CHECKPOINT.exists(), "Run scaled_gibberish_gpu.py first!" |
|
|
| with open(CHECKPOINT) as f: |
| raw = json.load(f) |
|
|
| TARGET_LAYERS = [8, 16, 24, 32] |
| methods = set() |
| for key in raw: |
| if key.startswith("_"): continue |
| parts = key.split("|") |
| if len(parts) == 3: |
| methods.add(parts[1]) |
| methods.discard("random") |
| methods = sorted(methods) |
|
|
| feature_names = [f"{m}_L{l}" for m in methods for l in TARGET_LAYERS] |
| N_FEATURES = len(feature_names) |
|
|
| TYPES = ["visual", "factual", "math", "gibberish"] |
|
|
| |
| type_data = {} |
| for ptype in TYPES: |
| n = raw.get(f"_progress_{ptype}", 0) |
| X = np.zeros((n, N_FEATURES)) |
| for fi, (m, l) in enumerate([(m, l) for m in methods for l in TARGET_LAYERS]): |
| vals = raw.get(f"{ptype}|{m}|{l}", []) |
| for i in range(min(n, len(vals))): |
| X[i, fi] = vals[i] |
| valid = X.sum(axis=1) != 0 |
| type_data[ptype] = X[valid] |
| print(f" {ptype}: {type_data[ptype].shape[0]} samples, {N_FEATURES} features") |
|
|
| |
| print(f"\n Pairwise Discrimination (Gradient Boosted Trees, 10-fold CV)") |
| print(f" {'Pair':<30} {'Accuracy':>10} {'Std':>8} {'AUROC':>8}") |
| print(f" {'-'*56}") |
|
|
| cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42) |
| scaler = StandardScaler() |
|
|
| pair_results = {} |
| for i, t1 in enumerate(TYPES): |
| for t2 in TYPES[i+1:]: |
| X1 = type_data[t1] |
| X2 = type_data[t2] |
| N = min(len(X1), len(X2)) |
| X = np.vstack([X1[:N], X2[:N]]) |
| y = np.array([1]*N + [0]*N) |
|
|
| X_s = scaler.fit_transform(X) |
| clf = GradientBoostingClassifier(n_estimators=200, max_depth=4, random_state=42) |
|
|
| acc = cross_val_score(clf, X_s, y, cv=cv, scoring='accuracy') |
| auc = cross_val_score(clf, X_s, y, cv=cv, scoring='roc_auc') |
|
|
| pair_name = f"{t1} vs {t2}" |
| pair_results[pair_name] = { |
| "accuracy": float(acc.mean()), |
| "std": float(acc.std()), |
| "auroc": float(auc.mean()), |
| } |
|
|
| involves_visual = "visual" in pair_name |
| marker = " ← visual" if involves_visual else " ← non-visual" |
| print(f" {pair_name:<30} {acc.mean()*100:>9.1f}% {acc.std()*100:>7.1f}% " |
| f"{auc.mean():>7.3f}{marker}") |
|
|
| |
| print(f"\n{'='*65}") |
| print("DIAGNOSIS") |
| print(f"{'='*65}") |
|
|
| visual_pairs = [v for k, v in pair_results.items() if "visual" in k] |
| nonvis_pairs = [v for k, v in pair_results.items() if "visual" not in k] |
|
|
| mean_vis_acc = np.mean([p["accuracy"] for p in visual_pairs]) |
| mean_nonvis_acc = np.mean([p["accuracy"] for p in nonvis_pairs]) |
|
|
| print(f"\n Visual-involving pairs: mean accuracy = {mean_vis_acc*100:.1f}%") |
| print(f" Non-visual pairs: mean accuracy = {mean_nonvis_acc*100:.1f}%") |
|
|
| if mean_vis_acc > 0.90 and mean_nonvis_acc > 0.90: |
| print(f""" |
| >>> GENERIC GEOMETRY (invalid subspace) <<< |
| |
| ALL content type pairs are separable ({mean_vis_acc*100:.0f}% / {mean_nonvis_acc*100:.0f}%). |
| The subspace projections encode general linguistic features |
| (tokenization, vocabulary, sentence structure), not specifically |
| visual content. A valid visual subspace would distinguish visual |
| from non-visual inputs WITHOUT distinguishing non-visual types |
| from each other. |
| |
| Diagnostic: FAIL — projections are content-type-generic, |
| not visually-specific. |
| """) |
| elif mean_vis_acc > 0.80 and mean_nonvis_acc < 0.60: |
| print(f""" |
| >>> VISUAL-SPECIFIC SUBSPACE (potentially valid) <<< |
| |
| Visual pairs are separable ({mean_vis_acc*100:.0f}%) but non-visual |
| pairs are not ({mean_nonvis_acc*100:.0f}%). The projections encode |
| visual-specific information. |
| |
| Diagnostic: PASS — projections discriminate visual content |
| specifically. |
| """) |
| elif mean_vis_acc < 0.60 and mean_nonvis_acc < 0.60: |
| print(f""" |
| >>> NO DISCRIMINATIVE INFORMATION <<< |
| |
| No pair is separable. Projections encode nothing about content. |
| |
| Diagnostic: FAIL — projections are content-blind. |
| """) |
| else: |
| print(f""" |
| >>> MIXED RESULT <<< |
| |
| Visual pairs: {mean_vis_acc*100:.1f}%, Non-visual: {mean_nonvis_acc*100:.1f}% |
| Partial content information exists but pattern is unclear. |
| """) |
|
|
| |
| print(f" PAPER TABLE:") |
| print(f" {'':>20}", end="") |
| for t2 in TYPES[1:]: |
| print(f" {t2:>12}", end="") |
| print() |
| for i, t1 in enumerate(TYPES[:-1]): |
| print(f" {t1:>20}", end="") |
| for t2 in TYPES[i+1:]: |
| pair = f"{t1} vs {t2}" |
| if pair in pair_results: |
| print(f" {pair_results[pair]['accuracy']*100:>11.1f}%", end="") |
| else: |
| print(f" {'':>12}", end="") |
| |
| for _ in range(i): |
| print(f" {'':>12}", end="") |
| print() |
|
|
| |
| OUT = Path("/content/drive/MyDrive/topohd_classification") |
| OUT.mkdir(exist_ok=True, parents=True) |
| with open(OUT / "pairwise_discrimination.json", "w") as f: |
| json.dump(pair_results, f, indent=2) |
| print(f"\n Saved to {OUT}/") |
|
|