Independent model copy
Create an architecture copy for independent experiments without sharing parameters with the original.
Keras · Function
Creates a new Functional or Sequential model with the same structure but newly instantiated layers and weights. Optional callbacks can customize layer cloning, layer invocation, and recursive handling of nested models.
clone_model reconstructs a Functional or Sequential model as a new Model. It can use replacement layer and call functions, optionally provide new inputs, and recursively clone nested models; the result has newly created weights rather than sharing the source model's weights.
keras.models.clone_model(model, input_tensors=None, clone_function=None, call_function=None, recursive=False, **kwargs)| Name | Type | Description |
|---|---|---|
model | Model | The Functional or Sequential Model instance to copy. |
input_tensors | list of tensors or InputLayer objects, optional | Optional input tensors or InputLayer objects on which to build the copy; omitted inputs are newly created. |
clone_function | callable, optional | Optional callback with signature fn(layer), returning the replacement for each non-input layer. |
call_function | callable, optional | Optional callback with signature fn(layer, *args, **kwargs), used to call cloned layers. |
recursive | boolean, optional | Whether nested Functional or Sequential models are cloned recursively. |
**kwargs | keyword arguments | Additional keyword arguments accepted by the model-cloning implementation. |
An instance of Model reproducing the original model's behavior on new input tensors, with newly instantiated weights.
import keras
inputs = keras.Input(shape=(4,))
x = keras.layers.Dense(8, activation="relu")(inputs)
outputs = keras.layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
cloned = keras.models.clone_model(model)
cloned.compile(optimizer="adam", loss="mse")
Creates a fresh architecture with newly initialized weights; compilation is not copied.
Create an architecture copy for independent experiments without sharing parameters with the original.
Supply replacement layers through clone_function to modify selected parts while rebuilding the model.
Use new input tensors to rebuild the same model structure around alternate inputs.
Propagate cloning behavior into nested Functional or Sequential models.
It clones Functional or Sequential models by creating new layers and weights, rather than sharing the original model's weights. If input_tensors is omitted, it creates new Input objects.
Follow-up: How would you preserve trained weights? Copy the weights explicitly after cloning, for example with cloned.set_weights(model.get_weights()).
clone_function receives each layer and returns the layer to place in the copy. By default, Keras recreates a layer from its configuration, while a custom callback can alter the cloned architecture.
Follow-up: What happens if the callback changes a layer? The resulting model may behave differently because the callback controls the replacement layer.
With recursive=False, nested Functional or Sequential models are cloned through clone_function. With recursive=True, nested models are cloned with clone_model using the same clone_function, call_function, and recursive settings.
Follow-up: When is recursive cloning useful? It is useful when nested models must receive the same cloning customization as the outer model.
keras.models.Modelkeras.Inputkeras.layers.Layerget_configfrom_config