feat: vary kernel sharpness and hold-out structure -- both readings change
Four teacher families on one axis (how predictable the kernel is from its neighbours): smooth, high-frequency, a rule-generated sparse knight, and iid noise as the negative control, which is asserted to have neighbour correlation below 0.2 where the smooth teacher's is above 0.8. The knight teacher forced a new hold-out: most of its cells are exactly zero, so withholding uniformly asks nothing and the floor collapses. --holdout support withholds cells the kernel uses, closed under negation since a pair contributes r and -r. Under it the CPPN beats a lookup plus blur 4.4x on smooth and 3.0x on high-frequency, 6/6 seeds, reversing the earlier headline. And on the knight nothing generalises at all -- both arms at or above the floor. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HCydQraDSoZ9WoTm2v23Eb
This commit is contained in:
7 files changed
+279
-34
No files matched your search
@@ -48,15 +48,40 @@ def cell_of(r: torch.Tensor) -> torch.Tensor:
|
||||
return dy * G + dx
|
||||
|
||||
|
||||
def gabor_kernel(sigma: float = 3.0, seed: int = 0, kfreq: float = 0.35) -> torch.Tensor:
|
||||
"""(CELLS, D) true kernel: a smooth, band-limited function of the displacement.
|
||||
KNIGHT = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)]
|
||||
|
||||
Smooth on purpose. A CPPN that cannot interpolate THIS has not failed a hard case, it has
|
||||
failed the easy one -- which is what makes a null here worth recording.
|
||||
|
||||
def gabor_kernel(sigma: float = 3.0, seed: int = 0, kfreq: float = 0.35,
|
||||
kind: str = "smooth") -> torch.Tensor:
|
||||
"""(CELLS, D) true kernel. `kind` varies HOW PREDICTABLE IT IS FROM ITS NEIGHBOURS.
|
||||
|
||||
Every null in unseen.md, scale.md and psi.md was measured against a smooth teacher, and
|
||||
smoothness is exactly what lets a lookup plus a blur compete. These four families vary that
|
||||
and nothing else:
|
||||
|
||||
smooth a band-limited Gabor. The replication: a blur should match the CPPN.
|
||||
highfreq the same, at a frequency near the grid's resolution. Structured, not smooth.
|
||||
knight 1 on the eight L-offsets, 0 elsewhere. NOT smooth and RULE-GENERATED -- a
|
||||
neighbour average cannot recover an isolated spike, since its neighbours are 0.
|
||||
This is chess_reach.md's knight, asked as a mechanism question.
|
||||
noise iid per cell. THE CONTROL: nothing should generalise here, the CPPN included.
|
||||
Without it, "the CPPN wins on non-smooth kernels" cannot be told apart from
|
||||
"the setup favours the CPPN".
|
||||
"""
|
||||
g = torch.Generator().manual_seed(seed)
|
||||
ys, xs = torch.meshgrid(torch.arange(-R, R + 1), torch.arange(-R, R + 1), indexing="ij")
|
||||
r = torch.stack([xs, ys], -1).reshape(CELLS, 2).float()
|
||||
if kind == "noise":
|
||||
return torch.randn(CELLS, D, generator=g) * 0.2
|
||||
if kind == "knight":
|
||||
w = torch.zeros(CELLS, D)
|
||||
sign = torch.randint(0, 2, (D, len(KNIGHT)), generator=g).float() * 2 - 1
|
||||
for c in range(D):
|
||||
for m, (dx, dy) in enumerate(KNIGHT):
|
||||
w[(dy + R) * G + (dx + R), c] = sign[c, m]
|
||||
return w
|
||||
if kind == "highfreq":
|
||||
kfreq = 1.5
|
||||
k = torch.randn(D, 2, generator=g) * kfreq # spatial frequencies of the domain
|
||||
phi = torch.rand(D, generator=g) * 6.2831853
|
||||
env = torch.exp(-(r * r).sum(-1) / (2 * sigma * sigma))[:, None]
|
||||
@@ -120,21 +145,34 @@ def fill_by_diffusion(w: torch.Tensor, held: torch.Tensor) -> None:
|
||||
w.copy_(grid.view(CELLS, D))
|
||||
|
||||
|
||||
def teacher(seed: int, kfreq: float = 0.35) -> nn.Module:
|
||||
def teacher(seed: int, kfreq: float = 0.35, kind: str = "smooth") -> nn.Module:
|
||||
"""kfreq is the DOMAIN's length scale, not the model's. Sweeping it against the CPPN's omega
|
||||
is what separates a knob that wants calibrating per domain from a defect in the
|
||||
architecture: if the best omega tracks kfreq, core.py's own comment was right all along and
|
||||
nobody had calibrated it; if it sits still, the default can simply be corrected."""
|
||||
m = make_model("table", seed + 1000)
|
||||
with torch.no_grad():
|
||||
m["rp"].cppn.w.copy_(gabor_kernel(seed=seed, kfreq=kfreq))
|
||||
m["rp"].cppn.w.copy_(gabor_kernel(seed=seed, kfreq=kfreq, kind=kind))
|
||||
for p in m.parameters():
|
||||
p.requires_grad_(False)
|
||||
return m
|
||||
|
||||
|
||||
def holdout_cells(kind: str, frac: float, seed: int) -> torch.Tensor:
|
||||
"""(CELLS,) bool, True = withheld from training."""
|
||||
def holdout_cells(kind: str, frac: float, seed: int, W: torch.Tensor | None = None
|
||||
) -> torch.Tensor:
|
||||
"""(CELLS,) bool, True = withheld from training.
|
||||
|
||||
`support` exists because the knight teacher broke the other two. Most of its 225 cells are
|
||||
exactly zero, so a hold-out drawn uniformly withholds cells that carry no signal, the
|
||||
target's variance there is ~0, and the floor collapses -- the question becomes vacuous
|
||||
rather than hard. `support` withholds cells the kernel actually uses, which is the same
|
||||
question for every family: having seen most of where the law lives, can the model infer the
|
||||
rest? For the knight that is "seen six of the eight L-squares, infer the other two", and a
|
||||
neighbour average cannot, because an isolated spike's neighbours are zero.
|
||||
|
||||
The set is closed under negation: a pair contributes displacements r and -r, so an
|
||||
asymmetric hold-out would have no evaluable pairs.
|
||||
"""
|
||||
ys, xs = torch.meshgrid(torch.arange(-R, R + 1), torch.arange(-R, R + 1), indexing="ij")
|
||||
centre = R * G + R # r = 0: every entity messages itself, so it is in every configuration
|
||||
if kind == "random":
|
||||
@@ -145,6 +183,24 @@ def holdout_cells(kind: str, frac: float, seed: int) -> torch.Tensor:
|
||||
m[idx] = True
|
||||
m[centre] = False
|
||||
return m
|
||||
if kind == "support":
|
||||
assert W is not None, "support hold-out needs the teacher's kernel"
|
||||
mag = W.abs().mean(1)
|
||||
live = (mag > 0.2 * mag.max()).nonzero().flatten().tolist()
|
||||
g = torch.Generator().manual_seed(seed)
|
||||
order = torch.randperm(len(live), generator=g).tolist()
|
||||
m = torch.zeros(CELLS, dtype=torch.bool)
|
||||
want = max(2, int(round(frac * len(live))))
|
||||
for idx in order:
|
||||
c = live[idx]
|
||||
if c == centre:
|
||||
continue
|
||||
dy, dx = divmod(c, G)
|
||||
neg = (2 * R - dy) * G + (2 * R - dx) # the cell for -r
|
||||
m[c] = m[neg] = True
|
||||
if int(m.sum()) >= want:
|
||||
break
|
||||
return m
|
||||
if kind == "band":
|
||||
d = (xs.abs().float() + ys.abs().float()).reshape(-1) # L1 radius
|
||||
lo = torch.quantile(d, 1.0 - frac - 0.12)
|
||||
@@ -214,9 +270,11 @@ def floor_mse(tea, data) -> float:
|
||||
|
||||
|
||||
def one_seed(arm: str, kind: str, frac: float, steps: int, seed: int, omega: float = 1.0,
|
||||
kfreq: float = 0.35) -> dict:
|
||||
tea = teacher(seed, kfreq)
|
||||
held = holdout_cells(kind, frac, seed)
|
||||
kfreq: float = 0.35, teacher_kind: str = "smooth") -> dict:
|
||||
# `kind` is the HOLD-OUT structure and `teacher_kind` is the kernel family. They are
|
||||
# different axes and were briefly the same identifier, which Python caught.
|
||||
tea = teacher(seed, kfreq, teacher_kind)
|
||||
held = holdout_cells(kind, frac, seed, gabor_kernel(seed=seed, kfreq=kfreq, kind=teacher_kind))
|
||||
gen = torch.Generator().manual_seed(seed)
|
||||
model = make_model(arm, seed, omega)
|
||||
opt = torch.optim.Adam(model.parameters(), lr=3e-3)
|
||||
@@ -233,7 +291,7 @@ def one_seed(arm: str, kind: str, frac: float, steps: int, seed: int, omega: flo
|
||||
ev = torch.Generator().manual_seed(seed + 5000)
|
||||
seen = pair_batch(held, False, ev)
|
||||
unseen = pair_batch(held, True, ev)
|
||||
return {"seed": seed, "arm": arm, "omega": omega, "kfreq": kfreq, "seen": evaluate(model, tea, seen),
|
||||
return {"seed": seed, "arm": arm, "omega": omega, "kfreq": kfreq, "teacher": teacher_kind, "seen": evaluate(model, tea, seen),
|
||||
"unseen": evaluate(model, tea, unseen),
|
||||
"floor_seen": floor_mse(tea, seen), "floor_unseen": floor_mse(tea, unseen),
|
||||
"n_held": int(held.sum())}
|
||||
@@ -241,25 +299,29 @@ def one_seed(arm: str, kind: str, frac: float, steps: int, seed: int, omega: flo
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--holdout", default="random", choices=["random", "band"])
|
||||
ap.add_argument("--holdout", default="random", choices=["random", "band", "support"])
|
||||
ap.add_argument("--frac", type=float, default=0.25)
|
||||
ap.add_argument("--steps", type=int, default=1500)
|
||||
ap.add_argument("--seeds", default="0,1,2,3,4,5,6,7")
|
||||
ap.add_argument("--arms", default="rp,table")
|
||||
ap.add_argument("--omega", type=float, default=1.0)
|
||||
ap.add_argument("--kfreq", type=float, default=0.35, help="the DOMAIN's spatial frequency")
|
||||
ap.add_argument("--teacher", default="smooth",
|
||||
choices=["smooth", "highfreq", "knight", "noise"])
|
||||
ap.add_argument("--out", default="results/unseen.json")
|
||||
args = ap.parse_args()
|
||||
|
||||
rows = []
|
||||
for arm in args.arms.split(","):
|
||||
for s in (int(x) for x in args.seeds.split(",")):
|
||||
rows.append(one_seed(arm, args.holdout, args.frac, args.steps, s, args.omega, args.kfreq))
|
||||
rows.append(one_seed(arm, args.holdout, args.frac, args.steps, s, args.omega, args.kfreq,
|
||||
args.teacher))
|
||||
r = rows[-1]
|
||||
print(f"{arm:6s} seed {s} seen {r['seen']:.5f} unseen {r['unseen']:.5f}"
|
||||
f" floor {r['floor_unseen']:.5f}", flush=True)
|
||||
|
||||
print(f"\n{args.holdout} hold-out, frac={args.frac}, omega={args.omega}, {rows[0]['n_held']}/{CELLS} cells")
|
||||
print(f"\n{args.teacher} teacher, {args.holdout} hold-out, frac={args.frac}, "
|
||||
f"omega={args.omega}, {rows[0]['n_held']}/{CELLS} cells")
|
||||
print(f"{'arm':6s} {'seen':>9s} {'unseen':>9s} {'floor':>9s} unseen/floor")
|
||||
for arm in args.arms.split(","):
|
||||
a = [r for r in rows if r["arm"] == arm]
|
||||
|
||||
Reference in new issue
Block a user