-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Saving models? #14
Comments
Hi @xloffree, First of all, you may save a fitted model in PiML using the following approach.
You may also register the loaded model into PiML using the demo at https://colab.research.google.com/github/SelfExplainML/PiML-Toolbox/blob/main/examples/Example_ExternalModels.ipynb#scrollTo=7WGJ8PzutkLh, "Scenario 2: Register external fitted models with dataset". Second, all the interactive panels used in PiML are based on python runtime. Currently, we don't have such functionality to export interactive results; the best way is to save the notebook as static Html: a) Click on "Widgets -> Save Notebook Widget State"; b) Export it as Html by "File -> Download as -> HTML (.html)". |
Hi, thank you for your help with this. Has this solution worked for you with PiML? When I try this solution, this line: results in a recursion error every time. I tried changing the recursion limit but even with a recursion limit pf 10000000 I still run into this error. Increasing the recursion limit indefinitely just causes the kernel to crash. The error is as follows: RecursionError Traceback (most recent call last) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:272, in load(file, ignore, **kwds) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:419, in Unpickler.load(self) File piml/models/glm.py:32, in piml.models.glm.GLMRegressor.getattr() File piml/models/glm.py:32, in piml.models.glm.GLMRegressor.getattr()
File piml/models/glm.py:32, in piml.models.glm.GLMRegressor.getattr() RecursionError: maximum recursion depth exceeded while calling a Python object Any help with this would be very appreciated. When using PiML for research purposes, being able to save a trained model is essential for reproducibility. Thank you! |
Hi @xloffree, For GLM, you may use the following code to do model saving,
|
Thank you very much. This works. How can I use this trained model to predict on other datasets? Is this functionality exclusively part of PiML or is there third party documentation I can view for more background on how this code works? Thank you |
If you have another dataset with the same input features set, then you can use this model to get predictions. Assume the new data has covariates "X" (the raw scale without preprocessing), then you can get the prediction using the fitted model in PiML via the following way.
|
What datatype should X be here? Is it a dataframe that includes all of the data for all of the predictors? Thanks |
Hi, Is different code required to save each different type of built-in model in PiML? It seems whenever I try to save a different model, I run into a new error. Is there somewhere where I can see the code for how to save each different type of model? |
The X is the numpy array of the selected features. It should have the same data format as the uploaded raw data, without any preprocessing. |
For the GLMRegressor model, use
For all the rest models, you can use
BTW, we will provide a unified API for model saving in the next release. |
I still do not understand what this means. I have tried to pass df and df.columns as X but it does not work. Do you have an example of what X should be? Thank you |
Would it be possible for us to discuss PiML over a zoom meeting? That might be more efficient than messages on this page. |
Hi, here X is just an n*p numpy array, where n is the sample size and p is the number of predictors (excluding unselected features and the response feature). For instance, assume the raw data is a pd.DataFrame as follows,
Then you selected season, yr, mnth, hr as the covariates in exp.data_summary and exp.feature_select, and cnt as the response (in exp.data_prepare). The X is supposed to be a np.array that looks like:
An example of X would be, which is the selected covariates of the loaded data.
Hope that helps. |
Saving a model works for glm and gaml. After that, none of the other models will save and they result in an error: PicklingError Traceback (most recent call last) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:235, in dump(obj, file, protocol, byref, fmode, recurse, **kwds) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:394, in Pickler.dump(self, obj) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:487, in _Pickler.dump(self, obj) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:388, in Pickler.save(self, obj, save_persistent_id) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:603, in _Pickler.save(self, obj, save_persistent_id) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:717, in _Pickler.save_reduce(self, func, args, state, listitems, dictitems, state_setter, obj) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:388, in Pickler.save(self, obj, save_persistent_id) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:560, in _Pickler.save(self, obj, save_persistent_id) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:1186, in save_module_dict(pickler, obj) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:971, in _Pickler.save_dict(self, obj) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:997, in _Pickler._batch_setitems(self, items) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/site-packages/dill/_dill.py:388, in Pickler.save(self, obj, save_persistent_id) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:589, in _Pickler.save(self, obj, save_persistent_id) File /n/holylfs05/LABS/liang_lab_l3/Lab/piml_py39_shared/lib/python3.9/pickle.py:1070, in _Pickler.save_global(self, obj, name) PicklingError: Can't pickle <cyfunction Model.register_model..sklearn_is_fitted at 0x2ae1c932aee0>: it's not found as piml.workflow.base.Model.register_model..sklearn_is_fitted |
Thanks for reporting this issue. You may use the following scripts to save and load a fitted model except for GLMRegressor.
|
Thanks. I am able to save every model as a .pkl file now. How can I easily load the model and view its interpretability metrics within PiML? For example, if I have an EBM model saved as a .pkl, and I want to view the results of exp.model_interpret(), how can I do this without retraining? Thank you |
You can do the following to register it into the PiML workflow:
Note that in this case, you need to do data loading, summary, and preparation first, so that all the data are available. |
Hi,
Is there a way to save models generated in PIML so that I do not need to run the program and train the model each time?
Also, what is the best way to export results? Is there any way to export results such that the widgets are still interactive? I have just been saving the notebook as an html file in order to share results.
Thank you
The text was updated successfully, but these errors were encountered: