feat(spatial): 消融参数与悬空边计数列

"机制存活于自身的移除"说明它从未被用上。report 增加 --ablate,评估时把
dangling / scene / edge_feat 清零再算;另加 dang 列,显示每个家族平均有几条
悬空边,避免把"没输入"误读成"没用上"。
This commit is contained in:
liuhuajie committed 2026-12-03 19:14:04 +08:00
1 parent e4c314b31a
commit 8146511a64
2 files changed
+16 -6

No files matched your search

+13 -5
View File
@@ -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}")