stgfn-repro-code / analyze_tune.py
GwenTsang's picture
Upload folder using huggingface_hub
cdd14d2 verified
Raw
History Blame Contribute Delete
3.36 kB
"""Summarise the ST-GFN hyperparameter grid against the TB reference.
The point of this sweep is fairness: a null result for the proposed method is
only meaningful if the method was not simply mis-tuned. We report the best
ST-GFN configuration found anywhere in the grid, per metric, and compare it with
plain TB run under an identical budget.
"""
from __future__ import annotations
import argparse
import glob
import json
import os
from collections import defaultdict
import numpy as np
METRICS = [("modes_found", "Modes", True),
("l1_to_target", "L1 to P*", False),
("mean_reward", "Mean R", True)]
def load(out_dir):
by = defaultdict(lambda: defaultdict(list)) # config -> method -> runs
for path in glob.glob(os.path.join(out_dir, "*", "*.json")):
cfg = os.path.basename(os.path.dirname(path))
with open(path) as f:
r = json.load(f)
by[cfg][r["method"]].append(r["final"])
return by
def agg(runs, key):
vals = [r.get(key) for r in runs if r.get(key) is not None]
if not vals:
return None, None
mu = float(np.mean(vals))
ci = float(1.96 * np.std(vals, ddof=1) / np.sqrt(len(vals))) if len(vals) > 1 else 0.0
return mu, ci
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--out", default="../outputs/tune")
ap.add_argument("--save", default="../outputs/summary_tune.json")
args = ap.parse_args()
by = load(args.out)
rows, tb_rows = [], []
for cfg, methods in sorted(by.items()):
for meth, runs in methods.items():
entry = {"config": cfg, "method": meth, "n": len(runs)}
for key, _, _ in METRICS:
mu, ci = agg(runs, key)
entry[key] = mu
entry[key + "_ci"] = ci
(tb_rows if meth == "tb" else rows).append(entry)
print(f"{'config':38s} {'n':>2s} " + " ".join(f"{n:>14s}" for _, n, _ in METRICS))
for r in sorted(rows, key=lambda x: (x["l1_to_target"] is None, x["l1_to_target"])):
cells = []
for key, _, _ in METRICS:
v, ci = r.get(key), r.get(key + "_ci")
cells.append(f"{v:9.3f}±{ci:4.3f}" if v is not None else f"{'-':>14s}")
print(f"{r['config']:38s} {r['n']:2d} " + " ".join(cells))
if tb_rows:
print("\nTB reference (identical budget):")
for r in tb_rows:
cells = []
for key, _, _ in METRICS:
v, ci = r.get(key), r.get(key + "_ci")
cells.append(f"{v:9.3f}±{ci:4.3f}" if v is not None else f"{'-':>14s}")
print(f"{'tb (baseline)':38s} {r['n']:2d} " + " ".join(cells))
print("\nBest ST-GFN configuration per metric vs TB:")
for key, name, hib in METRICS:
cand = [r for r in rows if r.get(key) is not None]
if not cand:
continue
best = (max if hib else min)(cand, key=lambda r: r[key])
tb = tb_rows[0].get(key)
verdict = ("ST-GFN better" if ((best[key] > tb) == hib) else "TB better")
print(f" {name:10s} best ST-GFN {best[key]:.3f} ({best['config']})"
f" vs TB {tb:.3f} -> {verdict}")
with open(args.save, "w") as f:
json.dump({"stgfn": rows, "tb": tb_rows}, f, indent=2)
print(f"\nwrote {args.save}")