feat(spatial): 消融参数与悬空边计数列
"机制存活于自身的移除"说明它从未被用上。report 增加 --ablate,评估时把 dangling / scene / edge_feat 清零再算;另加 dang 列,显示每个家族平均有几条 悬空边,避免把"没输入"误读成"没用上"。
This commit is contained in:
1 parent
e4c314b31a
commit
8146511a64
2 files changed
+16
-6
No files matched your search
+3
-1
@@ -216,7 +216,7 @@ def cmd_eval(args) -> int:
|
|||||||
|
|
||||||
def cmd_report(args) -> int:
|
def cmd_report(args) -> int:
|
||||||
from . import report
|
from . import report
|
||||||
report.report(args.config, n=args.n)
|
report.report(args.config, n=args.n, ablate=args.ablate)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@@ -233,6 +233,8 @@ def main(argv=None) -> int:
|
|||||||
sp.add_argument("--resume", action="store_true")
|
sp.add_argument("--resume", action="store_true")
|
||||||
if name == "report":
|
if name == "report":
|
||||||
sp.add_argument("--n", type=int, default=128)
|
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)
|
sp.set_defaults(fn=fn)
|
||||||
args = p.parse_args(argv)
|
args = p.parse_args(argv)
|
||||||
return args.fn(args)
|
return args.fn(args)
|
||||||
|
|||||||
+13
-5
@@ -29,11 +29,15 @@ ANSWERS = ("true", "false", "unknown")
|
|||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
@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)]
|
batch = [generator.make(family, seed=seed, index=i) for i in range(n)]
|
||||||
device = next(model.parameters()).device
|
device = next(model.parameters()).device
|
||||||
t = collate(batch, max_entities)
|
t = collate(batch, max_entities)
|
||||||
t = {k: (v.to(device) if torch.is_tensor(v) else v) for k, v in t.items()}
|
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"],
|
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["q_ents"], t["q_kind"], t["edit_op"], t["edit_idx"], t["edge_removed"],
|
||||||
t["edge_feat"], t["scene"], t["dangling"])
|
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__)))
|
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 = os.path.join(root, "out_spatial", os.path.basename(config_path).replace(".json", ""),
|
||||||
"ckpt.pt")
|
"ckpt.pt")
|
||||||
@@ -78,12 +82,16 @@ def report(config_path: str, n: int = 128) -> None:
|
|||||||
model.eval()
|
model.eval()
|
||||||
print(f"step {step} families: train={cfg['data']['train_families']} "
|
print(f"step {step} families: train={cfg['data']['train_families']} "
|
||||||
f"val={cfg['data']['val_families']}")
|
f"val={cfg['data']['val_families']}")
|
||||||
head = f"{'family':11s} {'n':>3s} {'dec':>4s} {'unk':>4s} {'acc':>5s} {'abst':>5s} " \
|
if ablate:
|
||||||
f"{'cert':>5s} {'#true':>5s} {'conf':>5s}"
|
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(head)
|
||||||
print("-" * len(head))
|
print("-" * len(head))
|
||||||
for fam in generator.FAMILIES:
|
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} "
|
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['acc']:5.3f} {r['abst']:5.3f} {r['cert']:5.3f} {r['n_true']:5d} "
|
||||||
f"{r['conf']:5.3f}")
|
f"{r['conf']:5.3f}")
|
||||||
Reference in new issue
Block a user