When I first started working with Keras in Python, one of the biggest challenges I faced was figuring out how to properly save my trained models. After spending hours training a neural network, I wanted to make sure I could reuse it later without retraining from scratch.
Saving a Keras model is one of those essential skills every Python developer working with deep learning should know. Whether you’re building a customer churn predictor for a U.S. retail company or a sentiment analysis model for social media data, saving your model ensures your hard work is preserved.
In this tutorial, I’ll show you different ways to save a Keras model in Python, step by step. I’ll also share some practical tips I’ve learned over the years to help you avoid common mistakes.
Why You Should Save Your Keras Model
When you train a model in Keras, it learns weights and parameters from your data. These weights represent the “knowledge” your model has gained. If you close your Python session without saving, all of that is lost.
By saving your model, you can:
- Reuse it later without retraining.
- Share it with others or deploy it in production.
- Save time and computing resources.
Now, let’s dive into the methods I use to save and load Keras models in Python.
Method 1: Save the Entire Keras Model Using model.save()
This is the most common and easiest method. It saves everything, the model’s architecture, weights, and optimizer state, in one file.
Here’s how I usually do it in my Python projects.
# Import necessary libraries
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Step 1: Build a simple Keras model
model = Sequential([
Dense(16, activation='relu', input_shape=(10,)),
Dense(8, activation='relu'),
Dense(1, activation='sigmoid')
])
# Step 2: Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Step 3: Train the model using dummy data
import numpy as np
X_train = np.random.rand(100, 10)
y_train = np.random.randint(2, size=(100, 1))
model.fit(X_train, y_train, epochs=5, batch_size=10)
# Step 4: Save the entire model
model.save('customer_churn_model.h5')
print("Model saved successfully as 'customer_churn_model.h5'")I executed the above example code and added the screenshot below.

This code saves your Keras model in the HDF5 format (.h5), which is widely used and portable. I often use this format when sharing models with teammates or deploying them in Flask or FastAPI applications.
To load the model later, you can use:
# Load the saved Keras model
loaded_model = keras.models.load_model('customer_churn_model.h5')
# Evaluate the loaded model
loss, accuracy = loaded_model.evaluate(X_train, y_train)
print(f"Loaded model accuracy: {accuracy:.2f}")This method is perfect when you want a one-file solution that stores everything.
Method 2: Save Only the Model Weights
Sometimes, I only need the weights of the model, not the full architecture or optimizer. This is useful when you want to retrain or fine-tune a model later.
Here’s how I save and load only the weights in Python.
# Save only the weights
model.save_weights('customer_churn_weights.h5')
print("Model weights saved successfully!")
# Load the weights back into the model
model.load_weights('customer_churn_weights.h5')
print("Model weights loaded successfully!")I executed the above example code and added the screenshot below.

This method is faster and results in smaller files. However, you’ll need to have the same model architecture defined in your Python code when loading the weights.
Method 3: Save the Model in TensorFlow’s SavedModel Format
The SavedModel format is the default for TensorFlow 2.x, and it’s the one I recommend for production environments. It saves your model in a directory structure that includes everything: architecture, weights, and training configuration.
Here’s how you can use it in Python:
# Save the model in TensorFlow SavedModel format
model.save('saved_model/customer_churn_model')
print("Model saved in TensorFlow SavedModel format!")
# Load the model back
loaded_model_tf = keras.models.load_model('saved_model/customer_churn_model')
# Check if the model works
loss, accuracy = loaded_model_tf.evaluate(X_train, y_train)
print(f"Loaded TensorFlow model accuracy: {accuracy:.2f}")I executed the above example code and added the screenshot below.

This format is ideal if you plan to deploy your Keras model using TensorFlow Serving or convert it to TensorFlow Lite for mobile applications.
Method 4: Save Model Architecture as JSON or YAML
Sometimes, I like to separate the model architecture from the weights. This gives me flexibility when I want to modify or inspect the model design later.
Here’s how I do it using JSON in Python:
# Save the model architecture to JSON
model_json = model.to_json()
with open("customer_churn_model.json", "w") as json_file:
json_file.write(model_json)
# Save weights separately
model.save_weights("customer_churn_weights_only.h5")
print("Model architecture and weights saved separately!")
# Load the model architecture
from tensorflow.keras.models import model_from_json
with open("customer_churn_model.json", "r") as json_file:
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("customer_churn_weights_only.h5")
print("Model loaded successfully from JSON and weights!")If you prefer YAML (though it’s less common nowadays), you can use model.to_yaml() and model_from_yaml() similarly.
Method 5: Automatically Save Best Models Using ModelCheckpoint
When I train deep learning models in Python, I often use callbacks to automatically save the best version of my model during training. This helps me avoid overfitting and ensures I don’t lose the best-performing model.
Here’s how I use the ModelCheckpoint callback in Keras:
from tensorflow.keras.callbacks import ModelCheckpoint
# Define a checkpoint callback
checkpoint = ModelCheckpoint(
filepath='best_model.h5',
monitor='val_accuracy',
save_best_only=True,
mode='max',
verbose=1
)
# Train the model with validation data
model.fit(
X_train, y_train,
validation_split=0.2,
epochs=10,
batch_size=10,
callbacks=[checkpoint]
)
print("Best model automatically saved during training!")This method is great for long training sessions. You can walk away from your computer knowing that Keras will save the best model automatically.
Common Mistakes to Avoid When Saving Keras Models
After years of working with Keras in Python, I’ve noticed a few common mistakes developers make:
- Forgetting to save the model after training: Always save right after training completes.
- Overwriting old models: Use versioned filenames like model_v1.h5, model_v2.h5, etc.
- Not saving the optimizer state: If you plan to resume training, use model.save() instead of save_weights().
- Using incompatible TensorFlow/Keras versions: Always note your environment versions when saving models.
Avoiding these small mistakes can save you hours of debugging later.
Bonus Tip: Save and Load Models in Google Colab or Jupyter Notebook
If you’re working in Google Colab or Jupyter Notebook, saving and loading models work the same way. However, if you want to download the model to your local machine, you can use:
from google.colab import files
files.download('customer_churn_model.h5')This will download the saved Keras model directly to your computer, which is super handy when you’re experimenting in the cloud.
Saving a Keras model in Python is not just a technical step; it’s a best practice that ensures your work is reproducible and ready for deployment. Whether you’re building a predictive model for a U.S. retail chain or experimenting with customer segmentation, saving your model properly will save you time and effort.
I’ve personally used all these methods in real-world Python projects, and each has its place depending on the use case. If you’re just starting, begin with model.save(). As your projects grow, explore TensorFlow’s SavedModel format and callbacks for automated saving.
You may also like to read:
- How to Install and Set Up Keras in Python
- How to Uninstall Keras in Python?
- Build Your First Neural Network in Python Using Keras
- How to Update Keras in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.