exp: --edges type on the move task, so the kernel is type-conditional there too

This commit is contained in:
chrnx committed 2026-09-02 18:16:37 -05:00
1 parent f49840f73d
commit e7737b8138
1 file changed
+27 -15
+27 -15
View File
@@ -153,25 +153,27 @@ def load(n_games: int, hist: int = 3, min_plies: int = 20) -> list[list[Ply]]:
return games return games
def n_params(memory: bool, hidden: int, cppn_hidden: int, attr_dim: int = ATTR_DIM) -> int: def n_params(memory: bool, hidden: int, cppn_hidden: int, attr_dim: int = ATTR_DIM,
org = Organism(attr_dim, 2, 2, hidden_dim=hidden, cppn_hidden=cppn_hidden, memory=memory) n_edge: int = 1) -> int:
org = Organism(attr_dim, 2, 2, n_edge_types=n_edge, hidden_dim=hidden,
cppn_hidden=cppn_hidden, memory=memory)
return sum(p.numel() for p in org.parameters()) return sum(p.numel() for p in org.parameters())
def match_width(hidden: int, cppn_hidden: int) -> int: def match_width(hidden: int, cppn_hidden: int, n_edge: int = 1) -> int:
"""The memoryless width whose parameter count lands nearest the gated model's. """The memoryless width whose parameter count lands nearest the gated model's.
`rp_only` at 1442 against `grp_k0` at 5602 is confounded 3.9x and is not a memory `rp_only` at 1442 against `grp_k0` at 5602 is confounded 3.9x and is not a memory
result (`results/frozen.md` §1d). This is how that confound is removed here. result (`results/frozen.md` §1d). This is how that confound is removed here.
""" """
target = n_params(True, hidden, cppn_hidden) target = n_params(True, hidden, cppn_hidden, n_edge=n_edge)
return min(range(hidden, 8 * hidden), return min(range(hidden, 8 * hidden),
key=lambda h: abs(n_params(False, h, cppn_hidden) - target)) key=lambda h: abs(n_params(False, h, cppn_hidden, n_edge=n_edge) - target))
def _stream(org: Organism, game: list[Ply], affect: Affect, acfg: AffectConfig, def _stream(org: Organism, game: list[Ply], affect: Affect, acfg: AffectConfig,
opt: torch.optim.Optimizer | None, bptt: int, use_affect: bool, opt: torch.optim.Optimizer | None, bptt: int, use_affect: bool,
oracle: bool, adjacency: bool): oracle: bool, adjacency: bool, edges: bool):
"""One game as one continuous stream. Memory starts empty: new game, new pieces.""" """One game as one continuous stream. Memory starts empty: new game, new pieces."""
memory: dict[int, torch.Tensor] = {} memory: dict[int, torch.Tensor] = {}
pending: list[torch.Tensor] = [] pending: list[torch.Tensor] = []
@@ -180,8 +182,15 @@ def _stream(org: Organism, game: list[Ply], affect: Affect, acfg: AffectConfig,
for ply in game: for ply in game:
coords = torch.from_numpy(ply.coords) coords = torch.from_numpy(ply.coords)
attrs = np.concatenate([ply.attrs, ply.past], 1) if oracle else ply.attrs attrs = np.concatenate([ply.attrs, ply.past], 1) if oracle else ply.attrs
# the receiving piece's type, into the CPPN's own channel. Without this the kernel
# is one geometry function shared by all six types -- see results/chess_reach.md,
# where that costs the knight's L entirely.
et = None
if edges:
t = torch.from_numpy(ply.attrs[:, :6]).argmax(1)
et = t[:, None].expand(len(ply.ids), len(ply.ids)).contiguous()
pred, memory = org(torch.from_numpy(attrs), flat_diff(coords), ply.ids, pred, memory = org(torch.from_numpy(attrs), flat_diff(coords), ply.ids,
affect.axes, memory, affect.axes, memory, edge_types=et,
mask=torch.from_numpy(ply.mask) if adjacency else None) mask=torch.from_numpy(ply.mask) if adjacency else None)
p = pred[ply.mover] p = pred[ply.mover]
loss = ((p - torch.from_numpy(ply.target)) ** 2).mean() # masked to the mover loss = ((p - torch.from_numpy(ply.target)) ** 2).mean() # masked to the mover
@@ -227,11 +236,11 @@ def run_constant(games: list[list[Ply]], seed: int) -> dict:
def run(games: list[list[Ply]], memory: bool, hidden: int, cppn_hidden: int, seed: int, def run(games: list[list[Ply]], memory: bool, hidden: int, cppn_hidden: int, seed: int,
lr: float, bptt: int, kappa: float, use_affect: bool, oracle: bool = False, lr: float, bptt: int, kappa: float, use_affect: bool, oracle: bool = False,
adjacency: bool = False) -> dict: adjacency: bool = False, edges: bool = False) -> dict:
torch.manual_seed(seed) torch.manual_seed(seed)
attr_dim = ATTR_DIM + (games[0][0].past.shape[1] if oracle else 0) attr_dim = ATTR_DIM + (games[0][0].past.shape[1] if oracle else 0)
org = Organism(attr_dim, 2, 2, hidden_dim=hidden, cppn_hidden=cppn_hidden, org = Organism(attr_dim, 2, 2, n_edge_types=6 if edges else 1, hidden_dim=hidden,
kappa=kappa, memory=memory) cppn_hidden=cppn_hidden, kappa=kappa, memory=memory)
opt = torch.optim.Adam(org.parameters(), lr=lr) opt = torch.optim.Adam(org.parameters(), lr=lr)
acfg, affect = AffectConfig(), Affect() acfg, affect = AffectConfig(), Affect()
@@ -239,12 +248,12 @@ def run(games: list[list[Ply]], memory: bool, hidden: int, cppn_hidden: int, see
order = np.random.default_rng(seed).permutation(len(games)) order = np.random.default_rng(seed).permutation(len(games))
cut = max(1, len(games) // 4) cut = max(1, len(games) // 4)
for gi in order[cut:]: for gi in order[cut:]:
affect, *_ = _stream(org, games[gi], affect, acfg, opt, bptt, use_affect, oracle, adjacency) affect, *_ = _stream(org, games[gi], affect, acfg, opt, bptt, use_affect, oracle, adjacency, edges)
mse, hits, chance = [], [], [] mse, hits, chance = [], [], []
for gi in order[:cut]: for gi in order[:cut]:
affect, l, h, c = _stream(org, games[gi], affect, acfg, None, bptt, use_affect, oracle, adjacency) affect, l, h, c = _stream(org, games[gi], affect, acfg, None, bptt, use_affect, oracle, adjacency, edges)
mse += l; hits += h; chance += c mse += l; hits += h; chance += c
return dict(seed=seed, hidden=hidden, memory=memory, oracle=oracle, mask=adjacency, return dict(seed=seed, hidden=hidden, memory=memory, oracle=oracle, mask=adjacency, edges=edges,
params=sum(p.numel() for p in org.parameters()), plies=len(mse), params=sum(p.numel() for p in org.parameters()), plies=len(mse),
mse=statistics.fmean(mse), acc=statistics.fmean(hits), mse=statistics.fmean(mse), acc=statistics.fmean(hits),
chance=statistics.fmean(chance)) chance=statistics.fmean(chance))
@@ -263,13 +272,15 @@ def main() -> None:
ap.add_argument("--affect", default="neutral", choices=("neutral", "stream"), ap.add_argument("--affect", default="neutral", choices=("neutral", "stream"),
help="neutral holds psi at (0,0); stream runs the real EMA chain") help="neutral holds psi at (0,0); stream runs the real EMA chain")
ap.add_argument("--arms", default="rp,grp,rp_wide,rp_oracle") ap.add_argument("--arms", default="rp,grp,rp_wide,rp_oracle")
ap.add_argument("--edges", default="none", choices=("none", "type"),
help="type puts the receiving piece's type into e_ij, the CPPN's channel")
ap.add_argument("--mask", default="all", choices=("all", "attack"), ap.add_argument("--mask", default="all", choices=("all", "attack"),
help="all is the spec default; attack is the domain's own adjacency") help="all is the spec default; attack is the domain's own adjacency")
ap.add_argument("--out", default="results/chess.json") ap.add_argument("--out", default="results/chess.json")
a = ap.parse_args() a = ap.parse_args()
games = load(a.games, a.hist) games = load(a.games, a.hist)
wide = match_width(a.hidden, a.cppn_hidden) wide = match_width(a.hidden, a.cppn_hidden, 6 if a.edges == 'type' else 1)
# rp_oracle is the ceiling, not a competitor: the memoryless model is HANDED each piece's # rp_oracle is the ceiling, not a competitor: the memoryless model is HANDED each piece's
# own past, which is exactly what memory would have to learn to carry. If it does not pay, # own past, which is exactly what memory would have to learn to carry. If it does not pay,
# a memory null here is about the setup and not about memory. # a memory null here is about the setup and not about memory.
@@ -287,7 +298,8 @@ def main() -> None:
for seed in (int(s) for s in a.seeds.split(",")): for seed in (int(s) for s in a.seeds.split(",")):
r = (run_constant(games, seed) if name == "constant" else r = (run_constant(games, seed) if name == "constant" else
run(games, mem, h, a.cppn_hidden, seed, a.lr, a.bptt, a.kappa, run(games, mem, h, a.cppn_hidden, seed, a.lr, a.bptt, a.kappa,
a.affect == "stream", orc, a.mask == "attack")) | {"arm": name} a.affect == "stream", orc, a.mask == "attack",
a.edges == "type")) | {"arm": name}
print(f"{name:<8} seed={seed} params={r['params']:<6} " print(f"{name:<8} seed={seed} params={r['params']:<6} "
f"mse={r['mse']:.5f} acc={r['acc']:.4f}", flush=True) f"mse={r['mse']:.5f} acc={r['acc']:.4f}", flush=True)
results.append(r) results.append(r)