Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Save a Trained Keras Model Weights and Topology to a File


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: 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.

    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.

    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.

    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.