feat: D4-invariant kernel input recovers the knight rule from partial support

InvariantCPPN canonicalises the displacement to (min|dx|, max|dy|), collapsing
the square's eight symmetries to one orbit, plus a knight_sym teacher whose
eight L-squares carry one value and are therefore a genuine D4 orbit.

Trained on six of the eight, it recovers the other two at 0.4% of the floor on
6/6 seeds where the raw-displacement kernel sits above the floor.

Two controls, both predicted beforehand: on a teacher whose eight squares share
a support but not a value the invariance is worse than nothing, and on a smooth
teacher with no D4 symmetry it costs 31x.

The arm dispatch now raises on unknown names -- an edit had silently failed to
land and rp_inv ran as plain rp, which looked like a null.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HCydQraDSoZ9WoTm2v23Eb
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-09-07 19:12:03 -05:00
1 parent 3feba1baac
commit ff8ccbc819
4 files changed
+186 -13

No files matched your search

+42 -4
View File
@@ -61,6 +61,8 @@ def gabor_kernel(sigma: float = 3.0, seed: int = 0, kfreq: float = 0.35,
smooth a band-limited Gabor. The replication: a blur should match the CPPN.
highfreq the same, at a frequency near the grid's resolution. Structured, not smooth.
knight_sym 1 on the eight L-offsets with the SAME value at all eight, 0 elsewhere. A
genuine D4 orbit: the eight squares are one rule under the square's symmetries.
knight 1 on the eight L-offsets, 0 elsewhere. NOT smooth and RULE-GENERATED -- a
neighbour average cannot recover an isolated spike, since its neighbours are 0.
This is chess_reach.md's knight, asked as a mechanism question.
@@ -73,12 +75,17 @@ def gabor_kernel(sigma: float = 3.0, seed: int = 0, kfreq: float = 0.35,
r = torch.stack([xs, ys], -1).reshape(CELLS, 2).float()
if kind == "noise":
return torch.randn(CELLS, D, generator=g) * 0.2
if kind == "knight":
if kind in ("knight", "knight_sym"):
w = torch.zeros(CELLS, D)
sign = torch.randint(0, 2, (D, len(KNIGHT)), generator=g).float() * 2 - 1
# knight_sym gives every channel ONE value across all eight offsets, so the pattern is a
# true D4 orbit and an invariant input can express it. knight varies the sign per offset,
# so the eight squares share a support but NOT a rule -- the control that keeps
# "invariants win" from being indistinguishable from "invariants always win".
sign = torch.randint(0, 2, (D, 1 if kind == "knight_sym" else len(KNIGHT)),
generator=g).float() * 2 - 1
for c in range(D):
for m, (dx, dy) in enumerate(KNIGHT):
w[(dy + R) * G + (dx + R), c] = sign[c, m]
w[(dy + R) * G + (dx + R), c] = sign[c, 0 if kind == "knight_sym" else m]
return w
if kind == "highfreq":
kfreq = 1.5
@@ -88,6 +95,31 @@ def gabor_kernel(sigma: float = 3.0, seed: int = 0, kfreq: float = 0.35,
return env * torch.cos(r @ k.T + phi)
class InvariantCPPN(nn.Module):
"""The CPPN over D4 invariants of the displacement instead of the displacement itself.
The knight null in `sharp.md` says nothing here induces a symmetry group from partial
support. This asks the other question: the CPPN sees raw r_ij, and has no reason to know
that (1,2) and (-2,-1) are the same law -- they are two different points of the plane.
Canonicalising to (min|dx|,max|dy|) collapses the square's eight symmetries to one orbit,
and the knight's eight squares become a single input.
STATED PLAINLY BECAUSE A CRITIC WILL SAY IT: this does not sneak the held-out cells into
training. It hands the model the CLAIM that those cells are equivalent to ones it did see.
The experiment measures whether that claim, when it is true of the domain, converts unseen
displacements into seen ones -- and the `knight` teacher, whose eight squares share a
support but not a value, is the control for when the claim is FALSE.
"""
def __init__(self, cppn: nn.Module):
super().__init__()
self.cppn = cppn
def forward(self, r, edge_onehot, context):
a = r.abs()
inv = torch.stack([a.min(-1).values, a.max(-1).values], dim=-1)
return self.cppn(inv, edge_onehot, context)
class TableKernel(nn.Module):
"""One weight vector per discrete displacement: a GNN with one edge type per relation.
@@ -114,6 +146,12 @@ def make_model(arm: str, seed: int, omega: float = 1.0) -> nn.Module:
rp = RelationalPerceptron(ATTR, 2, 1, 2, hidden_dim=D, cppn_hidden=32, omega=omega)
if arm.startswith("table"):
rp.cppn = TableKernel() # ponytail: swap in place so all arms share RP's exact forward
elif arm.startswith("rp_inv"):
rp.cppn = InvariantCPPN(rp.cppn)
elif arm != "rp":
# Exact, not a prefix check: "rp_inv" typed as "rp_invariant" or "rp_nv" would otherwise
# fall through and run as plain rp, which is how this went unnoticed the first time.
raise ValueError(f"unknown arm {arm!r}")
return nn.ModuleDict({"rp": rp, "out": nn.Linear(D, D)})
@@ -307,7 +345,7 @@ def main():
ap.add_argument("--omega", type=float, default=1.0)
ap.add_argument("--kfreq", type=float, default=0.35, help="the DOMAIN's spatial frequency")
ap.add_argument("--teacher", default="smooth",
choices=["smooth", "highfreq", "knight", "noise"])
choices=["smooth", "highfreq", "knight", "knight_sym", "noise"])
ap.add_argument("--out", default="results/unseen.json")
args = ap.parse_args()