test: assert the two RP properties that need no experiment
One parameter set serves any entity count, and entity order carries no meaning. Both are properties of the construction rather than results, and both are what distinguishes the RP from a fixed-weight layer -- which is not worse at variable N, it is undefined there. Permutation equivariance was already asserted at the RP level; the Organism keys memory by entity id, which is a second place order could leak, so it gets its own test. Checked against a broken RP (an index-dependent term in the pooled message): both permutation tests fail there and no others do. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HCydQraDSoZ9WoTm2v23Eb
This commit is contained in:
1 file changed
+35
@@ -179,3 +179,38 @@ def test_detach_memory_cuts_graph():
|
|||||||
_, mem = org(attrs, r, ids, (0.0, 0.0), {})
|
_, mem = org(attrs, r, ids, (0.0, 0.0), {})
|
||||||
assert all(m.requires_grad for m in mem.values())
|
assert all(m.requires_grad for m in mem.values())
|
||||||
assert not any(m.requires_grad for m in detach_memory(mem).values())
|
assert not any(m.requires_grad for m in detach_memory(mem).values())
|
||||||
|
|
||||||
|
|
||||||
|
# --- Property card rows: what the RP is, asserted rather than assumed. -------------------
|
||||||
|
# These two are not experiments and must never become them. They state properties that hold
|
||||||
|
# by construction, and exist so a future change cannot remove one silently. Both were checked
|
||||||
|
# against a deliberately broken RP (see results/rp_properties.md) and both failed there.
|
||||||
|
|
||||||
|
|
||||||
|
def test_rp_same_parameters_any_n():
|
||||||
|
"""One parameter set serves any entity count. A fixed-weight layer over N entities is
|
||||||
|
not worse here, it is undefined: its input dimension is N (or N^2) and has to be chosen."""
|
||||||
|
rp = RelationalPerceptron(D_ATTR, K, E, C, hidden_dim=H)
|
||||||
|
before = [p.clone() for p in rp.parameters()]
|
||||||
|
for n in (2, 7, 32):
|
||||||
|
attrs, coords = torch.randn(n, D_ATTR), torch.rand(n, K)
|
||||||
|
edge = torch.zeros(n, n, E); edge[..., 0] = 1.0
|
||||||
|
h = rp(attrs, flat_diff(coords), edge, torch.ones(n, n), torch.tensor([0.1, 0.2]))
|
||||||
|
assert h.shape == (n, H)
|
||||||
|
assert all(torch.equal(a, b) for a, b in zip(before, rp.parameters())) # no shape adapted
|
||||||
|
|
||||||
|
|
||||||
|
def test_organism_permutation_equivariance_with_memory():
|
||||||
|
"""The RP-level test uses positional tensors; the Organism keys memory by entity id, which
|
||||||
|
is a second place the order could leak in."""
|
||||||
|
org = make_org(memory=True)
|
||||||
|
attrs, coords, _, _, _ = make_inputs()
|
||||||
|
r, ids = flat_diff(coords), list(range(N))
|
||||||
|
mem = {i: torch.randn(H) for i in ids}
|
||||||
|
y0, m0 = org(attrs, r, ids, (0.0, 0.0), mem)
|
||||||
|
|
||||||
|
perm = torch.randperm(N)
|
||||||
|
ids_p = [ids[k] for k in perm.tolist()]
|
||||||
|
y1, m1 = org(attrs[perm], flat_diff(coords[perm]), ids_p, (0.0, 0.0), mem)
|
||||||
|
assert torch.allclose(y0[perm], y1, atol=1e-5)
|
||||||
|
assert all(torch.allclose(m0[i], m1[i], atol=1e-5) for i in ids)
|
||||||
Reference in new issue
Block a user