From 8146511a6450eda49cfc8dd69be9c04a1e3699f5 Mon Sep 17 00:00:00 2001 From: lhj <403133128@qq.com> Date: Thu, 3 Dec 2026 19:14:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(spatial):=20=E6=B6=88=E8=9E=8D=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E4=B8=8E=E6=82=AC=E7=A9=BA=E8=BE=B9=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "机制存活于自身的移除"说明它从未被用上。report 增加 --ablate,评估时把 dangling / scene / edge_feat 清零再算;另加 dang 列,显示每个家族平均有几条 悬空边,避免把"没输入"误读成"没用上"。 --- spatial/cli.py | 4 +++- spatial/report.py | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/spatial/cli.py b/spatial/cli.py index 8e8da56..8999cb2 100644 --- a/spatial/cli.py +++ b/spatial/cli.py @@ -216,7 +216,7 @@ def cmd_eval(args) -> int: def cmd_report(args) -> int: from . import report - report.report(args.config, n=args.n) + report.report(args.config, n=args.n, ablate=args.ablate) return 0 @@ -233,6 +233,8 @@ def main(argv=None) -> int: sp.add_argument("--resume", action="store_true") if name == "report": sp.add_argument("--n", type=int, default=128) + sp.add_argument("--ablate", default=None, + choices=[None, "dangling", "scene", "edge_feat"]) sp.set_defaults(fn=fn) args = p.parse_args(argv) return args.fn(args) diff --git a/spatial/report.py b/spatial/report.py index 26f52f3..ed2bde3 100644 --- a/spatial/report.py +++ b/spatial/report.py @@ -29,11 +29,15 @@ ANSWERS = ("true", "false", "unknown") @torch.no_grad() -def family_row(model, family: str, seed: int, n: int, max_entities: int = 32) -> dict: +def family_row(model, family: str, seed: int, n: int, max_entities: int = 32, + ablate: str = None) -> dict: batch = [generator.make(family, seed=seed, index=i) for i in range(n)] device = next(model.parameters()).device t = collate(batch, max_entities) t = {k: (v.to(device) if torch.is_tensor(v) else v) for k, v in t.items()} + if ablate: + # a mechanism that survives its own removal was never used + t[ablate] = torch.zeros_like(t[ablate]) out = model(t["feat"], t["pad_mask"], t["edge_index"], t["edge_type"], t["edge_mask"], t["q_ents"], t["q_kind"], t["edit_op"], t["edit_idx"], t["edge_removed"], t["edge_feat"], t["scene"], t["dangling"]) @@ -70,7 +74,7 @@ def family_row(model, family: str, seed: int, n: int, max_entities: int = 32) -> } -def report(config_path: str, n: int = 128) -> None: +def report(config_path: str, n: int = 128, ablate: str = None) -> None: root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ckpt = os.path.join(root, "out_spatial", os.path.basename(config_path).replace(".json", ""), "ckpt.pt") @@ -78,12 +82,16 @@ def report(config_path: str, n: int = 128) -> None: model.eval() print(f"step {step} families: train={cfg['data']['train_families']} " f"val={cfg['data']['val_families']}") - head = f"{'family':11s} {'n':>3s} {'dec':>4s} {'unk':>4s} {'acc':>5s} {'abst':>5s} " \ - f"{'cert':>5s} {'#true':>5s} {'conf':>5s}" + if ablate: + print(f"ABLATION: {ablate} zeroed at eval time") + head = f"{'family':11s} {'n':>3s} {'dec':>4s} {'unk':>4s} {'dang':>4s} {'acc':>5s} " \ + f"{'abst':>5s} {'cert':>5s} {'#true':>5s} {'conf':>5s}" print(head) print("-" * len(head)) for fam in generator.FAMILIES: - r = family_row(model, fam, cfg["seed"], n, cfg["model"]["max_entities"]) + ndang = sum(len(generator.make(fam, cfg["seed"], i).obs.dangling) for i in range(8)) / 8 + r = family_row(model, fam, cfg["seed"], n, cfg["model"]["max_entities"], ablate) print(f"{r['family']:11s} {r['n']:3d} {r['decidable']:4d} {r['unknown']:4d} " + f"{ndang:4.1f} " f"{r['acc']:5.3f} {r['abst']:5.3f} {r['cert']:5.3f} {r['n_true']:5d} " f"{r['conf']:5.3f}")