exp: A1 -- affect moves the gate, and moving it does not help

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KAWnnPg2qvmLNySamE2k85
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-09-02 09:48:40 -05:00
1 parent 44a892397f
commit 860646ad9a
4 files changed
+866 -5

No files matched your search

+29 -5
View File
@@ -54,35 +54,59 @@ def _build(name: str, seed: int, hidden: int, omega: float, kappa: float):
def _train(spec: dict, wcfg: ParticleConfig, world: ParticleWorld, org: Organism,
steps: int, bptt: int, lr: float, stateless: bool = False):
"""A verbatim copy of `regime.run`'s training loop (`stateless` aside).
steps: int, bptt: int, lr: float, stateless: bool = False,
gate_arousal=None, on_step=None):
"""A verbatim copy of `regime.run`'s training loop (the keyword arguments aside).
Copied rather than reused because `regime.run` returns metrics and drops the
organism and the world, and this task has to keep stepping the same world with
the same organism after training ends. `tests/test_frozen.py` pins the copy: it
asserts this loop reproduces `regime.run`'s metrics bit-identically.
asserts this loop reproduces `regime.run`'s metrics bit-identically. Every
keyword below defaults to off, and with all three off the loop body is the same
sequence of calls it always was.
`stateless=True` hands the organism an empty memory dict every step, so M_prev is
always zero. The GRP keeps every parameter it had and loses only the carrying of
state -- the control that separates "memory helps" from "the GRP's extra 4160
parameters help". It is a call into the public API, not a change to the library.
`gate_arousal(t, affect) -> float` replaces the arousal the *gate* sees while the
CPPN keeps the organism's own live affect: the context is then passed explicitly,
built exactly as `Organism.forward` would have built it from the real affect. This
is Task 8's shuffled-affect control -- it has to move `-kappa*a` alone, because
shuffling the affect the CPPN reads would change a second input as well.
`on_step(t, obs, r, ctx, axes, prev, memory)` is a read-only callback fired after
the forward pass, for measuring the gate. `prev` is the M_prev the organism was
handed, captured before `memory` is rebound.
"""
opt = torch.optim.Adam(org.parameters(), lr=lr)
acfg = AffectConfig()
affect = Affect()
memory: dict[int, Tensor] = {}
zero = torch.zeros(org.hidden_dim)
losses, switched, arousals = [], [], []
pending = []
for t in range(steps):
obs = world.step()
ctx = _context(spec["ctx"], affect, obs.regime, wcfg.n_regimes)
pred, memory = org(obs.attrs, wrapped_diff(obs.coords), obs.ids, affect.axes,
{} if stateless else memory, context=ctx)
r = wrapped_diff(obs.coords)
axes = affect.axes
if gate_arousal is not None:
if ctx is None: # what Organism.forward builds from the real affect
ctx = torch.tensor(affect.axes, dtype=obs.attrs.dtype)
axes = (affect.valence, float(gate_arousal(t, affect)))
held = {} if stateless else memory
prev = (torch.stack([held.get(i, zero) for i in obs.ids])
if on_step is not None and org.use_memory else None)
pred, memory = org(obs.attrs, r, obs.ids, axes, held, context=ctx)
loss = ((pred - obs.target) ** 2).mean()
pending.append(loss)
affect = update(affect, loss.item(), acfg)
losses.append(loss.item()); switched.append(obs.switched); arousals.append(affect.arousal)
if on_step is not None:
on_step(t, obs, r, ctx, axes, prev, memory)
if len(pending) == bptt:
opt.zero_grad()
torch.stack(pending).mean().backward()