exp: type-by-colour edge types, twelve kernels instead of six

The type-conditional kernel lost 0.14 on pawns and nothing elsewhere, because e_ij carried the
piece type and not its colour: a white pawn and a black pawn share one kernel and their moves
are mirrored. typecol gives each colour its own.
This commit is contained in:
chrnx committed 2026-09-02 19:59:14 -05:00
1 parent 4567c2e18b
commit 67cf06bd71
1 file changed
+25 -9
+25 -9
View File
@@ -199,19 +199,35 @@ def evaluate(scores, batch, tsum):
return list(hit[keep].numpy()), list((k[keep] / GRID).numpy())
def run(n_train: int, seed_train: int, test, hidden: int, cppn_hidden: int, seed: int, lr: float, batch: int,
edges: bool) -> dict:
EDGES = {"none": 1, "type": 6, "typecol": 12}
def _edge_types(pos: Position, kind: str, n: int):
"""What the CPPN is told about the pieces it relates, beyond their geometry.
`type` shares one kernel between a white pawn and a black pawn, whose moves are mirrored --
which is why the type-conditional arm lost 0.14 on pawns and nothing elsewhere. `typecol`
gives each colour its own.
"""
if kind == "none":
return None
t = torch.from_numpy(pos.types if kind == "type"
else pos.types * 2 + (1 - pos.attrs[:, 6].astype(np.int64)))
return t[:, None].expand(n, n).contiguous()
def run(n_train: int, seed_train: int, test, hidden: int, cppn_hidden: int, seed: int,
lr: float, batch: int, edges: str) -> dict:
torch.manual_seed(seed)
org = Organism(ATTR_DIM, 2, GRID, n_edge_types=6 if edges else 1, hidden_dim=hidden,
org = Organism(ATTR_DIM, 2, GRID, n_edge_types=EDGES[edges], hidden_dim=hidden,
cppn_hidden=cppn_hidden, memory=False)
opt = torch.optim.Adam(org.parameters(), lr=lr)
zero = Affect().axes
pending = []
for pos in stream(n_train, seed_train):
n = len(pos.types)
et = (torch.from_numpy(pos.types)[:, None].expand(n, n).contiguous() if edges else None)
pred, _ = org(torch.from_numpy(pos.attrs), flat_diff(torch.from_numpy(pos.coords)),
list(range(n)), zero, {}, edge_types=et)
list(range(n)), zero, {}, edge_types=_edge_types(pos, edges, n))
pending.append(torch.nn.functional.binary_cross_entropy_with_logits(
pred, torch.from_numpy(pos.y)))
if len(pending) == batch:
@@ -221,9 +237,8 @@ def run(n_train: int, seed_train: int, test, hidden: int, cppn_hidden: int, seed
with torch.no_grad():
for pos in test:
n = len(pos.types)
et = (torch.from_numpy(pos.types)[:, None].expand(n, n).contiguous() if edges else None)
pred, _ = org(torch.from_numpy(pos.attrs), flat_diff(torch.from_numpy(pos.coords)),
list(range(n)), zero, {}, edge_types=et)
list(range(n)), zero, {}, edge_types=_edge_types(pos, edges, n))
p, c = evaluate(pred.numpy(), pos, tsum)
prec += p; chance += c
return dict(seed=seed, params=sum(q.numel() for q in org.parameters()),
@@ -258,7 +273,7 @@ def main() -> None:
ap.add_argument("--cppn-hidden", type=int, default=32)
ap.add_argument("--lr", type=float, default=1e-3)
ap.add_argument("--batch", type=int, default=16)
ap.add_argument("--arms", default="constant,shape,rp,rp_type")
ap.add_argument("--arms", default="constant,shape,rp,rp_type,rp_typecol")
ap.add_argument("--out", default="results/chess_rules.json")
a = ap.parse_args()
@@ -273,7 +288,8 @@ def main() -> None:
r = (run_baseline(min(a.positions, 5000), seed, test, seed, name)
if name in ("constant", "shape") else
run(a.positions, seed, test, a.hidden, a.cppn_hidden, seed, a.lr, a.batch,
name == "rp_type")) | {"arm": name}
{"rp": "none", "rp_type": "type",
"rp_typecol": "typecol"}[name])) | {"arm": name}
print(f"{name:<10} seed={seed} params={r['params']:<6} prec@k={r['prec']:.4f}",
flush=True)
results.append(r)