Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Save TuneSearchCV object with tensorflow and keras models #249

Open
Goicu opened this issue Sep 6, 2022 · 7 comments
Open

Save TuneSearchCV object with tensorflow and keras models #249

Goicu opened this issue Sep 6, 2022 · 7 comments

Comments

@Goicu
Copy link

Goicu commented Sep 6, 2022

I try with pickle, joblib and dill.

With pickle I had this error:
AttributeError: Can't pickle local object 'MaximumIterationStopper.__init__.<locals>.<lambda>'

With joblib I had this error:
raise PicklingError(_pickle.PicklingError: Can't pickle <function MaximumIterationStopper.__init__.<locals>.<lambda> at 0x7f3093158790>: it's not found as ray.tune.stopper.MaximumIterationStopper.__init__.<locals>.<lambda>

With dill I had this error:
TypeError: cannot pickle 'tensorflow.python._pywrap_tf_session.TF_Operation' object

I don't have defined any lambda function in my code. I suppose that the lambda function is inside the keras or tensorflow libraries.

@Yard1
Copy link
Member

Yard1 commented Sep 6, 2022

Thanks for the report. As a workaround, can you try with cloudpickle?

@Goicu
Copy link
Author

Goicu commented Sep 6, 2022

Thanks for the tip Yard1. But still don't work.

With cloudpickle:

from ray.cloudpickle import cloudpickle

cloudpickle.pickle.dump(trained_model, f)

Error:
AttributeError: Can't pickle local object 'MaximumIterationStopper.__init__.<locals>.<lambda>'


from ray import cloudpickle or import cloudpickle

cloudpickle.dump(trained_model, f)

Error:
TypeError: cannot pickle 'weakref' object

@Yard1
Copy link
Member

Yard1 commented Sep 6, 2022

Why do you need to save the TuneSearchCV object in the first place? What's the use case? Doesn't the model by itself suffice?

@Goicu
Copy link
Author

Goicu commented Sep 7, 2022

You are complete right. Actually what I want is to save the refit trained model. But I do not know how either.

@Yard1
Copy link
Member

Yard1 commented Sep 7, 2022

You can obtain the best model by calling TuneSearchCV.best_estimator_ after fitting had completed. You can then save it following instructions for the framework you are using.

@Goicu
Copy link
Author

Goicu commented Sep 7, 2022

Thanks again Yard1.

With TuneSearchCV.best_estimator_ I couldn't save it either because I am using a KerasClassifier estimator.

I understand the porpoise of the CV like it is said here:
https://stackoverflow.com/questions/55814197/kerasclassifier-object-has-no-attribute-save

But I suppose that the TuneSearchCV has the refit param for this porpoise and with this as I am in research and not in production this will save me the task of retrain the model with the best_params_ founds in the search.

But perhaps this was not contemplated.

@Yard1
Copy link
Member

Yard1 commented Sep 7, 2022

The refit param merely specifies that the best estimator will be returned in its trained form. However, using best_params_ and refitting the estimator yourself is also a solution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants