Saving and Loading Models

Deep Learning with Keras and TensorFlow 8 min min read Updated: Nov 16, 2026 Beginner
Saving and Loading Models
Beginner Topic 7 of 8

Why Save Models?

Training can take minutes or even days. You do not want to repeat it each time you need a prediction. Saving lets you store the trained model and use it later, share it or deploy it.

The .keras Format

The recommended way in Keras 3 is a single file with the extension .keras. It stores the architecture, the weights, the optimizer state and the compile settings.

python # Save model.save("digit_model.keras") # Load import keras loaded = keras.models.load_model("digit_model.keras") # Use it right away print(loaded.evaluate(x_test, y_test)) preds = loaded.predict(x_test[:3])
Note: Older tutorials use .h5 or the TensorFlow SavedModel folder with model.save(). In Keras 3, use .keras for saving and loading models. You can still use model.export() for the SavedModel format used in serving.

Continue Training

Because the optimizer state is saved, you can load the model and keep training from where you stopped.

python loaded = keras.models.load_model("digit_model.keras") loaded.fit(x_train, y_train, epochs=3)

Saving Only the Weights

python model.save_weights("my.weights.h5") # Later: build the same architecture, then load weights new_model = build_model() new_model.load_weights("my.weights.h5")

Weights files must end with .weights.h5 in Keras 3.

Export for Serving

python model.export("exported_model") # creates a TensorFlow SavedModel folder

An exported model is made for inference only (predictions), and is used with TensorFlow Serving or converted to TensorFlow Lite.

Custom Layers and Saving

If your model contains custom classes, tell Keras about them when loading:

python loaded = keras.models.load_model( "custom.keras", custom_objects={"MyLayer": MyLayer} )
GoalCommand
Save everythingmodel.save("m.keras")
Load everythingkeras.models.load_model("m.keras")
Save weights onlymodel.save_weights("m.weights.h5")
Export for servingmodel.export("folder")
Security tip: Only load model files from sources you trust. A model file can contain code, so treat it like a program.

Conclusion

Saving and loading models is easy and essential. Use .keras for most of your work and export when you deploy.

Quick Summary: model.save('x.keras') and keras.models.load_model('x.keras'). Use save_weights for weights only and export for serving.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators