ray-project / tune-sklearn

A drop-in replacement for Scikit-Learn’s GridSearchCV / RandomizedSearchCV -- but with cutting edge hyperparameter tuning techniques.
https://docs.ray.io/en/master/tune/api_docs/sklearn.html
Apache License 2.0
467 stars 51 forks source link

Save TuneSearchCV object with tensorflow and keras models #249

Open Goicu opened 2 years ago

Goicu commented 2 years ago

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 commented 2 years ago

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

Goicu commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

Yard1 commented 2 years ago

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 commented 2 years ago

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 bestparams founds in the search.

But perhaps this was not contemplated.

Yard1 commented 2 years ago

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