Loads a model saved via

Used in the notebooks

Used in the guide Used in the tutorials


model = tf.keras.Sequential([
    tf.keras.layers.Dense(5, input_shape=(3,)),
loaded_model = tf.keras.models.load_model('/tmp/model')
x = tf.random.uniform((10, 3))
assert np.allclose(model.predict(x), loaded_model.predict(x))

Note that the model weights may have different scoped names after being loaded. Scoped names include the model/layer names, such as "dense_1/kernel:0". It is recommended that you use the layer properties to access specific variables, e.g. model.get_layer("dense_1").kernel.

filepath One of the following:

  • String or pathlib.Path object, path to the saved model
  • h5py.File object from which to load the model
custom_objects Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.
compile Boolean, whether to compile the model after loading.
options Optional tf.saved_model.LoadOptions object that specifies options for loading from SavedModel.

A Keras model instance. If the original model was co