diff --git a/spatial/model.py b/spatial/model.py index a4d9af5..bdc3c25 100644 --- a/spatial/model.py +++ b/spatial/model.py @@ -303,7 +303,10 @@ class RelationBackbone(nn.Module): # backward pass simple and it makes "an entity receiving three edges sums # three vectors" obvious. Dangling edges are excluded: their far end is # not an entity, and pretending otherwise would credit an arbitrary node - # with a relation it does not have. + # with a relation it does not have. Removed edges are excluded too, for + # the same reason the hop distance excludes them: the question is about + # the graph after the cut, and an entity whose value still sums a cut + # constraint has to un-learn it before it can answer. inc = torch.zeros(B, n_ent, E, device=device) cols = torch.arange(E, device=device).view(1, E).expand(B, E) ones = torch.ones_like(cols, dtype=inc.dtype) @@ -311,7 +314,10 @@ class RelationBackbone(nn.Module): edge_index[..., 0].clamp(min=0, max=n_ent - 1), cols), ones) inc.index_put_((ar.unsqueeze(1).expand(B, E), edge_index[..., 1].clamp(min=0, max=n_ent - 1), cols), ones) - inc = inc * (edge_mask & ~has_unk).unsqueeze(1).to(inc.dtype) + live = edge_mask & ~has_unk + if edge_removed is not None: + live = live & ~(edge_removed > 0) + inc = inc * live.unsqueeze(1).to(inc.dtype) msg = torch.bmm(inc, ef) # query token is read out of the two entities it names