File size: 2,026 Bytes
7c2d684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Minimal inference for the TabICLv2 replication baseline after stages 2 and 3 (12 untied ICL blocks, 27.6M parameters).

    pip install "tabicl==2.1.1" pandas scikit-learn huggingface_hub   # the version used for all reported numbers
    python inference.py                                             # demo on sklearn's breast-cancer data, final stage-3 model
    python inference.py --ckpt tabiclv2-replication-stage2-40k.ckpt # end of stage 2
    python inference.py --ckpt checkpoints/stage2/step-20000.ckpt   # any intermediate checkpoint

Or from your own code:
    from inference import load_classifier
    clf = load_classifier().fit(X_train, y_train); clf.predict_proba(X_test)
"""
import argparse, os
HERE = os.path.dirname(os.path.abspath(__file__))
CKPT = os.path.join(HERE, "tabiclv2-replication-stage3-10k.ckpt")


def load_classifier(ckpt=None, device=None, **kw):
    """A TabICLClassifier on the checkpoint (stock architecture; no patch needed)."""
    from tabicl import TabICLClassifier
    import torch
    device = device or ("cuda" if torch.cuda.is_available() else "cpu")
    return TabICLClassifier(model_path=ckpt or CKPT, allow_auto_download=False, device=device, **kw)


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--ckpt", default=None, help="another checkpoint, e.g. checkpoints/stage3/step-05000.ckpt")
    ap.add_argument("--device", default=None)
    args = ap.parse_args()
    from sklearn.datasets import load_breast_cancer
    from sklearn.metrics import accuracy_score, roc_auc_score
    from sklearn.model_selection import train_test_split
    X, y = load_breast_cancer(return_X_y=True, as_frame=True)
    Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
    clf = load_classifier(args.ckpt, args.device).fit(Xtr, ytr)
    p = clf.predict_proba(Xte)
    print(f"checkpoint: {os.path.basename(args.ckpt or CKPT)} | accuracy {accuracy_score(yte, p.argmax(1)):.3f} | ROC AUC {roc_auc_score(yte, p[:, 1]):.3f}")