Load and use saved Keras model.h5

Viewed 5562

I try to a KerasClassifier (wrapper) into final_model.h5

validator  = GridSearchCV(estimator=clf, param_grid=param_grid)
grid_result = validator.fit(train_images, train_labels)
best_estimator = grid_result.best_estimator_
best_estimator.model.save("final_model.h5")

And then I want to reuse the model

from keras.models import load_model
loaded_model = load_model("final_model.h5")

But it seems like loaded_model is now a Sequential object instead. In other words it is different from KerasClassifier object like best_estimator

I want to reuse some method like score which is available in KerasClassifier, which is not available in Sequential model. What should I do?

Also, I would like to know more about how to continue the training process left off on final_model.h5. What can I do next?

1 Answers

Yes, in the end you saved the Keras model as HDF5, not the KerasClassifier that is just an adapter to use with scikit-learn.

But you don't really need the KerasClassifier instance, you want the score function and this in keras is called evaluate, so just call model.evaluate(X, Y) and this will return a list containing first the loss and then any metrics that your model used (most likely accuracy).

To continue training the model, just load it and call model.fit with the new training set and that's it.

Related