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

+123
View File
@@ -0,0 +1,123 @@
"""M2, the model of the pilot: multiple-instance learning. A label names a station and a range, not its sensors, and
each station has its own number of sensors: one small MLP, shared by every sensor, reads a sensor's features with the
station's; per family, an attention over the sensors pools their scores into the station's. The attention weights give
the sensors (step 6).
Inputs: every feature through sign(x) log(1 + |x|) (jumps reach thousands of noises), NaN set to 0 with a presence mask.
Training: binary cross-entropy per family on the reviewed labels (fine-tuning only: the pre-training is step 5), each
station drawn as often as the others.
default: the criterion, trained on the first 70 % of every station, scored on the last 30 % (evaluate.CUT);
--leave-out: the indicator, each station predicted by the model trained on the others; one <STATION>.npz per station
in --out, with yhat [instant, family] (the rules outside the pilot families), proba and attention [instant, sensor, family].
Usage: python m2.py --cache <cache> [--leave-out --out <folder>] [--families Visibility,Obstruction] [--epochs 200 --seeds 5]
"""
import argparse
from pathlib import Path
import numpy as np
import torch
from torch import nn
from dataset import FAMILIES
from evaluate import CUT, report_time_split
BATCHES, BATCH = 8, 128 # per station and per epoch: every station weighs the same
def prepare(d):
"""A station of the cache -> tensors xs [T, S, 2 Fs], xc [T, 2 Fc]: the squashed features and their presence."""
def squash(x):
m = np.isfinite(x)
v = np.where(m, x, 0)
return np.concatenate([np.sign(v) * np.log1p(np.abs(v)), m], axis=-1).astype(np.float32)
return torch.from_numpy(squash(d["xs"])), torch.from_numpy(squash(d["xc"]))
class MIL(nn.Module):
def __init__(self, fs, fc, k, hidden=64, dropout=0.1):
super().__init__()
self.encoder = nn.Sequential(nn.Linear(fs + fc, hidden), nn.ReLU(), nn.Dropout(dropout),
nn.Linear(hidden, hidden), nn.ReLU())
self.attention = nn.Linear(hidden, k) # per family: how much a sensor counts for the station
self.score = nn.Linear(hidden, k) # per family: the sensor's own score
def forward(self, xs, xc):
"""xs [B, S, Fs], xc [B, Fc] -> station logits [B, K], attention [B, S, K]."""
h = self.encoder(torch.cat([xs, xc[:, None, :].expand(-1, xs.shape[1], -1)], dim=-1))
attention = torch.softmax(self.attention(h), dim=1)
return (attention * self.score(h)).sum(dim=1), attention
def train(parts, k_index, epochs, seed=0, proportional=False):
"""parts: [(xs, xc, y [T, K])], one per station -> the trained model. Each station drawn as often as the others, or
in proportion to its instants."""
torch.manual_seed(seed)
rng = np.random.default_rng(seed)
sizes = np.array([len(p[2]) for p in parts], float)
model = MIL(parts[0][0].shape[-1], parts[0][1].shape[-1], len(k_index))
opt = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-4)
loss = nn.BCEWithLogitsLoss()
model.train()
for _ in range(epochs):
for _ in range(BATCHES):
order = (rng.choice(len(parts), len(parts), p=sizes / sizes.sum()) if proportional
else rng.permutation(len(parts)))
for i in order:
xs, xc, y = parts[i]
b = torch.from_numpy(rng.integers(0, len(y), BATCH))
logits, _ = model(xs[b], xc[b])
opt.zero_grad()
loss(logits, y[b][:, k_index]).backward()
opt.step()
return model.eval()
@torch.no_grad()
def predict(model, xs, xc):
out = [model(xs[i:i + 1024], xc[i:i + 1024]) for i in range(0, len(xs), 1024)]
return torch.sigmoid(torch.cat([o[0] for o in out])).numpy(), torch.cat([o[1] for o in out]).numpy()
def main():
p = argparse.ArgumentParser(usage=__doc__)
p.add_argument("--cache", required=True)
p.add_argument("--families", default="Visibility,Obstruction")
p.add_argument("--epochs", type=int, default=200)
p.add_argument("--seeds", type=int, default=5, help="models averaged: one run alone varies by 10 to 20 points")
p.add_argument("--threshold", type=float, default=0.5)
p.add_argument("--proportional", action="store_true", help="stations drawn in proportion to their instants")
p.add_argument("--leave-out", action="store_true")
p.add_argument("--out")
a = p.parse_args()
families = a.families.split(",")
k_index = torch.tensor([FAMILIES.index(f) for f in families])
data = {q.stem: np.load(q) for q in sorted(Path(a.cache).glob("*.npz"))}
tensors = {s: (*prepare(d), torch.from_numpy(d["y"].astype(np.float32))) for s, d in data.items()}
def ensemble(parts):
return [train(parts, k_index, a.epochs, seed, a.proportional) for seed in range(a.seeds)]
def averaged(models, xs, xc):
out = [predict(m, xs, xc) for m in models]
return np.mean([o[0] for o in out], axis=0), np.mean([o[1] for o in out], axis=0)
if not a.leave_out:
cut = {s: int(CUT * len(d["y"])) for s, d in data.items()}
models = ensemble([tuple(t[:cut[s]] for t in tensors[s]) for s in data])
proba = {s: averaged(models, tensors[s][0][cut[s]:], tensors[s][1][cut[s]:])[0] for s in data}
report_time_split(a.cache, data, proba, families, a.threshold)
return
out = Path(a.out)
out.mkdir(parents=True, exist_ok=True)
for test in data:
proba, attention = averaged(ensemble([tensors[s] for s in data if s != test]), *tensors[test][:2])
yhat = np.load(Path(a.cache) / "rules" / f"{test}.npz")["yhat"].copy()
for j, f in enumerate(families):
yhat[:, FAMILIES.index(f)] = proba[:, j] >= a.threshold
np.savez_compressed(out / f"{test}.npz", yhat=yhat, proba=proba, attention=attention.astype(np.float16),
families=np.array(families))
print(test, {f: int(yhat[:, FAMILIES.index(f)].sum()) for f in families}, flush=True)
if __name__ == "__main__":
main()