Model Saving and Loading in TensorFlow
Model Saving and Loading
Training a model takes time, computing, and electricity. Once you have a trained model, you need to be able to save it reliably to resume training later, share it with teammates, deploy it to production, or convert it to run on mobile devices. TensorFlow provides several formats for this, each suited to a different use case.
Why Save Models?
- Resume interrupted training: if training crashes at epoch 47 of 100, a checkpoint lets you restart from where you left off.
- Deploy to production: a saved model can be served via TensorFlow Serving, run in the browser via TF.js, or converted to run on edge devices via TFLite.
- Share reproducibility: a saved model plus its weights is a complete, runnable artefact. Anyone can load it without access to the original training code.
- Model versioning: save multiple checkpoints during training to compare performance at different stages.
SavedModel Format
SavedModel is TensorFlow's recommended format for production deployment. According to the official TensorFlow guide, 'SavedModel is the recommended way to save a complete TensorFlow program, including the model architecture, trained weights, and the computation graph itself, in a language-neutral, recoverable format.' It can be loaded by TF Serving, TFLite converter, TF.js, and C++ environments — without any Python code:
# Save in SavedModel format (creates a directory)
model.save('saved_models/my_model') # directory: my_model/
# Alternatively, save as a single .keras file (recommended for Keras objects)
model.save('my_model.keras') # single zip-format file
# Check what was saved
import os
print(os.listdir('saved_models/my_model'))
# ['assets', 'fingerprint.pb', 'saved_model.pb', 'variables']
Saving Model Weights
Sometimes you only need the weights; perhaps you want to apply them to a slightly different architecture, or you are using a checkpoint callback during training:
# Save only weights (lightweight — no graph, no optimiser state)
model.save_weights('checkpoints/weights.weights.h5')
# Save weights at every epoch using a callback
checkpoint_cb = tf.keras.callbacks.ModelCheckpoint(
filepath='checkpoints/epoch_{epoch:02d}.weights.h5',
save_weights_only=True,
save_best_only=True,
monitor='val_accuracy',
verbose=1
)
model.fit(x_train, y_train, epochs=20, callbacks=[checkpoint_cb])
Loading and Using Saved Models
import tensorflow as tf
# Load a full model (architecture + weights + optimiser state)
loaded_model = tf.keras.models.load_model('my_model.keras')
# Verify predictions match the original
import numpy as np
sample = np.random.random((1, 784)) # dummy input
print(np.allclose(
model.predict(sample),
loaded_model.predict(sample)
) # True
# Load weights only (model architecture must already be defined)
fresh_model.load_weights('checkpoints/weights.weights.h5')
Reusing Models for Inference
A loaded model is fully functional, no recompilation needed for inference. You can call predict() immediately. For production, exporting a lightweight inference-only artefact is more efficient:
# Export a lightweight inference-only SavedModel
# No original Python code required to run this artefact
model.export('exported_model')
# Load and use the exported artefact
import tensorflow as tf
reloaded = tf.saved_model.load('exported_model')
output = reloaded.serve(tf.constant(sample)) # call the serving endpoint
print(output.numpy())
Model Versioning Basics
In production, you will often have multiple versions of the same model. A simple versioning convention prevents confusion and makes rollbacks trivial:
import datetime
# Timestamp-based versioning: models/20250625_143022/
version = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
save_path = f'models/{version}'
model.save(save_path)
print(f'Model saved to {save_path}')
# TensorFlow Serving expects: models/my_model/1/, /2/, /3/ ...
# where the folder name is an integer version number
tf_serving_path = f'models/my_model/1'
model.save(tf_serving_path)










