Save a Trained Keras Model Weights and Topology to a File

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 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.

egghead
egghead
~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today