🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

model.save()

AI & DATA SCIENCE // model-save

model.save() saves an entire Keras model, architecture, weights, and optimizer state, to a single file or directory, allowing it to be fully restored later.

Syntax

model.save(filepath)

Deep Dive Course

save() captures everything needed to resume working with a model exactly as it was: its architecture, its learned weights, its compiled optimizer along with its current state, and its loss/metrics configuration. Saving with a filepath ending in .keras, the modern recommended format, produces a single self-contained file; the model can later be fully reconstructed with tf.keras.models.load_model(), without needing to redefine the architecture or recompile it manually.

1Understanding model.save()

save() captures everything needed to resume working with a model exactly as it was: its architecture, its learned weights, its compiled optimizer along with its current state, and its loss/metrics configuration. Saving with a filepath ending in .keras, the modern recommended format, produces a single self-contained file; the model can later be fully reconstructed with tf.keras.models.load_model(), without needing to redefine the architecture or recompile it manually.

💡

Use the modern .keras file extension when calling model.save() — it's the current recommended single-file format, replacing the older SavedModel directory and HDF5 (.h5) formats for most use cases.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import os

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
model.save('my_model.keras')
print(os.path.exists('my_model.keras'))
localhost:3000

2Practical Example

Here is a real-world application of model.save() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='adam', loss='mse')
model.save('my_model.keras')

restored = tf.keras.models.load_model('my_model.keras')
print(restored.optimizer.__class__.__name__)
localhost:3000

3Best Practices

Follow these guidelines when working with model.save():

1. Save the full model with model.save() when you need to later restore it completely, ready to keep training or make predictions immediately, without re-defining or re-compiling anything

2. Use the .keras file extension for the modern, recommended single-file save format

3. Save periodically during long training runs, such as via a ModelCheckpoint callback, rather than relying on a single save at the very end that a crash could prevent

⚠️

Tip: Use the modern .keras file extension when calling model.save() — it's the current recommended single-file format, replacing the older SavedModel directory and HDF5 (.h5) formats for most use cases.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import os

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
model.save('my_model.keras')
print(os.path.exists('my_model.keras'))
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import os

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
model.save('my_model.keras')
print(os.path.exists('my_model.keras'))
Example 02Advanced Example
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='adam', loss='mse')
model.save('my_model.keras')

restored = tf.keras.models.load_model('my_model.keras')
print(restored.optimizer.__class__.__name__)

Best Practices

  • Save the full model with model.save() when you need to later restore it completely, ready to keep training or make predictions immediately, without re-defining or re-compiling anything
  • Use the .keras file extension for the modern, recommended single-file save format
  • Save periodically during long training runs, such as via a ModelCheckpoint callback, rather than relying on a single save at the very end that a crash could prevent

Interview Question

What's the key difference between what model.save() preserves compared to model.save_weights()?

Hint: Think beyond just the numeric weight values — what else does a fully trained, ready-to-resume model need?

model.save_weights() stores only the numeric values of the model's trainable parameters, the weights and biases themselves, with no information at all about the architecture that connects them, the compiled optimizer, its type, learning rate, and internal state like momentum accumulators, or the loss/metrics configuration. model.save() captures all of that together: the full architecture, so the model can be reconstructed from scratch without you needing to redefine it in code, the complete weight values, and the compiled optimizer's exact state, meaning training can resume later with the optimizer picking up exactly where it left off, rather than starting fresh. This makes save() the right choice when you want to fully restore a model ready to keep training or predict immediately, while save_weights() is more appropriate when you already have the architecture defined in code and just need to load previously learned parameter values into it.

Exercises

MediumPractice using model.save() in a real scenario.
View Solution
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import os

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
model.save('my_model.keras')
print(os.path.exists('my_model.keras'))

Frequently Asked Questions

What's the key difference between what model.save() preserves compared to model.save_weights()?

model.save_weights() stores only the numeric values of the model's trainable parameters, the weights and biases themselves, with no information at all about the architecture that connects them, the compiled optimizer, its type, learning rate, and internal state like momentum accumulators, or the loss/metrics configuration. model.save() captures all of that together: the full architecture, so the model can be reconstructed from scratch without you needing to redefine it in code, the complete weight values, and the compiled optimizer's exact state, meaning training can resume later with the optimizer picking up exactly where it left off, rather than starting fresh. This makes save() the right choice when you want to fully restore a model ready to keep training or predict immediately, while save_weights() is more appropriate when you already have the architecture defined in code and just need to load previously learned parameter values into it.

Related Functions

tf-keras-models-load-modelmodel-save-weightsmodel-load-weights