|TensorFlow 1 version||View source on GitHub|
Clone a Functional or Sequential
Compat aliases for migration
See Migration guide for more details.
tf.keras.models.clone_model( model, input_tensors=None, clone_function=None )
Used in the notebooks
|Used in the guide||Used in the tutorials|
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.
clone_model will not preserve the uniqueness of shared objects within the
model (e.g. a single variable attached to two distinct layers will be
restored as two separate variables).
optional list of input tensors or InputLayer objects
to build the model upon. If not provided,
Callable to be used to clone each layer in the target
An instance of
# Create a test Sequential model. model = keras.Sequential([ keras.Input(shape=(728,)), keras.layers.Dense(32, activation='relu'), keras.layers.Dense(1, activation='sigmoid'), ]) # Create a copy of the test model (with freshly initialized weights). new_model = clone_model(model)
Note that subclassed models cannot be cloned, since their internal
layer structure is not known. To achieve equivalent functionality
clone_model in the case of a subclassed model, simply make sure
that the model class implements
from_config()), and call:
new_model = model.__class__.from_config(model.get_config())