"""Side-by-side cloverleaf comparison: mean-field × 3 seeds vs NTK vs narrow.""" from __future__ import annotations from pathlib import Path import numpy as np, pandas as pd, seaborn as sns, matplotlib.pyplot as plt, torch ROOT = Path("/workspace/mft") RUNS = [ ("run_seed_1", "Mean-field, seed 1\n(width 2048)"), ("run_seed_2", "Mean-field, seed 2"), ("run_seed_3", "Mean-field, seed 3"), ("run_ntk", "NTK scaling\n(no cloverleaf)"), ("run_narrow", "Narrow (width 16)\n(too few neurons)"), ] sns.set_theme(style="white", context="paper") fig, axes = plt.subplots(1, 5, figsize=(20, 4.2), sharex=True, sharey=True) for ax, (run, title) in zip(axes, RUNS): ckpt = torch.load(ROOT/run/"model.pt", map_location="cpu", weights_only=True) W = ckpt["W"].numpy(); a = ckpt["a"].numpy() sgn = np.where(a > 0, "+", "−") df = pd.DataFrame({"w1": W[:,0], "w2": W[:,1], "sign_a": sgn}) sns.scatterplot(df, x="w1", y="w2", hue="sign_a", palette={"+":"#1f77b4","−":"#d62728"}, s=8, alpha=0.6, edgecolor=None, ax=ax, legend=(ax is axes[0])) ax.set_title(title) ax.set_xlabel("w₁"); ax.set_ylabel("w₂" if ax is axes[0] else "") ax.set_xlim(-35,35); ax.set_ylim(-35,35); ax.set_aspect("equal") fig.suptitle("Hidden-layer incoming-weight distribution by training regime", fontsize=13, y=1.02) fig.tight_layout() fig.savefig(ROOT/"comparison.pdf", bbox_inches="tight") print("Wrote", ROOT/"comparison.pdf") # Angle histogram comparison fig, ax = plt.subplots(figsize=(9, 4)) for run, title in RUNS: ckpt = torch.load(ROOT/run/"model.pt", map_location="cpu", weights_only=True) W = ckpt["W"].numpy() # filter out near-dead neurons norms = np.linalg.norm(W, axis=1) mask = norms > np.quantile(norms, 0.5) angles = np.degrees(np.arctan2(W[mask,1], W[mask,0])) sns.kdeplot(angles, ax=ax, label=title.split('\n')[0], bw_adjust=0.4) for x0 in [-180, -90, 0, 90, 180]: ax.axvline(x0, color="grey", lw=0.5, ls="--") ax.set_xlabel("Incoming-weight angle [deg]"); ax.set_ylabel("Density") ax.set_title("Top-50% by ‖w‖ — angle density") ax.legend(fontsize=8, loc="upper right") fig.tight_layout() fig.savefig(ROOT/"angle_compare.pdf", bbox_inches="tight") print("Wrote", ROOT/"angle_compare.pdf")