-
Notifications
You must be signed in to change notification settings - Fork 12
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
Save a trained model for future use? #24
Comments
Thanks! Currently there isn't a way to do this, no. It seems a very sensible thing to allow though. I think it would require each type of model (GPFlow, sckit-learn and keras) having I will flag this as a feature request and try to implement it when I have a chance but would also be very happy to review pull-requests that implement it. In the meantime, for the random forest model (only) I think you should be able to just use pickle:
|
Thanks for the reply. That solution doesn't appear to work in my case pickle.dump(rf_model, 'rf_model.pkl')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_14674/1446057301.py in <module>
----> 1 pickle.dump(rf_model, 'rf_model.pkl')
TypeError: file must have a 'write' attribute
b Likewise with with open("rf_model.pkl","wb") as f:
pickle.dump(rf_model, f)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_14674/851769721.py in <module>
1 with open("rf_model.pkl","wb") as f:
----> 2 pickle.dump(rf_model, f)
TypeError: can't pickle tensorflow.python._pywrap_tfe.EagerContextThreadLocalData objects
|
Ah OK, that's because of some of the |
@duncanwp It seems like the |
Yes, you're absolutely right. It's also possible (but a little convoluted currently) to wrap the loaded model back in to ESEm: # Save the sklearn model held internally in the esem wrapper
with open("rf_model.pkl","wb") as f:
pickle.dump(esem_rf_model.model.model, f)
# Load it again
with open("rf_model.pkl","rb") as f:
skmodel=pickle.load(f)
# Wrap the loaded model
from esem.wrappers import wrap_data
from esem.data_processors import Flatten
from esem.model_adaptor import SKLearnModel
from esem.emulator import Emulator
from sklearn.ensemble import RandomForestRegressor
wrapped_skmodel = SKLearnModel(skmodel)
# Note that we need to reload the data seperately (this is used internally for post-processing)
data = wrap_data(y_train, data_processors=[Flatten()])
loaded_esem_rf_model = Emulator(wrapped_skmodel, x_train, data)
loaded_esem_rf_model.predict(...) I made a full example here: https://gist.github.com/duncanwp/e4b96690da5bb0bf2505bb94d5450001 |
Thanks @duncanwp ! I managed to save a RF model. I'll leave this issue open in case you want this as documentation for a feature request. Otherwise, feel free to close. Thanks again! |
Hi there @duncanwp thanks for that work around. Just bumping this thread that this would be great to have! I was able to use your workaround for a GPFlow model, but it would be great to have this as method of an Emulator object too! |
Hi @adrifoster , part of the reason this stalled was because it didn't seem to work for tensor flow based emulators - did you find it worked OK for the GPFLow one though? |
Wow! Great project - thanks for your hard work.
Is there a way to save a trained model, load it into a new notebook, and run inference? Apologies if this is documented somewhere.
e.g.
and then in a new notebook
The text was updated successfully, but these errors were encountered: