» 케라스 API / Models API / Model saving & serialization APIs

Model saving & serialization APIs

save method

Model.save(
    filepath,
    overwrite=True,
    include_optimizer=True,
    save_format=None,
    signatures=None,
    options=None,
)

Saves the model to Tensorflow SavedModel or a single HDF5 file.

The savefile includes:

  • The model architecture, allowing to re-instantiate the model.
  • The model weights.
  • The state of the optimizer, allowing to resume training exactly where you left off.

This allows you to save the entirety of the state of a model in a single file.

Saved models can be re-instantiated via keras.models.load_model. The model returned by load_model is a compiled model ready to be used (unless the saved model was never compiled in the first place).

Models built with the Sequential and Functional API can be saved to both the HDF5 and SavedModel formats. Subclassed models can only be saved with the SavedModel format.

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.

Arguments

  • filepath: String, PathLike, path to SavedModel or H5 file to save the model.
  • overwrite: Whether to silently overwrite any existing file at the target location, or provide the user with a manual prompt.
  • include_optimizer: If True, save optimizer's state together.
  • save_format: Either 'tf' or 'h5', indicating whether to save the model to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X, and 'h5' in TF 1.X.
  • signatures: Signatures to save with the SavedModel. Applicable to the 'tf' format only. Please see the signatures argument in tf.saved_model.save for details.
  • options: Optional tf.saved_model.SaveOptions object that specifies options for saving to SavedModel.

Example

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

save_model function

tf.keras.models.save_model(
    model,
    filepath,
    overwrite=True,
    include_optimizer=True,
    save_format=None,
    signatures=None,
    options=None,
)

Saves a model as a TensorFlow SavedModel or HDF5 file.

Usage:

>>> model = tf.keras.Sequential([
...     tf.keras.layers.Dense(5, input_shape=(3,)),
...     tf.keras.layers.Softmax()])
>>> model.save('/tmp/model')
>>> 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))

The saved model contains:

- the model's configuration (topology)
- the model's weights
- the model's optimizer's state (if any)

Thus the saved model can be reinstantiated in the exact same state, without any of the code used for model definition or training.

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.

SavedModel serialization

The SavedModel serialization path uses tf.saved_model.save to save the model and all trackable objects attached to the model (e.g. layers and variables). @tf.function-decorated methods are also saved. Additional trackable objects and functions are added to the SavedModel to allow the model to be loaded back as a Keras Model object.

Arguments

  • model: Keras model instance to be saved.
  • filepath: One of the following:
    • String or pathlib.Path object, path where to save the model
    • h5py.File object where to save the model
  • overwrite: Whether we should overwrite any existing model at the target location, or instead ask the user with a manual prompt.
  • include_optimizer: If True, save optimizer's state together.
  • save_format: Either 'tf' or 'h5', indicating whether to save the model to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X, and 'h5' in TF 1.X.
  • signatures: Signatures to save with the SavedModel. Applicable to the 'tf' format only. Please see the signatures argument in tf.saved_model.save for details.
  • options: Optional tf.saved_model.SaveOptions object that specifies options for saving to SavedModel.

Raises

  • ImportError: If save format is hdf5, and h5py is not available.

load_model function

tf.keras.models.load_model(
    filepath, custom_objects=None, compile=True, options=None
)

Loads a model saved via model.save().

Usage:

>>> model = tf.keras.Sequential([
...     tf.keras.layers.Dense(5, input_shape=(3,)),
...     tf.keras.layers.Softmax()])
>>> model.save('/tmp/model')
>>> 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.

Arguments

  • 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.

Returns

A Keras model instance. If the original model was compiled, and saved with the optimizer, then the returned model will be compiled. Otherwise, the model will be left uncompiled. In the case that an uncompiled model is returned, a warning is displayed if the compile argument is set to True.

Raises

  • ImportError: if loading from an hdf5 file and h5py is not available.
  • IOError: In case of an invalid savefile.

get_weights method

Model.get_weights()

Retrieves the weights of the model.

Returns

A flat list of Numpy arrays.


set_weights method

Model.set_weights(weights)

Sets the weights of the layer, from Numpy arrays.

