fix: validate window<t_min and test _metrics directly (spec §8)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KAWnnPg2qvmLNySamE2k85
This commit is contained in:
2 files changed
+91
-14
No files matched your search
@@ -43,13 +43,35 @@ def _context(kind: str, affect: Affect, regime: int, n_regimes: int) -> torch.Te
|
||||
raise ValueError(kind)
|
||||
|
||||
|
||||
def _metrics(losses: list[float], switched: list[bool], arousals: list[float], window: int) -> dict:
|
||||
# metrics on the second half only: the first half is training warm-up
|
||||
steps = len(losses)
|
||||
half = steps // 2
|
||||
switch_idx = [i for i in range(half, steps - window) if switched[i]]
|
||||
recovery = [statistics.fmean(losses[i + k] for i in switch_idx) for k in range(window)] if switch_idx else [float("nan")] * window
|
||||
steady_idx = [i for i in range(half, steps) if not any(switched[max(0, i - window):i + 1])]
|
||||
steady = statistics.fmean(losses[i] for i in steady_idx) if steady_idx else float("nan")
|
||||
return dict(
|
||||
recovery=recovery, steady=steady,
|
||||
# counts switches in the measured second half only, not the whole run
|
||||
n_switches=len(switch_idx),
|
||||
arousal_at_switch=statistics.fmean(arousals[i] for i in switch_idx) if switch_idx else float("nan"),
|
||||
arousal_steady=statistics.fmean(arousals[i] for i in steady_idx) if steady_idx else float("nan"),
|
||||
)
|
||||
|
||||
|
||||
def run(name: str, seed: int, steps: int, window: int = 20, bptt: int = 20,
|
||||
lr: float = 1e-3, hidden: int = 32, kappa: float = 4.0) -> dict:
|
||||
spec = dict(CONDITIONS[name])
|
||||
if spec["kappa"] == KAPPA_FLAG:
|
||||
spec["kappa"] = kappa
|
||||
torch.manual_seed(seed)
|
||||
wcfg = ParticleConfig(seed=seed, oracle=spec["oracle"])
|
||||
if window >= wcfg.t_min:
|
||||
raise ValueError(
|
||||
f"window={window} must be < t_min={wcfg.t_min}: a recovery window that reaches "
|
||||
"the next switch blends two events into one curve"
|
||||
)
|
||||
torch.manual_seed(seed)
|
||||
world = ParticleWorld(wcfg)
|
||||
ctx_dim = wcfg.n_regimes if spec["ctx"] == "regime" else 2
|
||||
org = Organism(attr_dim=world.attr_dim, coord_dim=2, target_dim=2, context_dim=ctx_dim,
|
||||
@@ -76,18 +98,7 @@ def run(name: str, seed: int, steps: int, window: int = 20, bptt: int = 20,
|
||||
pending = []
|
||||
memory = detach_memory(memory)
|
||||
|
||||
# metrics on the second half only: the first half is training warm-up
|
||||
half = steps // 2
|
||||
switch_idx = [i for i in range(half, steps - window) if switched[i]]
|
||||
recovery = [statistics.fmean(losses[i + k] for i in switch_idx) for k in range(window)] if switch_idx else [float("nan")] * window
|
||||
steady_idx = [i for i in range(half, steps) if not any(switched[max(0, i - window):i + 1])]
|
||||
steady = statistics.fmean(losses[i] for i in steady_idx) if steady_idx else float("nan")
|
||||
return dict(
|
||||
condition=name, seed=seed, steps=steps,
|
||||
recovery=recovery, steady=steady, n_switches=len(switch_idx),
|
||||
arousal_at_switch=statistics.fmean(arousals[i] for i in switch_idx) if switch_idx else float("nan"),
|
||||
arousal_steady=statistics.fmean(arousals[i] for i in steady_idx) if steady_idx else float("nan"),
|
||||
)
|
||||
return dict(condition=name, seed=seed, steps=steps, **_metrics(losses, switched, arousals, window))
|
||||
|
||||
|
||||
def table(results: list[dict]) -> str:
|
||||
|
||||
Reference in new issue
Block a user