File size: 1,777 Bytes
d1988f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fast end-to-end smoke test: every model must train and predict on a tiny task."""

from __future__ import annotations

import argparse
import traceback

import numpy as np
import torch

from lnpbench import data, metrics, paths
from lnpbench.registry import REGISTRY, build_model
from lnpbench.store import FeatureStore
from lnpbench.trainer import TrainConfig


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--models", nargs="+", default=None)
    ap.add_argument("--n-train", type=int, default=1500)
    ap.add_argument("--epochs", type=int, default=2)
    ap.add_argument("--device", default="cpu")
    args = ap.parse_args()

    store = FeatureStore()
    tasks = data.load_tasks(paths.CACHE_DIR / "tasks.pkl")
    task = [t for t in tasks if t.name == "BL_2023_fold0"][0]
    rng = np.random.default_rng(0)
    train_idx = np.sort(rng.choice(task.train_idx, size=min(args.n_train, len(task.train_idx)), replace=False))
    test_idx = task.test_idx[:200]

    models = args.models or list(REGISTRY)
    failures = []
    for name in models:
        try:
            model = build_model(name)
            cfg = TrainConfig(epochs=args.epochs, batch_size=128, device=args.device, seed=0, patience=99)
            model.fit(store, train_idx, val_idx=test_idx, cfg=cfg)
            pred = model.predict(store, test_idx)
            rho = metrics.spearman(store.y[test_idx], pred)
            print(f"OK   {name:24s} rho={rho:.4f} pred_mean={pred.mean():.3f} n={len(pred)}", flush=True)
        except Exception as exc:  # noqa: BLE001
            failures.append(name)
            print(f"FAIL {name}: {exc}")
            traceback.print_exc()

    print("\nfailures:", failures if failures else "none")


if __name__ == "__main__":
    main()