diff --git a/chess_dist.py b/chess_dist.py index 5f11176..230a498 100644 --- a/chess_dist.py +++ b/chess_dist.py @@ -59,7 +59,7 @@ def offsets(dests: np.ndarray) -> np.ndarray: return (d[:, 1] + 7) * 15 + (d[:, 0] + 7) -def _stream(org, game, opt, bptt: int, oracle: bool, edges: bool): +def _stream(org, game, opt, bptt: int, oracle: bool, edges: bool, nostate: bool = False): memory: dict[int, torch.Tensor] = {} pending, losses, hits, chance = [], [], [], [] zero = Affect().axes @@ -72,7 +72,7 @@ def _stream(org, game, opt, bptt: int, oracle: bool, edges: bool): t = torch.from_numpy(ply.attrs[:, :6]).argmax(1) et = t[:, None].expand(n, n).contiguous() pred, memory = org(torch.from_numpy(attrs), flat_diff(torch.from_numpy(ply.coords)), - ply.ids, zero, memory, edge_types=et) + ply.ids, zero, {} if nostate else memory, edge_types=et) legal = torch.from_numpy(offsets(ply.dests)) logits = pred[ply.mover][legal] # only the legal destinations loss = torch.nn.functional.cross_entropy(logits[None, :], @@ -92,7 +92,7 @@ def _stream(org, game, opt, bptt: int, oracle: bool, edges: bool): def run(games, memory: bool, hidden: int, cppn_hidden: int, seed: int, lr: float, bptt: int, - oracle: bool, edges: bool) -> dict: + oracle: bool, edges: bool, nostate: bool = False) -> dict: torch.manual_seed(seed) attr_dim = ATTR_DIM + (games[0][0].past.shape[1] if oracle else 0) org = Organism(attr_dim, 2, GRID, n_edge_types=6 if edges else 1, hidden_dim=hidden, @@ -101,12 +101,12 @@ def run(games, memory: bool, hidden: int, cppn_hidden: int, seed: int, lr: float order = np.random.default_rng(seed).permutation(len(games)) cut = max(1, len(games) // 4) for gi in order[cut:]: - _stream(org, games[gi], opt, bptt, oracle, edges) + _stream(org, games[gi], opt, bptt, oracle, edges, nostate) loss, hits, chance = [], [], [] for gi in order[:cut]: - l, h, c = _stream(org, games[gi], None, bptt, oracle, edges) + l, h, c = _stream(org, games[gi], None, bptt, oracle, edges, nostate) loss += l; hits += h; chance += c - return dict(seed=seed, memory=memory, oracle=oracle, hidden=hidden, + return dict(seed=seed, memory=memory, oracle=oracle, nostate=nostate, hidden=hidden, params=sum(p.numel() for p in org.parameters()), loss=statistics.fmean(loss), acc=statistics.fmean(hits), chance=statistics.fmean(chance)) @@ -151,7 +151,12 @@ def main() -> None: "rp_wide": (False, wide, False), # the oracle sits at the SAME width as `rp`: same architecture, one of them # handed the past. Anything else confounds the ceiling with capacity. - "rp_oracle": (False, a.hidden, True)} + "rp_oracle": (False, a.hidden, True), + # the control that separates the state from the gate: the SAME architecture and + # the SAME parameters, handed an empty memory every ply. In the particles work + # this is what turned "memory helps" into "the gated nonlinearity helps" + # (results/frozen.md). A GRP gain is not a memory result until this arm is run. + "grp_nostate": (True, a.hidden, False)} arms = {k: all_arms[k] for k in a.arms.split(",")} print(f"{len(games)} games, {sum(len(g) for g in games)} plies, edges={a.edges}, " f"width-matched memoryless arm at hidden={wide}\n") @@ -161,7 +166,7 @@ def main() -> None: for seed in (int(s) for s in a.seeds.split(",")): r = (run_constant(games, seed) if spec is None else run(games, spec[0], spec[1], a.cppn_hidden, seed, a.lr, a.bptt, spec[2], - edges)) | {"arm": name} + edges, name == "grp_nostate")) | {"arm": name} print(f"{name:<10} seed={seed} params={r['params']:<6} " f"loss={r['loss']:.4f} acc={r['acc']:.4f}", flush=True) results.append(r) @@ -175,7 +180,8 @@ def main() -> None: print(f"{n:<10}{rs[0]['params']:>8}{statistics.fmean(r['loss'] for r in rs):>9.4f}" f"{statistics.fmean(r['acc'] for r in rs):>9.4f}" f"{statistics.fmean(r['chance'] for r in rs):>9.4f}") - for top, base in (("rp_oracle", "rp"), ("grp", "rp_wide"), ("grp", "rp")): + for top, base in (("rp_oracle", "rp"), ("grp", "rp_wide"), ("grp", "rp"), + ("grp", "grp_nostate"), ("grp_nostate", "rp_wide")): if top not in by or base not in by: continue d = [x["acc"] - y["acc"] for x, y in zip(by[top], by[base])]