TabPFN icon indicating copy to clipboard operation
TabPFN copied to clipboard

save an ICL tuned model

Open miraculixx opened this issue 11 months ago • 4 comments

How can a fitted model be saved? For example, scikit learn models can generally be exported to disk by using joblib or cloudpickle as noted here

Thanks for the amazing work.

miraculixx avatar Jan 09 '25 10:01 miraculixx

We had a similar decision on our Discord before, take a look at https://discord.com/channels/1285598202732482621/1316254428654866462 and let me know if it works for you!

LennartPurucker avatar Jan 09 '25 10:01 LennartPurucker

In summary from the Discord discussion, you can save the model with pickle as expected:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import pickle
from tabpfn import TabPFNClassifier

# Load data
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

# Train classifier
classifier = TabPFNClassifier(device='cpu')
classifier.fit(X_train, y_train)

# Save the trained classifier to a file
with open('tabpfn_classifier.pkl', 'wb') as f:
    pickle.dump(classifier, f)

# Can be in a separate code
# Load the classifier from the file
with open('tabpfn_classifier.pkl', 'rb') as f:
    loaded_classifier = pickle.load(f)

# Predict
y_pred, p_pred = loaded_classifier.predict(X_test)

LennartPurucker avatar Jan 10 '25 14:01 LennartPurucker

I'd keep this open until we've properly documented it @LennartPurucker right?

noahho avatar Jan 10 '25 16:01 noahho

Mhm, we are not diverging from the default, so I do not know if we need extra documentation. Nevertheless, we could clarify what saving a fitted TabPFN model even means and the alternatives, such as KV caching.

Let us keep it open for now.

LennartPurucker avatar Jan 13 '25 08:01 LennartPurucker

I just implemented an improved workflow that allows you to save a model independent of the checkpoint which means you don't have to save model weights every time and saving is independent of pickle! "Q: How do I save and load a trained TabPFN model?" in the READM

noahho avatar Jun 27 '25 17:06 noahho

Hello, is this https://arc.net/l/quote/swwdgajw then deprecated? It doesn't work for me. I'm getting model not found on model_loading although I do have it.

RodrigoEstrela avatar Aug 14 '25 14:08 RodrigoEstrela