load_model() reverses model.save(), reconstructing the full model from the saved file — its architecture, its learned weights, and its compiled optimizer along with its saved state — ready to immediately call predict(), evaluate(), or continue training with fit() exactly as if the original model object had never gone away. Unlike loading only weights with load_weights(), this requires no matching architecture already defined in code, since the architecture itself is reconstructed directly from the saved file.
1Understanding tf.keras.models.load_model()
load_model() reverses model.save(), reconstructing the full model from the saved file — its architecture, its learned weights, and its compiled optimizer along with its saved state — ready to immediately call predict(), evaluate(), or continue training with fit() exactly as if the original model object had never gone away. Unlike loading only weights with load_weights(), this requires no matching architecture already defined in code, since the architecture itself is reconstructed directly from the saved file.
A model restored with load_model() is immediately ready to continue training with fit(), since the optimizer's saved state, not just its type, is restored too — training can genuinely resume rather than restart from a freshly initialized optimizer.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(2, activation='relu', input_shape=(3,))])
model.compile(optimizer='adam', loss='mse')
model.save('my_model.keras')
restored = tf.keras.models.load_model('my_model.keras')
print(restored.output_shape)2Practical Example
Here is a real-world application of tf.keras.models.load_model() showing how it is used in production TensorFlow code.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(2, input_shape=(3,))])
model.compile(optimizer='adam', loss='mse')
model.save('my_model.keras')
restored = tf.keras.models.load_model('my_model.keras')
print(len(restored.layers))3Best Practices
Follow these guidelines when working with tf.keras.models.load_model():
1. Use load_model() when you need a fully working model restored with no architecture code required beforehand, unlike load_weights() which needs a matching architecture already built
2. Verify the restored model's architecture with model.summary() after loading, especially when loading a model saved by a different version of the code
3. Continue training a restored model directly with fit() when needed, since its optimizer state is fully restored, not reset
Tip: A model restored with load_model() is immediately ready to continue training with fit(), since the optimizer's saved state, not just its type, is restored too — training can genuinely resume rather than restart from a freshly initialized optimizer.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(2, activation='relu', input_shape=(3,))])
model.compile(optimizer='adam', loss='mse')
model.save('my_model.keras')
restored = tf.keras.models.load_model('my_model.keras')
print(restored.output_shape)