Save a Trained Keras Model Weights and Topology to a File

Chris Achard
InstructorChris Achard

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 3 years ago

Instead of training the network every single time it is run, we’ll want to save the Keras model so that we can use it in the future. Keras makes it easy to save both the model weights and topology to a single file, so we’ll do that here.

Instructor: [00:00] Every time we run this file, it defines the network and retrains the entire network before it can make a prediction. This is definitely not optimal since training will always take longer than predicting.

[00:17] Instead of predicting here, we can save the entire model definition with the trained weights by using the model's save method. The model save only takes one argument, which is the path to the file that you want to save.

[00:31] We'll save our file as meannetwork.h5. We use the h5 file extension because Keras uses the h5py library to make a binary file, but you can name this file whatever you'd like, like .model or .network.

[00:48] When we run that now, we have a saved model file. It's a binary file that we can't read in a text editor, but it contains all the model information that we need to make predictions with the model, without requiring training again.