Download plot_cloverleaf.py from arcadia-impact/mean-field-xor-repro-2026-06-29: direct link, hf CLI and curl.
- Browser
- Download file 3.66 kB
-
https://huggingface.co/datasets/arcadia-impact/mean-field-xor-repro-2026-06-29/resolve/main/plot_cloverleaf.py
- Command line
-
hf download hf://datasets/arcadia-impact/mean-field-xor-repro-2026-06-29/plot_cloverleaf.py
-
curl -L -o plot_cloverleaf.py https://huggingface.co/datasets/arcadia-impact/mean-field-xor-repro-2026-06-29/resolve/main/plot_cloverleaf.py
3.66 kB
| """Plot the cloverleaf distribution of (w_1, w_2) from a trained 2-layer net. | |
| Also shows: bias histogram, output-weight histogram, prediction surface.""" | |
| from __future__ import annotations | |
| import argparse | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| import torch | |
| def main(model_path: Path, out_dir: Path, tag: str = "cloverleaf"): | |
| ckpt = torch.load(model_path, map_location="cpu", weights_only=True) | |
| W = ckpt["W"].numpy() # (N, 2) | |
| b = ckpt["b"].numpy() | |
| a = ckpt["a"].numpy() | |
| width = ckpt["width"] | |
| scale = ckpt["scale"] | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| sns.set_theme(style="white", context="paper") | |
| # ---- cloverleaf scatter --------------------------------------------------- | |
| df = pd.DataFrame({"w1": W[:, 0], "w2": W[:, 1], "a": a, "b": b}) | |
| df["sign_a"] = np.where(df["a"] > 0, "+", "−") | |
| g = sns.JointGrid(data=df, x="w1", y="w2", hue="sign_a", | |
| palette={"+": "#1f77b4", "−": "#d62728"}, height=6) | |
| g.plot_joint(sns.scatterplot, s=6, alpha=0.5, edgecolor=None) | |
| g.plot_marginals(sns.kdeplot, common_norm=False, fill=True, alpha=0.3) | |
| g.figure.suptitle(f"Hidden-layer (w₁, w₂) — width={width}, scale={scale}", y=1.02) | |
| g.savefig(out_dir / f"{tag}_scatter.pdf", bbox_inches="tight") | |
| plt.close("all") | |
| # ---- direction histogram (angle of incoming weight) ----------------------- | |
| angle = np.arctan2(W[:, 1], W[:, 0]) | |
| norm = np.linalg.norm(W, axis=1) | |
| df2 = pd.DataFrame({"angle_deg": np.degrees(angle), "norm": norm, | |
| "sign_a": df["sign_a"]}) | |
| fig, axes = plt.subplots(1, 2, figsize=(10, 4)) | |
| sns.histplot(df2, x="angle_deg", hue="sign_a", bins=72, ax=axes[0], | |
| palette={"+": "#1f77b4", "−": "#d62728"}, multiple="stack") | |
| axes[0].set_xlabel("angle of (w₁, w₂) [deg]") | |
| axes[0].set_title("Incoming-weight direction histogram") | |
| # cloverleaf prediction: peaks at ±45° and ±135° | |
| for x0 in [-135, -45, 45, 135]: | |
| axes[0].axvline(x0, color="grey", lw=0.5, ls="--") | |
| sns.histplot(df2, x="norm", bins=60, ax=axes[1], color="grey") | |
| axes[1].set_xlabel("‖w‖₂") | |
| axes[1].set_title("Incoming-weight norms") | |
| fig.tight_layout() | |
| fig.savefig(out_dir / f"{tag}_angle_norm.pdf", bbox_inches="tight") | |
| plt.close(fig) | |
| # ---- prediction surface --------------------------------------------------- | |
| grid = np.linspace(-1, 1, 200) | |
| XX, YY = np.meshgrid(grid, grid) | |
| X = np.stack([XX.ravel(), YY.ravel()], axis=1) | |
| H = np.clip(X @ W.T + b, -1, 1) | |
| pred = H @ a | |
| if scale == "meanfield": | |
| pred = pred / width | |
| elif scale == "ntk": | |
| pred = pred / np.sqrt(width) | |
| pred = pred.reshape(XX.shape) | |
| target = np.sign(XX) * np.sign(YY) | |
| fig, axes = plt.subplots(1, 2, figsize=(10, 4)) | |
| im0 = axes[0].imshow(pred, extent=[-1, 1, -1, 1], origin="lower", | |
| cmap="RdBu_r", vmin=-1.2, vmax=1.2) | |
| axes[0].set_title("Network output") | |
| axes[1].imshow(target, extent=[-1, 1, -1, 1], origin="lower", cmap="RdBu_r") | |
| axes[1].set_title("Target (XOR)") | |
| plt.colorbar(im0, ax=axes, shrink=0.8) | |
| fig.savefig(out_dir / f"{tag}_pred_surface.pdf", bbox_inches="tight") | |
| plt.close(fig) | |
| print(f"Wrote plots under {out_dir}") | |
| if __name__ == "__main__": | |
| p = argparse.ArgumentParser() | |
| p.add_argument("--model", type=Path, required=True) | |
| p.add_argument("--out", type=Path, required=True) | |
| p.add_argument("--tag", default="cloverleaf") | |
| args = p.parse_args() | |
| main(args.model, args.out, args.tag) | |