Download plot_kstep_probe_acc.py from Kalso42/WorldModelForMaze: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/datasets/Kalso42/WorldModelForMaze/resolve/1e09a35a0fa3229c8eaa8a4871f2ecd882a67a96/plot_kstep_probe_acc.py
- Command line
-
hf download hf://datasets/Kalso42/WorldModelForMaze@1e09a35a0fa3229c8eaa8a4871f2ecd882a67a96/plot_kstep_probe_acc.py
-
curl -L -o plot_kstep_probe_acc.py https://huggingface.co/datasets/Kalso42/WorldModelForMaze/resolve/1e09a35a0fa3229c8eaa8a4871f2ecd882a67a96/plot_kstep_probe_acc.py
5.01 kB
| """Plot k-step detour metrics vs k for the architectures on one figure per metric. | |
| Data is loaded automatically from the kdetour result files saved by | |
| maze_kstep_detour_test.py (out/maze_kdetour/kdetour_*.npz, key 'table' with | |
| column names in 'columns'). You only need to set TASK / DATASET / configs | |
| (via the command line, see --help). One PNG is produced per metric in | |
| METRICS, written to out/plot/. | |
| """ | |
| import os | |
| import argparse | |
| import numpy as np | |
| import matplotlib | |
| matplotlib.use('Agg') | |
| import matplotlib.pyplot as plt | |
| # --------------------------------------------------------------------------- | |
| # Defaults (overridable on the command line, see --help). | |
| # --------------------------------------------------------------------------- | |
| TASK = 'I1' | |
| DATASET = '10M' | |
| # Model configs: transformer-family uses TF_CONFIG, RNN/SSM-family uses RNN_CONFIG. | |
| TF_CONFIG = '6_6_384' | |
| RNN_CONFIG = '12_384' | |
| # kdetour result settings (match how the .npz files were produced). | |
| CKPT_ITER = 10000 | |
| PATH_TYPE = 'RWs' | |
| KDETOUR_DIR = 'out/maze_kdetour' | |
| OUT_DIR = 'out/plot' | |
| # (display name, file model key, is_transformer_family). Configs come from args. | |
| MODEL_SPECS = [ | |
| ('Transformer', 'transformer', True), | |
| ('Transformer-NextLat', 'transformer_nextlat', True), | |
| ('Mamba', 'mamba', False), | |
| ('Mamba-2', 'mamba2', False), | |
| ('Gated-DeltaNet', 'gated_deltanet', False), | |
| ('GRU', 'gru', False), | |
| ] | |
| MARKERS = ['o', 's', '^', 'D', 'P', 'v'] | |
| # Each metric maps to a column name in the kdetour table. | |
| # ylim=None means autoscale (used for JSD which is not a percentage). | |
| METRICS = [ | |
| ('probe_acc', 'probe accuracy (%)', (-2, 105)), | |
| ('c_match_jsd', 'c_match_jsd', None), | |
| ('reach_acc', 'reach accuracy (%)', (-2, 105)), | |
| ] | |
| def parse_args(): | |
| p = argparse.ArgumentParser(description='Plot k-step detour metrics from kdetour npz files.') | |
| p.add_argument('--task', default=TASK) | |
| p.add_argument('--dataset', default=DATASET) | |
| p.add_argument('--tf_config', default=TF_CONFIG) | |
| p.add_argument('--rnn_config', default=RNN_CONFIG) | |
| p.add_argument('--ckpt_iter', type=int, default=CKPT_ITER) | |
| p.add_argument('--path_type', default=PATH_TYPE) | |
| p.add_argument('--kdetour_dir', default=KDETOUR_DIR) | |
| p.add_argument('--out_dir', default=OUT_DIR) | |
| return p.parse_args() | |
| def load_table(args, model_key, config): | |
| """Return (k array, {col_name: values}) from the kdetour npz, or (None, None).""" | |
| fname = f'kdetour_{args.task}_{args.path_type}_{args.ckpt_iter}_{args.dataset}_{model_key}_{config}.npz' | |
| path = os.path.join(args.kdetour_dir, fname) | |
| if not os.path.exists(path): | |
| print(f"[missing] {path}") | |
| return None, None | |
| d = np.load(path, allow_pickle=True) | |
| if 'table' not in d or 'columns' not in d: | |
| print(f"[no table] {path}") | |
| return None, None | |
| table = np.asarray(d['table'], dtype=float) | |
| cols = [str(c) for c in d['columns']] | |
| by_col = {c: table[:, i] for i, c in enumerate(cols)} | |
| k = by_col.get('k') | |
| return k, by_col | |
| def plot_metric(args, loaded, metric, ylabel, ylim): | |
| plt.figure(figsize=(9, 5.5)) | |
| all_k = None | |
| plotted = 0 | |
| for (name, key, is_tf), marker in zip(MODEL_SPECS, MARKERS): | |
| k, by_col = loaded[key] | |
| if k is None or metric not in by_col: | |
| continue | |
| config = args.tf_config if is_tf else args.rnn_config | |
| plt.plot(k, by_col[metric], marker=marker, markersize=6, linewidth=2, | |
| label=f'{name} ({config})') | |
| if all_k is None or len(k) > len(all_k): | |
| all_k = k | |
| plotted += 1 | |
| if plotted == 0: | |
| print(f"[{metric}] nothing to plot: no kdetour npz files found.") | |
| plt.close() | |
| return | |
| plt.xlabel('detour length k (steps)', fontsize=12) | |
| plt.ylabel(ylabel, fontsize=12) | |
| plt.title(f'k-step detour {metric} (Task {args.task}, {args.dataset})', fontsize=13) | |
| ax = plt.gca() | |
| ticks = [int(x) for x in all_k] | |
| ax.set_xticks(ticks) | |
| ax.set_xticklabels([str(t) for t in ticks]) | |
| for t, lbl in zip(ticks, ax.get_xticklabels()): | |
| if t >= 100: | |
| lbl.set_color('red') | |
| if ylim is not None: | |
| plt.ylim(*ylim) | |
| plt.grid(True, alpha=0.3) | |
| plt.legend(fontsize=10, framealpha=0.9) | |
| plt.tight_layout() | |
| out = os.path.join( | |
| args.out_dir, | |
| f'kstep_{metric}_{args.task}_{args.dataset}_{args.tf_config}_{args.rnn_config}.png') | |
| plt.savefig(out, dpi=150) | |
| plt.close() | |
| print(f"Saved figure to {out}") | |
| def main(): | |
| args = parse_args() | |
| os.makedirs(args.out_dir, exist_ok=True) | |
| loaded = {} | |
| for name, key, is_tf in MODEL_SPECS: | |
| config = args.tf_config if is_tf else args.rnn_config | |
| loaded[key] = load_table(args, key, config) | |
| for metric, ylabel, ylim in METRICS: | |
| plot_metric(args, loaded, metric, ylabel, ylim) | |
| if __name__ == '__main__': | |
| main() | |