Step 4: M2, multiple-instance model in PyTorch (shared sensor MLP, attention per family, averaged seeds); the time-split report shared with M1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-11-16 19:22:53 +01:00
1 parent 975ebc7c1a
commit fef30b5ef4
4 files changed
+153 -16

No files matched your search

+6 -16
View File
@@ -49,28 +49,18 @@ def fit(x, y, w=None):
def split_time(a, data, rows, families):
"""Every station seen in training: does the information lie in the features, apart from moving to a new station?"""
from evaluate import counts, regressions, show, total
cut = {s: int(0.7 * len(r)) for s, r in rows.items()}
from evaluate import CUT, report_time_split
cut = {s: int(CUT * len(r)) for s, r in rows.items()}
x = np.concatenate([rows[s][:cut[s]] for s in data])
base, new = {}, {}
yhat = {s: np.load(Path(a.cache) / "rules" / f"{s}.npz")["yhat"][cut[s]:].copy() for s in data}
for f in families:
proba = {s: np.zeros((len(r) - cut[s], len(families))) for s, r in rows.items()}
for j, f in enumerate(families):
k = FAMILIES.index(f)
y = np.concatenate([data[s]["y"][:cut[s], k] for s in data])
w = np.concatenate([np.full(cut[s], 1 / cut[s]) for s in data]) if a.balance else None
model = fit(x, y, w)
for s in data:
yhat[s][:, k] = model.predict_proba(rows[s][cut[s]:])[:, 1] >= a.threshold
for s in data:
y = data[s]["y"][cut[s]:]
base[s] = counts(y, np.load(Path(a.cache) / "rules" / f"{s}.npz")["yhat"][cut[s]:])
new[s] = counts(y, yhat[s])
keep = lambda c: {f: v for f, v in c.items() if f in families} # noqa: E731
for s in data:
for f, name, pb, pn in regressions(keep(base[s]), keep(new[s])):
print(f" regression: {s}, {f}, {name} {pb:.0f} % -> {'—' if pn is None else f'{pn:.0f} %'}"
f" (rules {base[s][f]}, model {new[s].get(f)}: labelled, flagged, both)")
show("Last 30 % of every station", keep(total(base)), keep(total(new)))
proba[s][:, j] = model.predict_proba(rows[s][cut[s]:])[:, 1]
report_time_split(a.cache, data, proba, families, a.threshold)
def main():