Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Function

keras.models.clone_model

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.

How it works

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.

Syntax

keras.models.clone_model(model, input_tensors=None, clone_function=None, call_function=None, recursive=False, **kwargs)

Parameters

NameTypeDescription
modelModelThe Functional or Sequential Model instance to copy.
input_tensorslist of tensors or InputLayer objects, optionalOptional input tensors or InputLayer objects on which to build the copy; omitted inputs are newly created.
clone_functioncallable, optionalOptional callback with signature fn(layer), returning the replacement for each non-input layer.
call_functioncallable, optionalOptional callback with signature fn(layer, *args, **kwargs), used to call cloned layers.
recursiveboolean, optionalWhether nested Functional or Sequential models are cloned recursively.
**kwargskeyword argumentsAdditional keyword arguments accepted by the model-cloning implementation.

Returns

An instance of Model reproducing the original model's behavior on new input tensors, with newly instantiated weights.

Key points

  • The function accepts Functional and Sequential model instances.
  • The clone has separately instantiated layers and weights, so it does not share the source model's weight objects.
  • Omitting input_tensors makes Keras construct fresh input objects for the cloned model.
  • clone_function receives one layer at a time and returns its replacement layer.
  • call_function controls how each cloned layer is invoked with its inputs and call arguments.
  • recursive determines whether nested Functional or Sequential models are independently cloned with clone_model.

Common mistakes

  • Shared objects are not guaranteed to remain shared: one variable attached to multiple layers can become separate variables in the clone.
  • A custom clone_function is applied to layers other than Input instances, so input handling is not customized through that callback.

Example: Clone a functional model

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.

When to use it

Independent model copy

Create an architecture copy for independent experiments without sharing parameters with the original.

Customize cloned layers

Supply replacement layers through clone_function to modify selected parts while rebuilding the model.

Rebuild on alternate inputs

Use new input tensors to rebuild the same model structure around alternate inputs.

Clone nested models

Propagate cloning behavior into nested Functional or Sequential models.

Interview questions

QWhat does clone_model copy, and what does it not share?

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()).

QHow can clone_function customize cloning?

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.

QWhat does recursive control?

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.Model
  • keras.Input
  • keras.layers.Layer
  • get_config
  • from_config

Source: Model config serialization. Checked 2026-10-15.