File size: 3,385 Bytes
b19e8c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Re-run the best grid configurations (plus the 2026-10-16 stable baseline) across 8 seeds.
Usage: seed_check.py <workspace.pkl> <out.jsonl> <shard> <n_shards>
Resumable: (config, seed) pairs already in <out.jsonl> are skipped."""
import json
import pickle
import sys
from dataclasses import asdict, replace
from pathlib import Path

import numpy as np
import torch
from sklearn.metrics import recall_score

import biolmnet.training as training

WORKSPACE, OUT, SHARD, SHARDS = Path(sys.argv[1]), Path(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])
torch.set_num_threads(2)
SEEDS = [0, 1, 2, 3, 7, 42, 123, 2024]

GRID_BASE = dict(epochs=200, learning_rate=0.01, weight_decay=0.01, dropout=0.3, optimizer="Adam")
CONFIGS = {
    "A b32 p64 tanh/tanh/relu": dict(GRID_BASE, batch_size=32, projection_dim=64,
                                     biological_activation="tanh", projection_activation="tanh", fusion_activation="relu"),
    "B b16 p128 relu/relu/tanh": dict(GRID_BASE, batch_size=16, projection_dim=128,
                                      biological_activation="relu", projection_activation="relu", fusion_activation="tanh"),
    "C b32 p128 tanh/relu/relu": dict(GRID_BASE, batch_size=32, projection_dim=128,
                                      biological_activation="tanh", projection_activation="relu", fusion_activation="relu"),
    "D b32 p64 tanh/relu/tanh": dict(GRID_BASE, batch_size=32, projection_dim=64,
                                     biological_activation="tanh", projection_activation="relu", fusion_activation="tanh"),
    "E b32 p128 tanh/relu/tanh": dict(GRID_BASE, batch_size=32, projection_dim=128,
                                      biological_activation="tanh", projection_activation="relu", fusion_activation="tanh"),
    # 2026-10-16 stable setting: Adam lr 1e-3, weight_decay 1e-4, published activations, 100-epoch limit.
    "BASE lr1e-3 wd1e-4 relu/sigmoid/tanh": dict(epochs=100, weight_decay=1e-4),
}

workspace = pickle.loads(WORKSPACE.read_bytes())
names = workspace.label_names
done = set()
if OUT.exists():
    done = {(r["config"], r["seed"]) for r in map(json.loads, OUT.read_text().splitlines()) if r}

jobs = [(c, s) for c in CONFIGS for s in SEEDS]
for index, (config, seed) in enumerate(jobs):
    if index % SHARDS != SHARD or (config, seed) in done:
        continue
    hp = replace(training.Hyperparameters(), seed=seed, **CONFIGS[config])
    result = training.train(workspace, hp)
    m = result.bundle.metrics
    vp = result.validation_predictions
    counts = vp["predicted"].value_counts().reindex(names, fill_value=0).astype(int).tolist()
    row = {
        "config": config, "seed": seed, "hyperparameters": asdict(hp),
        "accuracy": m["accuracy"], "f1_macro": m["f1_macro"], "f1_weighted": m["f1_weighted"],
        "epochs_completed": m["epochs_completed"],
        "best_epoch": int(min(result.bundle.history, key=lambda h: h["validation_loss"])["epoch"]),
        "pred_counts": counts, "collapsed": int((np.array(counts) > 0).sum()) == 1,
        "recall": recall_score(vp["observed"], vp["predicted"], labels=names, average=None, zero_division=0).tolist(),
    }
    with OUT.open("a") as handle:
        handle.write(json.dumps(row) + "\n")
    print(f"[{SHARD}] {config} seed={seed} f1m={row['f1_macro']:.3f} collapsed={row['collapsed']}", flush=True)
print(f"SHARD {SHARD} DONE", flush=True)