text
stringlengths 0
4.99k
|
---|
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). This can also be a path to a SavedModel saved from model.save.
|
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).
|
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
|
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.
|
Usage:
|
# for a Functional API model
|
tf.keras.Model().from_config(model.get_config())
|
# for a Sequential model
|
tf.keras.Sequential().from_config(model.get_config())
|
Arguments
|
config: Configuration dictionary.
|
custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.
|
Returns
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.