exp: chess has the per-entity history particles lacked
A piece's own past predicts its next square beyond the board: +0.056 with ridge on 6/6 seeds, +0.034 with a 2x128 MLP on 3/3, eleven paired standard errors from zero. The shuffled-history control is negative in both arms, so the gain is about the alignment between a piece's past and its future rather than about feature count. Split by game, never by position. The MLP reads more from the board alone than ridge does, so part of what history gave the linear model was recoverable from the present; the gain shrinks from +0.056 to +0.034 and this measurement does not establish where that stops. Stated as the caveat it is. This is the property particles lacked. There the hidden variable was global -- one coupling matrix belonging to no entity -- and the per-entity state held less about it than the memoryless representation it was built from. Here what there is to remember belongs to the piece.
This commit is contained in:
2 files changed
+133
-8
No files matched your search
+40
-8
@@ -105,22 +105,52 @@ def collect(n_games: int, hist: int, min_moves: int, seed: int):
|
||||
return np.array(A), np.array(B), np.array(y), np.array(gid)
|
||||
|
||||
|
||||
def fit_eval(Xtr, ytr, Xte, yte, l2: float = 1.0):
|
||||
"""Ridge onto one-hot targets, argmax at test. One estimator for both arms, so the
|
||||
difference between them is about the features and not about the fit."""
|
||||
K = 64
|
||||
Y = np.zeros((len(ytr), K), dtype=np.float32)
|
||||
def fit_ridge(Xtr, ytr, Xte, yte, l2: float = 1.0):
|
||||
"""Ridge onto one-hot targets, argmax at test."""
|
||||
Y = np.zeros((len(ytr), 64), dtype=np.float32)
|
||||
Y[np.arange(len(ytr)), ytr] = 1.0
|
||||
d = Xtr.shape[1]
|
||||
W = np.linalg.solve(Xtr.T @ Xtr + l2 * np.eye(d, dtype=np.float32), Xtr.T @ Y)
|
||||
return float((np.argmax(Xte @ W, axis=1) == yte).mean())
|
||||
|
||||
|
||||
def fit_mlp(Xtr, ytr, Xte, yte, hidden: int = 128, epochs: int = 12, bs: int = 512):
|
||||
"""A 2xN ReLU net on the same features.
|
||||
|
||||
Task 11 is why this arm exists: on the particles domain a linear decoder read 0.23
|
||||
where an MLP read 0.83 on identical features, so a linear null there would have been
|
||||
wrong by 0.6 in accuracy. A linear-only null here would inherit that objection.
|
||||
"""
|
||||
import torch
|
||||
g = torch.Generator().manual_seed(0)
|
||||
Xtr_t = torch.from_numpy(Xtr); ytr_t = torch.from_numpy(ytr).long()
|
||||
net = torch.nn.Sequential(
|
||||
torch.nn.Linear(Xtr.shape[1], hidden), torch.nn.ReLU(),
|
||||
torch.nn.Linear(hidden, hidden), torch.nn.ReLU(),
|
||||
torch.nn.Linear(hidden, 64))
|
||||
opt = torch.optim.Adam(net.parameters(), lr=1e-3)
|
||||
n = len(ytr_t)
|
||||
for _ in range(epochs):
|
||||
perm = torch.randperm(n, generator=g)
|
||||
for i in range(0, n, bs):
|
||||
idx = perm[i:i + bs]
|
||||
opt.zero_grad()
|
||||
torch.nn.functional.cross_entropy(net(Xtr_t[idx]), ytr_t[idx]).backward()
|
||||
opt.step()
|
||||
with torch.no_grad():
|
||||
pred = net(torch.from_numpy(Xte)).argmax(1).numpy()
|
||||
return float((pred == yte).mean())
|
||||
|
||||
|
||||
FITS = {"ridge": fit_ridge, "mlp": fit_mlp}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--games", type=int, default=3000)
|
||||
ap.add_argument("--hist", type=int, default=3, help="how many past squares B may see")
|
||||
ap.add_argument("--min-moves", type=int, default=2, help="skip a piece's first moves")
|
||||
ap.add_argument("--estimator", default="ridge", choices=sorted(FITS))
|
||||
ap.add_argument("--seeds", default="0,1,2,3,4,5,6,7,8,9,10,11")
|
||||
a = ap.parse_args()
|
||||
|
||||
@@ -128,7 +158,9 @@ def main() -> None:
|
||||
print(f"{len(y)} moves from {gid.max() + 1} games, {A.shape[1]} present features, "
|
||||
f"{B.shape[1]} with history\n")
|
||||
|
||||
fit = FITS[a.estimator]
|
||||
gains = []
|
||||
print(f"estimator: {a.estimator}\n")
|
||||
print(f"{'seed':>5}{'present':>10}{'+history':>10}{'gain':>9}{'shuffled':>10}")
|
||||
print("-" * 44)
|
||||
for s in (int(x) for x in a.seeds.split(",")):
|
||||
@@ -139,14 +171,14 @@ def main() -> None:
|
||||
rng.shuffle(games)
|
||||
te = np.isin(gid, games[: max(1, len(games) // 4)])
|
||||
tr = ~te
|
||||
acc_a = fit_eval(A[tr], y[tr], A[te], y[te])
|
||||
acc_b = fit_eval(B[tr], y[tr], B[te], y[te])
|
||||
acc_a = fit(A[tr], y[tr], A[te], y[te])
|
||||
acc_b = fit(B[tr], y[tr], B[te], y[te])
|
||||
# chance for the history arm: same features, history block shuffled across rows,
|
||||
# so its marginal is intact and only its alignment with the target is broken
|
||||
Bs = B.copy()
|
||||
idx = rng.permutation(len(Bs))
|
||||
Bs[:, 135:] = Bs[idx, 135:]
|
||||
acc_s = fit_eval(Bs[tr], y[tr], Bs[te], y[te])
|
||||
acc_s = fit(Bs[tr], y[tr], Bs[te], y[te])
|
||||
gains.append((acc_a, acc_b, acc_b - acc_a, acc_s))
|
||||
print(f"{s:>5}{acc_a:>10.4f}{acc_b:>10.4f}{acc_b - acc_a:>+9.4f}{acc_s:>10.4f}")
|
||||
|
||||
|
||||
Reference in new issue
Block a user