Save your trained Model, Save your Time!

Sometimes, Deep learning model takes hours and days to train. We can’t afford to run the same thing again and again, when we work with the notebook the next day. For this, we can save that model and load that in future use. A reloaded model won’t need to be retrained.
Today I will show the HDF5 format of saving model, which stores the complete model along with:
- weights,
- compilation information (if
compile()
was called), - the optimizer and its state (if any, allows to restart training where you left) in the .h5 file format. So let’s dive in!
Saving Model
Let model
is our "trained model". So we run this code snippet to save our model:
# Load the library
from tensorflow.keras.models import load_model
# Save the model
model.save('/path_to_model/model.h5')
Load the Model in the Future and Predicting from it
In the future, whenever we need the model, we will load that like this:
# load model
m = load_model('/path_to_model/model.h5')
# predict
pred = m.predict(x)
Thanks for your interest. This was my first story. So let me know if any more good way you knew and if there’s any mistake. Happy Machine Learning!
Reference:
Image taken from https://www.vision-systems.com/home/article/16736100/deep-learning-brings-a-new-dimension-to-machine-vision