The weights of a layer represent the state of the layer. This function sets the weight values from numpy arrays. The weight values should be passed in the order they are created by the layer. Note that the layer's weights must be instantiated before calling this function by calling the layer.

For example, a Dense layer returns a list of two values-- per-output weights and the bias value. These can be used to set the weights of another Dense layer:

>>> a = tf.keras.layers.Dense(1,
...   kernel_initializer=tf.constant_initializer(1.))
>>> a_out = a(tf.convert_to_tensor([[1., 2., 3.]]))
>>> a.get_weights()
[array([[1.],
       [1.],
       [1.]], dtype=float32), array([0.], dtype=float32)]
>>> b = tf.keras.layers.Dense(1,
...   kernel_initializer=tf.constant_initializer(2.))
>>> b_out = b(tf.convert_to_tensor([[10., 20., 30.]]))
>>> b.get_weights()
[array([[2.],
       [2.],
       [2.]], dtype=float32), array([0.], dtype=float32)]
>>> b.set_weights(a.get_weights())
>>> b.get_weights()
[array([[1.],
       [1.],
       [1.]], dtype=float32), array([0.], dtype=float32)]

Arguments

  • weights: a list of Numpy arrays. The number of arrays and their shape must match number of the dimensions of the weights of the layer (i.e. it should match the output of get_weights).

Raises

  • ValueError: If the provided weights list does not match the layer's specifications.

save_weights method

Model.save_weights(filepath, overwrite=True, save_format=None, options=None)

Saves all layer weights.

Either saves in HDF5 or in TensorFlow format based on the save_format argument.

When saving in HDF5 format, the weight file has: - layer_names (attribute), a list of strings (ordered names of model layers). - For every layer, a group named layer.name - For every such layer group, a group attribute weight_names, a list of strings (ordered names of weights tensor of the layer). - For every weight in the layer, a dataset storing the weight value, named after the weight tensor.

When saving in TensorFlow format, all objects referenced by the network are saved in the same format as tf.train.Checkpoint, including any Layer instances or Optimizer instances assigned to object attributes. For networks constructed from inputs and outputs using tf.keras.Model(inputs, outputs), Layer instances used by the network are tracked/saved automatically. For user-defined classes which inherit from tf.keras.Model, Layer instances must be assigned to object attributes, typically in the constructor. See the documentation of tf.train.Checkpoint and tf.keras.Model for details.

While the formats are the same, do not mix save_weights and tf.train.Checkpoint. Checkpoints saved by Model.save_weights should be loaded using Model.load_weights. Checkpoints saved using tf.train.Checkpoint.save should be restored using the corresponding tf.train.Checkpoint.restore. Prefer tf.train.Checkpoint over save_weights for training checkpoints.

The TensorFlow format matches objects and variables by starting at a root object, self for save_weights, and greedily matching attribute names. For Model.save this is the Model, and for Checkpoint.save this is the Checkpoint even if the Checkpoint has a model attached. This means saving a tf.keras.Model using save_weights and loading into a tf.train.Checkpoint with a Model attached (or vice versa) will not match the Model's variables. See the guide to training checkpoints for details on the TensorFlow format.

Arguments

  • filepath: String or PathLike, path to the file to save the weights to. When saving in TensorFlow format, this is the prefix used for checkpoint files (multiple files are generated). Note that the '.h5' suffix causes weights to be saved in HDF5 format.
  • overwrite: Whether to silently overwrite any existing file at the target location, or provide the user with a manual prompt.
  • save_format: Either 'tf' or 'h5'. A filepath ending in '.h5' or '.keras' will default to HDF5 if save_format is None. Otherwise None defaults to 'tf'.
  • options: Optional tf.train.CheckpointOptions object that specifies options for saving weights.

Raises

  • ImportError: If h5py is not available when attempting to save in HDF5 format.
  • ValueError: For invalid/unknown format arguments.

load_weights method

Model.load_weights(filepath, by_name=False, skip_mismatch=False, options=None)

Loads all layer weights, either from a TensorFlow or an HDF5 weight file.

If by_name is False weights are loaded based on the network's topology. This means the architecture should be the same as when the weights were saved. Note that layers that don't have weights are not taken into account in the topological ordering, so adding or removing layers is fine as long as they don't have weights.

