Introduction
SavedModel format lets you save your trained TensorFlow model so you can use it later without retraining.
model.save('path_to_folder') # To load the model later: loaded_model = tf.keras.models.load_model('path_to_folder')
model.save('my_model')loaded_model = tf.keras.models.load_model('my_model')model.save('models/house_price_predictor')import tensorflow as tf import numpy as np # Create a simple model model = tf.keras.Sequential([ tf.keras.layers.Dense(5, activation='relu', input_shape=(3,)), tf.keras.layers.Dense(1) ]) model.compile(optimizer='adam', loss='mse') # Generate some dummy data x_train = np.random.random((10, 3)) y_train = np.random.random((10, 1)) # Train the model model.fit(x_train, y_train, epochs=3, verbose=0) # Save the model model.save('saved_model_example') # Load the model loaded_model = tf.keras.models.load_model('saved_model_example') # Predict with the loaded model predictions = loaded_model.predict(x_train) print('Predictions shape:', predictions.shape) print('First prediction:', predictions[0])
import tensorflow as tf
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.save('my_model')
loaded_model = tf.keras.models.load_model('my_model')
print(type(loaded_model))model.save('model_path') but when loading with tf.keras.models.load_model('wrong_path'), you get an error. What is the likely cause?model.save_weights('deploy_model') and load with model.load_weights('deploy_model') saves only weights, not full model. Save with model.save('deploy_model.h5') and load with tf.keras.models.load_model('deploy_model.h5') saves in HDF5, which is less preferred for deployment. Save with tf.saved_model.save(model, 'deploy_model') and load with tf.saved_model.load('deploy_model') uses low-level API which returns a different object, not a Keras model.Pass the test and this pattern moves unknown