|
| 1 | +__signature__ |
| 2 | +keras.models.clone_model( |
| 3 | + model, |
| 4 | + input_tensors=None, |
| 5 | + clone_function=None, |
| 6 | + call_function=None, |
| 7 | + recursive=False, |
| 8 | + **kwargs |
| 9 | +) |
| 10 | +__doc__ |
| 11 | +Clone a Functional or Sequential `Model` instance. |
| 12 | + |
| 13 | +Model cloning is similar to calling a model on new inputs, |
| 14 | +except that it creates new layers (and thus new weights) instead |
| 15 | +of sharing the weights of the existing layers. |
| 16 | + |
| 17 | +Note that |
| 18 | +`clone_model` will not preserve the uniqueness of shared objects within the |
| 19 | +model (e.g. a single variable attached to two distinct layers will be |
| 20 | +restored as two separate variables). |
| 21 | + |
| 22 | +Args: |
| 23 | + model: Instance of `Model` |
| 24 | + (could be a Functional model or a Sequential model). |
| 25 | + input_tensors: optional list of input tensors or InputLayer objects |
| 26 | + to build the model upon. If not provided, |
| 27 | + new `Input` objects will be created. |
| 28 | + clone_function: Callable with signature `fn(layer)` |
| 29 | + to be used to clone each layer in the target |
| 30 | + model (except `Input` instances). It takes as argument the |
| 31 | + layer instance to be cloned, and returns the corresponding layer |
| 32 | + instance to be used in the model copy. If unspecified, this callable |
| 33 | + defaults to the following serialization/deserialization function: |
| 34 | + `lambda layer: layer.__class__.from_config(layer.get_config())`. |
| 35 | + By passing a custom callable, you can customize your copy of the |
| 36 | + model, e.g. by wrapping certain layers of interest (you might want |
| 37 | + to replace all `LSTM` instances with equivalent |
| 38 | + `Bidirectional(LSTM(...))` instances, for example). |
| 39 | + Defaults to `None`. |
| 40 | + call_function: Callable with signature |
| 41 | + `fn(layer, *args, **kwargs)` to be used to call each |
| 42 | + cloned layer and a set of inputs. It takes the layer instance, |
| 43 | + the call arguments and keyword arguments, and returns the |
| 44 | + call outputs. If unspecified, this callable defaults to |
| 45 | + the regular `__call__()` method: |
| 46 | + `def fn(layer, *args, **kwargs): return layer(*args, **kwargs)`. |
| 47 | + By passing a custom callable, you can insert new layers before or |
| 48 | + after a given layer. Note: this argument can only be used with |
| 49 | + Functional models. |
| 50 | + recursive: Boolean. Whether to recursively clone any Sequential |
| 51 | + or Functional models encountered in the original |
| 52 | + Sequential/Functional model. If `False`, |
| 53 | + then inner models are cloned by calling `clone_function()`. |
| 54 | + If `True`, then inner models are cloned by calling `clone_model()` |
| 55 | + with the same `clone_function`, `call_function`, and `recursive` |
| 56 | + arguments. Note that in this case, `call_function` |
| 57 | + will not be propagated to any Sequential model |
| 58 | + (since it is not applicable to Sequential models). |
| 59 | + |
| 60 | +Returns: |
| 61 | + An instance of `Model` reproducing the behavior |
| 62 | + of the original model, on top of new inputs tensors, |
| 63 | + using newly instantiated weights. The cloned model may behave |
| 64 | + differently from the original model if a custom `clone_function` |
| 65 | + or `call_function` modifies a layer or layer call. |
| 66 | + |
| 67 | +Example: |
| 68 | + |
| 69 | +```python |
| 70 | +# Create a test Sequential model. |
| 71 | +model = keras.Sequential([ |
| 72 | + keras.layers.Input(shape=(728,)), |
| 73 | + keras.layers.Dense(32, activation='relu'), |
| 74 | + keras.layers.Dense(1, activation='sigmoid'), |
| 75 | +]) |
| 76 | +# Create a copy of the test model (with freshly initialized weights). |
| 77 | +new_model = clone_model(model) |
| 78 | +``` |
| 79 | + |
| 80 | +Using a `clone_function` to make a model deterministic by setting the |
| 81 | +random seed everywhere: |
| 82 | + |
| 83 | +```python |
| 84 | +def clone_function(layer): |
| 85 | + config = layer.get_config() |
| 86 | + if "seed" in config: |
| 87 | + config["seed"] = 1337 |
| 88 | + return layer.__class__.from_config(config) |
| 89 | + |
| 90 | +new_model = clone_model(model) |
| 91 | +``` |
| 92 | + |
| 93 | +Using a `call_function` to add a `Dropout` layer after each `Dense` layer |
| 94 | +(without recreating new layers): |
| 95 | + |
| 96 | +```python |
| 97 | +def call_function(layer, *args, **kwargs): |
| 98 | + out = layer(*args, **kwargs) |
| 99 | + if isinstance(layer, keras.layers.Dense): |
| 100 | + out = keras.layers.Dropout(0.5)(out) |
| 101 | + return out |
| 102 | + |
| 103 | +new_model = clone_model( |
| 104 | + model, |
| 105 | + clone_function=lambda x: x, # Reuse the same layers. |
| 106 | + call_function=call_function, |
| 107 | +) |
| 108 | +``` |
| 109 | + |
| 110 | +Note that subclassed models cannot be cloned by default, |
| 111 | +since their internal layer structure is not known. |
| 112 | +To achieve equivalent functionality |
| 113 | +as `clone_model` in the case of a subclassed model, simply make sure |
| 114 | +that the model class implements `get_config()` |
| 115 | +(and optionally `from_config()`), and call: |
| 116 | + |
| 117 | +```python |
| 118 | +new_model = model.__class__.from_config(model.get_config()) |
| 119 | +``` |
| 120 | + |
| 121 | +In the case of a subclassed model, you cannot using a custom |
| 122 | +`clone_function`. |
| 123 | + |
0 commit comments