If by_name is True, weights are loaded into layers only if they share the same name. This is useful for fine-tuning or transfer-learning models where some of the layers have changed.

Only topological loading (by_name=False) is supported when loading weights from the TensorFlow format. Note that topological loading differs slightly between TensorFlow and HDF5 formats for user-defined classes inheriting from tf.keras.Model: HDF5 loads based on a flattened list of weights, while the TensorFlow format loads based on the object-local names of attributes to which layers are assigned in the Model's constructor.

Arguments

  • filepath: String, path to the weights file to load. For weight files in TensorFlow format, this is the file prefix (the same as was passed to save_weights).
  • by_name: Boolean, whether to load weights by name or by topological order. Only topological loading is supported for weight files in TensorFlow format.
  • skip_mismatch: Boolean, whether to skip loading of layers where there is a mismatch in the number of weights, or a mismatch in the shape of the weight (only valid when by_name=True).
  • options: Optional tf.train.CheckpointOptions object that specifies options for loading weights.

Returns

When loading a weight file in TensorFlow format, returns the same status object as tf.train.Checkpoint.restore. When graph building, restore ops are run automatically as soon as the network is built (on first call for user-defined classes inheriting from Model, immediately if it is already built).

When loading weights in HDF5 format, returns None.

Raises

  • ImportError: If h5py is not available and the weight file is in HDF5 format.
  • ValueError: If skip_mismatch is set to True when by_name is False.

get_config method

Model.get_config()

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Returns

Python dictionary.


from_config method

Model.from_config(config, custom_objects=None)

Creates a layer from its config.

This method is the reverse of get_config, capable of instantiating the same layer from the config dictionary. It does not handle layer connectivity (handled by Network), nor weights (handled by set_weights).

Arguments

  • config: A Python dictionary, typically the output of get_config.

Returns

A layer instance.


model_from_config function

tf.keras.models.model_from_config(config, custom_objects=None)

Instantiates a Keras model from its config.

Arguments

  • config: Configuration dictionary.
  • custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.

Returns

A Keras model instance (uncompiled).

Raises

  • TypeError: if config is not a dictionary.

to_json method

Model.to_json(**kwargs)

Returns a JSON string containing the network configuration.

To load a network from a JSON save file, use keras.models.model_from_json(json_string, custom_objects={}).

Arguments

  • **kwargs: Additional keyword arguments to be passed to json.dumps().

Returns

A JSON string.


model_from_json function

tf.keras.models.model_from_json(json_string, custom_objects=None)

Parses a JSON model configuration string and returns a model instance.

Usage:

>>> model = tf.keras.Sequential([
...     tf.keras.layers.Dense(5, input_shape=(3,)),
...     tf.keras.layers.Softmax()])
>>> config = model.to_json()
>>> loaded_model = tf.keras.models.model_from_json(config)

Arguments

  • json_string: JSON string encoding a model configuration.
  • custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.

Returns

A Keras model instance (uncompiled).


clone_model function

tf.keras.models.clone_model(model, input_tensors=None, clone_function=None)

Clone any Model instance.

Model cloning is similar to calling a model on new inputs, except that it creates new layers (and thus new weights) instead of sharing the weights of the existing layers.

Arguments

  • model: Instance of Model (could be a functional model or a Sequential model).
  • input_tensors: optional list of input tensors or InputLayer objects to build the model upon. If not provided, placeholders will be created.
  • clone_function: Callable to be used to clone each layer in the target model (except InputLayer instances). It takes as argument the layer instance to be cloned, and returns the corresponding layer instance to be used in the model copy. If unspecified, this callable defaults to the following serialization/deserialization function: lambda layer: layer.__class__.from_config(layer.get_config()). By passing a custom callable, you can customize your copy of the model, e.g. by wrapping certain layers of interest (you might want to replace all LSTM instances with equivalent Bidirectional(LSTM(...)) instances, for example).

Returns

An instance of Model reproducing the behavior of the original model, on top of new inputs tensors, using newly instantiated weights. The cloned model might behave differently from the original model if a custom clone_function modifies the layer.

Raises

  • ValueError: in case of invalid model argument value.