tejasvaidhya's picture
Copy of tejasvaidhya/tabiclv2-regression-replication-stage2-stage3
78034f6 verified
Raw History Blame Contribute Delete
1.77 kB
"""Minimal inference for the TabICLv2 regression replication checkpoints (official TabICLv2 regressor format).
pip install "tabicl==2.2.0" scikit-learn huggingface_hub
python inference.py # stage3-10k.ckpt
python inference.py --ckpt stage2-40k.ckpt
Or from your own code:
from inference import load_regressor
reg = load_regressor().fit(X_train, y_train); reg.predict(X_test)
"""
import argparse, os
HERE = os.path.dirname(os.path.abspath(__file__))
CKPT = os.path.join(HERE, "stage3-10k.ckpt")
def load_regressor(ckpt=None, device=None, **kw):
"""TabICLRegressor of the public tabicl package on a checkpoint of this repo (a path relative to this folder or absolute)."""
from tabicl import TabICLRegressor
import torch
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
path = CKPT if ckpt is None else (ckpt if os.path.isabs(ckpt) else os.path.join(HERE, ckpt))
return TabICLRegressor(model_path=path, allow_auto_download=False, device=device, **kw)
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--ckpt", default=None, help="a checkpoint path relative to this folder")
ap.add_argument("--device", default=None)
args = ap.parse_args()
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
X, y = load_diabetes(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0)
p = load_regressor(args.ckpt, args.device).fit(Xtr, ytr).predict(Xte)
rmse = float(np.sqrt(np.mean((p - yte) ** 2))); r2 = 1 - np.sum((p - yte) ** 2) / np.sum((yte - yte.mean()) ** 2)
print(f"diabetes: RMSE {rmse:.3f} R^2 {r2:.4f}")