If I want to determine the type of model i.e. from which framework was it made programmatically, is there a way to do that?
I have a model in some serialized manner(Eg. a pickle file). For simplicity purposes, assume that my model can be either tensorflow's, pytorch's or scikit learn's. How can I determine programmatically which one of these 3 is the one?
Asked
Active
Viewed 706 times
1

desertnaut
- 57,590
- 26
- 140
- 166

Sarthak Agrawal
- 321
- 4
- 17
1 Answers
2
AFAIK, I have never heard of Tensorflow/Keras and Pytorch models to be saved with pickle or joblib - these frameworks provide their own functionality for saving & loading models: see the SO threads Tensorflow: how to save/restore a model? and Best way to save a trained model in PyTorch?. Additionally, there is a Github thread reporting all kinds of issues when trying to save Tensorflow models with pickle and joblib.
Given that, if you have loaded a model with, say, pickle, it is trivial to see what type it is using type(model)
and model
. Here is a short demonstration with a scikit-learn linear regression model:
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
y = np.dot(X, np.array([1, 2])) + 3
reg = LinearRegression()
reg.fit(X, y)
# save it
import pickle
filename = 'model1.pkl'
pickle.dump(reg, open(filename, 'wb'))
Now, loading the model:
loaded_model = pickle.load(open(filename, 'rb'))
type(loaded_model)
# sklearn.linear_model._base.LinearRegression
loaded_model
# LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
This will also work with frameworks like XGBoost, LightGBM, CatBoost etc.

desertnaut
- 57,590
- 26
- 140
- 166