Clone any `Model` instance. 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. Arguments: model: Instance of `Model` (could be a functional model or a Seq
(model, input_tensors=None, clone_function=None)
| 365 | |
| 366 | @keras_export('keras.models.clone_model') |
| 367 | def clone_model(model, input_tensors=None, clone_function=None): |
| 368 | """Clone any `Model` instance. |
| 369 | |
| 370 | Model cloning is similar to calling a model on new inputs, |
| 371 | except that it creates new layers (and thus new weights) instead |
| 372 | of sharing the weights of the existing layers. |
| 373 | |
| 374 | Arguments: |
| 375 | model: Instance of `Model` |
| 376 | (could be a functional model or a Sequential model). |
| 377 | input_tensors: optional list of input tensors or InputLayer objects |
| 378 | to build the model upon. If not provided, |
| 379 | placeholders will be created. |
| 380 | clone_function: Callable to be used to clone each layer in the target |
| 381 | model (except `InputLayer` instances). It takes as argument the layer |
| 382 | instance to be cloned, and returns the corresponding layer instance to |
| 383 | be used in the model copy. If unspecified, this callable defaults to |
| 384 | the following serialization/deserialization function: |
| 385 | `lambda layer: layer.__class__.from_config(layer.get_config())`. |
| 386 | By passing a custom callable, you can customize your copy of the |
| 387 | model, e.g. by wrapping certain layers of interest (you might want to |
| 388 | replace all `LSTM` instances with equivalent |
| 389 | `Bidirectional(LSTM(...))` instances, for example). |
| 390 | |
| 391 | Returns: |
| 392 | An instance of `Model` reproducing the behavior |
| 393 | of the original model, on top of new inputs tensors, |
| 394 | using newly instantiated weights. The cloned model might behave |
| 395 | differently from the original model if a custom clone_function |
| 396 | modifies the layer. |
| 397 | |
| 398 | Raises: |
| 399 | ValueError: in case of invalid `model` argument value. |
| 400 | """ |
| 401 | if clone_function is None: |
| 402 | clone_function = _clone_layer |
| 403 | |
| 404 | if isinstance(model, Sequential): |
| 405 | return _clone_sequential_model( |
| 406 | model, input_tensors=input_tensors, layer_fn=clone_function) |
| 407 | else: |
| 408 | return _clone_functional_model( |
| 409 | model, input_tensors=input_tensors, layer_fn=clone_function) |
| 410 | |
| 411 | |
| 412 | # "Clone" a subclassed model by reseting all of the attributes. |
no test coverage detected