feat(spatial): 跳数偏置、边内容进 value、证据回灌第二轮、学习温度
四件结构补齐,每件都有对应的失效模式: - 跳数偏置:原来只靠 no-position 的注意力,实体顺序被打乱会让输出漂移。 改为按截断最短路分桶的注意力偏置(HOP_MAX=4),并遵守干预语义——被切掉的 边不参与连通,否则"剪掉主干后是否还连通"的跳数本身就是错的。 - 边内容进 value:边类型此前只是注意力偏置,只能表达"看那边",不能表达 "那是一个 0.8 的接触面、锁的是 tz 轴"。现在把入射边的特征投影后累加进实体 的 value,接触面积由两端 box 重叠算出,并按单位归一化。 - 证据回灌:第一轮选出 top-k 边作为候选凭证,第二轮让所有实体都能注意到 自己所在的那张凭证。此前证据头和答案头看的根本不是同一个对象。 - 学习温度:答案/变化/证据 logits 统一除以 exp(log_temp),并在评估里加 ECE。 只报准确率的话,置信度 0.99 而正确率 0.72 和直接报 0.72 是同一个数。 另外:把边偏置的累加改成扁平 index_put_(accumulate),平行边不再互相覆盖; 新增 configs/cross.json(训练 cond.p1/area.sup2/dof.c6,验证 held-out 家族)。
This commit is contained in:
1 parent
f31288bf06
commit
085ad1415f
5 files changed
+318
-69
No files matched your search
+41
-4
@@ -11,8 +11,8 @@ import torch
|
||||
|
||||
from collections import deque
|
||||
|
||||
from .schema import ANSWERS, CHANGES, EDGE_KINDS, ENTITY_KINDS, Scenario
|
||||
from .model import FEAT_DIM, QUERY_KINDS
|
||||
from .schema import ANSWERS, CHANGES, DOF_AXES, EDGE_KINDS, ENTITY_KINDS, Scenario
|
||||
from .model import EDGE_FEAT_DIM, FEAT_DIM, N_AXIS, N_EDGE, QUERY_KINDS
|
||||
|
||||
|
||||
def _reach(edges, src: str, alive) -> set:
|
||||
@@ -74,6 +74,39 @@ def entity_features(sc: Scenario, e) -> list:
|
||||
return f
|
||||
|
||||
|
||||
def _contact_area(by_id: dict, ed, per_unit: int = 1) -> float:
|
||||
"""Overlap area of two boxes, normalised so a unit change cannot move it.
|
||||
|
||||
Stability depends on how much material two blocks share, not on the fact
|
||||
that they touch; without this the model has to recompute geometry from two
|
||||
coordinate vectors that the value stream never even carried.
|
||||
"""
|
||||
a, b = by_id.get(ed.a), by_id.get(ed.b)
|
||||
if a is None or b is None:
|
||||
return 0.0
|
||||
ba, bb = a.attrs.get("box"), b.attrs.get("box")
|
||||
if not ba or not bb:
|
||||
return 0.0
|
||||
ox = min(ba[3], bb[3]) - max(ba[0], bb[0])
|
||||
oy = min(ba[4], bb[4]) - max(ba[1], bb[1])
|
||||
if ox <= 0 or oy <= 0:
|
||||
return 0.0
|
||||
return min(1.0, ox * oy / (400.0 * max(1, per_unit) ** 2))
|
||||
|
||||
|
||||
def edge_features(sc: Scenario, ed, removed: bool) -> list:
|
||||
f = [0.0] * EDGE_FEAT_DIM
|
||||
f[EDGE_KINDS.index(ed.kind)] = 1.0
|
||||
ax = ed.attrs.get("axis")
|
||||
if ax in DOF_AXES:
|
||||
f[N_EDGE + DOF_AXES.index(ax)] = 1.0
|
||||
f[N_EDGE + N_AXIS] = 1.0 if removed else 0.0
|
||||
f[N_EDGE + N_AXIS + 1] = ed.layer / 6.0
|
||||
by_id = {e.eid: e for e in sc.obs.entities}
|
||||
f[N_EDGE + N_AXIS + 2] = _contact_area(by_id, ed, sc.obs.units.get("per_unit", 1))
|
||||
return f
|
||||
|
||||
|
||||
def collate(batch, max_entities: int = 32):
|
||||
B = len(batch)
|
||||
N = max(1, max(len(sc.obs.entities) for sc in batch))
|
||||
@@ -86,6 +119,7 @@ def collate(batch, max_entities: int = 32):
|
||||
pad_mask = torch.ones(B, N, dtype=torch.bool)
|
||||
edge_index = torch.zeros(B, E, 2, dtype=torch.long)
|
||||
edge_type = torch.zeros(B, E, dtype=torch.long)
|
||||
edge_feat = torch.zeros(B, E, EDGE_FEAT_DIM)
|
||||
edge_mask = torch.zeros(B, E, dtype=torch.bool)
|
||||
evidence = torch.zeros(B, E)
|
||||
edge_removed = torch.zeros(B, E)
|
||||
@@ -102,14 +136,16 @@ def collate(batch, max_entities: int = 32):
|
||||
feat[i, j] = torch.tensor(entity_features(sc, e))
|
||||
pad_mask[i, j] = False
|
||||
ev = evidence_target(sc)
|
||||
removed_eid = (sc.query.edit or {}).get("edge")
|
||||
for k, ed in enumerate(sc.obs.edges):
|
||||
edge_index[i, k, 0] = ids.get(ed.a, 0)
|
||||
edge_index[i, k, 1] = ids.get(ed.b, 0)
|
||||
edge_type[i, k] = EDGE_KINDS.index(ed.kind)
|
||||
edge_mask[i, k] = True
|
||||
evidence[i, k] = 1.0 if ed.eid in ev else 0.0
|
||||
if (sc.query.edit or {}).get("edge") == ed.eid:
|
||||
edge_removed[i, k] = 1.0
|
||||
rm = ed.eid == removed_eid
|
||||
edge_removed[i, k] = 1.0 if rm else 0.0
|
||||
edge_feat[i, k] = torch.tensor(edge_features(sc, ed, rm))
|
||||
q_ents[i, 0] = ids.get(sc.query.a, -1)
|
||||
q_ents[i, 1] = ids.get(sc.query.b, -1)
|
||||
q_kind[i] = QUERY_KINDS.index(sc.query.kind)
|
||||
@@ -130,6 +166,7 @@ def collate(batch, max_entities: int = 32):
|
||||
return {
|
||||
"feat": feat, "pad_mask": pad_mask,
|
||||
"edge_index": edge_index, "edge_type": edge_type, "edge_mask": edge_mask,
|
||||
"edge_feat": edge_feat,
|
||||
"q_ents": q_ents, "q_kind": q_kind,
|
||||
"edit_op": edit_op, "edit_idx": edit_idx,
|
||||
"edge_removed": edge_removed,
|
||||
|
||||
Reference in new issue
Block a user