dreamquark-ai / tabnet

PyTorch implementation of TabNet paper : https://arxiv.org/pdf/1908.07442.pdf
https://dreamquark-ai.github.io/tabnet/
MIT License
2.61k stars 485 forks source link

AttributeError: 'TabNetClassifier' object has no attribute 'preds_mapper' #434

Closed CHr0m31 closed 2 years ago

CHr0m31 commented 2 years ago

Describe the bug Trying to load the saved model however, got the error when trying to predict the results.

What is the current behavior? `--------------------------------------------------------------------------- AttributeError Traceback (most recent call last)

in ----> 1 preds = loaded_clf.predict(x_test_0.values) 2 preds ~/anaconda3/envs/aix360/lib/python3.6/site-packages/pytorch_tabnet/abstract_model.py in predict(self, X) 269 results.append(predictions) 270 res = np.vstack(results) --> 271 return self.predict_func(res) 272 273 def explain(self, X): ~/anaconda3/envs/aix360/lib/python3.6/site-packages/pytorch_tabnet/tab_model.py in predict_func(self, outputs) 72 def predict_func(self, outputs): 73 outputs = np.argmax(outputs, axis=1) ---> 74 return np.vectorize(self.preds_mapper.get)(outputs) 75 76 def predict_proba(self, X): AttributeError: 'TabNetClassifier' object has no attribute 'preds_mapper'` **If the current behavior is a bug, please provide the steps to reproduce.** `preds = clf.predict(x_test_0.values) # save tabnet model saving_path_name = "../ROT_model/tabnet_model_normalised_1" saved_filepath = clf.save_model(saving_path_name) ` These code are ran in the background and the saved file is in zip. So i came back trying to load the model `from pytorch_tabnet.metrics import Metric from sklearn.metrics import roc_auc_score from pytorch_tabnet.tab_model import TabNetClassifier, TabNetRegressor saving_path_name = "../ROT_model/tabnet_model_normalised_1.zip" loaded_clf = TabNetClassifier() loaded_clf.load_model(saving_path_name) preds = loaded_clf.predict(x_test_0.values) preds ` The error comes after i use the loaded_clf.predict function. I also tried `preds_mapper = { idx : class_name for idx, class_name in enumerate(loaded_clf.classes_)} preds = loaded_clf.predict_proba(x_test_0.values) loaded_y_pred = np.vectorize(preds_mapper.get)(np.argmax(preds, axis=1)) ` however the error comes with "AttributeError: 'TabNetClassifier' object has no attribute 'classes_'" **Expected behavior** I was expecting for it to give me the preded value. **Screenshots** **Other relevant information:** poetry version: None python version: 3.6.13 Operating System: Ubuntu Additional tools: **Additional context**
CHr0m31 commented 2 years ago

I've retried the code `preds_mapper = { idx : class_name for idx, class_name in enumerate(loadedclf.classes)}

preds = loaded_clf.predict_proba(x_test_0.values)

loaded_y_pred = np.vectorize(preds_mapper.get)(np.argmax(preds, axis=1))` and changes the loadedclf.classes to a list of declared classes and am able to get the results.