exp: chess attack adjacency is a measured null, and the oracle still does not pay
Mean degree falls 30 -> 3.3. Accuracy falls with it (0.414 -> 0.381): the mask removes board information the model was using. The dilution hypothesis is not supported -- the history oracle reads -0.0003 +/- 0.0019 on 4/8 seeds, ambiguous, same as under all-ones. grp - rp_wide is +0.0023 +/- 0.0026 on 4/8, so the earlier -0.0057 dissolves too. Twice-controlled: per-entity history does not pay under contract A with an MSE readout, in either adjacency.
This commit is contained in:
1 file changed
+26
-10
+26
-10
@@ -57,6 +57,7 @@ class Ply:
|
|||||||
dests: np.ndarray # (L, 2) displacement of every legal move from its square
|
dests: np.ndarray # (L, 2) displacement of every legal move from its square
|
||||||
true_dest: int # index into dests
|
true_dest: int # index into dests
|
||||||
past: np.ndarray # (N, 2*hist) each entity's previous squares, as displacements
|
past: np.ndarray # (N, 2*hist) each entity's previous squares, as displacements
|
||||||
|
mask: np.ndarray # (N, N) the attack/defend graph, symmetric, self-loops on
|
||||||
|
|
||||||
|
|
||||||
def advance_ids(board: chess.Board, move: chess.Move, piece: chess.Piece,
|
def advance_ids(board: chess.Board, move: chess.Move, piece: chess.Piece,
|
||||||
@@ -105,6 +106,15 @@ def plies(game: chess.pgn.Game, hist: int = 3) -> list[Ply]:
|
|||||||
mover = squares.index(move.from_square)
|
mover = squares.index(move.from_square)
|
||||||
attrs[mover, 7] = 1.0
|
attrs[mover, 7] = 1.0
|
||||||
|
|
||||||
|
# the spec's optional adjacency input (§2), which chess actually has: i sees j when
|
||||||
|
# either attacks the other's square. With the all-ones default an entity's own
|
||||||
|
# attributes reach it only through the diagonal term and are divided by deg ~= 30.
|
||||||
|
sq = np.array(squares, dtype=np.uint64)
|
||||||
|
bb = np.array([int(board.attacks(int(t))) for t in squares], dtype=np.uint64)
|
||||||
|
att = ((bb[:, None] >> sq[None, :]) & np.uint64(1)).astype(np.float32)
|
||||||
|
mask = np.maximum(att, att.T)
|
||||||
|
np.fill_diagonal(mask, 1.0)
|
||||||
|
|
||||||
# each entity's own past, relative to where it stands now. Absent history is zero,
|
# each entity's own past, relative to where it stands now. Absent history is zero,
|
||||||
# which is also "I am where I was" -- the ambiguity costs the oracle, never the model.
|
# which is also "I am where I was" -- the ambiguity costs the oracle, never the model.
|
||||||
past = np.zeros((len(squares), 2 * hist), dtype=np.float32)
|
past = np.zeros((len(squares), 2 * hist), dtype=np.float32)
|
||||||
@@ -120,8 +130,8 @@ def plies(game: chess.pgn.Game, hist: int = 3) -> list[Ply]:
|
|||||||
(chess.square_rank(t) - chess.square_rank(move.from_square)) / SCALE]
|
(chess.square_rank(t) - chess.square_rank(move.from_square)) / SCALE]
|
||||||
for t in legal], dtype=np.float32)
|
for t in legal], dtype=np.float32)
|
||||||
j = legal.index(move.to_square)
|
j = legal.index(move.to_square)
|
||||||
out.append(Ply([id_at[sq] for sq in squares], attrs, coords, mover, dests[j], dests, j,
|
out.append(Ply([id_at[s] for s in squares], attrs, coords, mover, dests[j], dests, j,
|
||||||
past))
|
past, mask))
|
||||||
|
|
||||||
seen.setdefault(id_at[move.from_square], []).append(move.from_square)
|
seen.setdefault(id_at[move.from_square], []).append(move.from_square)
|
||||||
advance_ids(board, move, piece, id_at)
|
advance_ids(board, move, piece, id_at)
|
||||||
@@ -160,7 +170,7 @@ def match_width(hidden: int, cppn_hidden: int) -> int:
|
|||||||
|
|
||||||
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):
|
oracle: bool, adjacency: 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] = []
|
||||||
@@ -170,7 +180,8 @@ def _stream(org: Organism, game: list[Ply], affect: Affect, acfg: AffectConfig,
|
|||||||
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
|
||||||
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,
|
||||||
|
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
|
||||||
if use_affect:
|
if use_affect:
|
||||||
@@ -214,7 +225,8 @@ 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) -> dict:
|
lr: float, bptt: int, kappa: float, use_affect: bool, oracle: bool = False,
|
||||||
|
adjacency: 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, hidden_dim=hidden, cppn_hidden=cppn_hidden,
|
||||||
@@ -226,12 +238,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)
|
affect, *_ = _stream(org, games[gi], affect, acfg, opt, bptt, use_affect, oracle, adjacency)
|
||||||
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)
|
affect, l, h, c = _stream(org, games[gi], affect, acfg, None, bptt, use_affect, oracle, adjacency)
|
||||||
mse += l; hits += h; chance += c
|
mse += l; hits += h; chance += c
|
||||||
return dict(seed=seed, hidden=hidden, memory=memory, oracle=oracle,
|
return dict(seed=seed, hidden=hidden, memory=memory, oracle=oracle, mask=adjacency,
|
||||||
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))
|
||||||
@@ -250,6 +262,8 @@ 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("--mask", default="all", choices=("all", "attack"),
|
||||||
|
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()
|
||||||
|
|
||||||
@@ -262,15 +276,17 @@ def main() -> None:
|
|||||||
"rp_wide": (False, wide, False), "rp_oracle": (False, wide, True),
|
"rp_wide": (False, wide, False), "rp_oracle": (False, wide, True),
|
||||||
"constant": (False, 0, False)}
|
"constant": (False, 0, False)}
|
||||||
arms = {k: all_arms[k] for k in a.arms.split(",")}
|
arms = {k: all_arms[k] for k in a.arms.split(",")}
|
||||||
|
deg = statistics.fmean(float(p.mask.sum(1).mean()) for g in games for p in g)
|
||||||
print(f"{len(games)} games, {sum(len(g) for g in games)} plies; "
|
print(f"{len(games)} games, {sum(len(g) for g in games)} plies; "
|
||||||
f"width-matched memoryless arm at hidden={wide}\n")
|
f"width-matched memoryless arm at hidden={wide}; mask={a.mask}, "
|
||||||
|
f"mean degree {deg if a.mask == 'attack' else statistics.fmean(len(p.ids) for g in games for p in g):.1f}\n")
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
for name, (mem, h, orc) in arms.items():
|
for name, (mem, h, orc) in arms.items():
|
||||||
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)) | {"arm": name}
|
a.affect == "stream", orc, a.mask == "attack")) | {"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)
|
||||||
|
|||||||
Reference in new issue
Block a user