Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pickle Keras model?

Tags:

Official documents state that "It is not recommended to use pickle or cPickle to save a Keras model."

However, my need for pickling Keras model stems from hyperparameter optimization using sklearn's RandomizedSearchCV (or any other hyperparameter optimizers). It's essential to save the results to a file, since then the script can be executed remotely in a detached session etc.

Essentially, I want to:

trial_search = RandomizedSearchCV( estimator=keras_model, ... )
pickle.dump( trial_search, open( "trial_search.pickle", "wb" ) )
like image 322
Sida Zhou Avatar asked Jan 17 '18 07:01

Sida Zhou


People also ask

Can we save Keras model as pickle?

As of now, Keras models are pickle-able. But we still recommend using model. save() to save model to disk.

How do you pickle a model?

To save the ML model using Pickle all we need to do is pass the model object into the dump() function of Pickle. This will serialize the object and convert it into a “byte stream” that we can save as a file called model. pkl .

How do I save the Keras model in Tensorflow?

Save the entire modelCall tf.keras.Model.save to save a model's architecture, weights, and training configuration in a single file/folder . This allows you to export a model so it can be used without access to the original Python code*.

How do you save a Keras best model?

We can use the Keras callback keras. callbacks. ModelCheckpoint() to save the model at its best performing epoch.


1 Answers

As of now, Keras models are pickle-able. But we still recommend using model.save() to save model to disk.

like image 102
farizrahman4u Avatar answered Sep 22 '22 23:09

farizrahman4u