MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / clone_and_build_model

Function clone_and_build_model

tensorflow/python/keras/models.py:564–692  ·  view source on GitHub ↗

Clone a `Model` and build/compile it with the same settings used before. This function can be be run in the same graph or in a separate graph from the model. When using a separate graph, `in_place_reset` must be `False`. Note that, currently, the clone produced from this function may not wor

(
    model, input_tensors=None, target_tensors=None, custom_objects=None,
    compile_clone=True, in_place_reset=False, optimizer_iterations=None,
    optimizer_config=None)

Source from the content-addressed store, hash-verified

562
563
564def clone_and_build_model(
565 model, input_tensors=None, target_tensors=None, custom_objects=None,
566 compile_clone=True, in_place_reset=False, optimizer_iterations=None,
567 optimizer_config=None):
568 """Clone a `Model` and build/compile it with the same settings used before.
569
570 This function can be be run in the same graph or in a separate graph from the
571 model. When using a separate graph, `in_place_reset` must be `False`.
572
573 Note that, currently, the clone produced from this function may not work with
574 TPU DistributionStrategy. Try at your own risk.
575
576 Args:
577 model: `tf.keras.Model` object. Can be Functional, Sequential, or
578 sub-classed.
579 input_tensors: Optional list or dictionary of input tensors to build the
580 model upon. If not provided, placeholders will be created.
581 target_tensors: Optional list of target tensors for compiling the model. If
582 not provided, placeholders will be created.
583 custom_objects: Optional dictionary mapping string names to custom classes
584 or functions.
585 compile_clone: Boolean, whether to compile model clone (default `True`).
586 in_place_reset: Boolean, whether to reset the model in place. Only used if
587 the model is a subclassed model. In the case of a subclassed model,
588 this argument must be set to `True` (default `False`). To restore the
589 original model, use the function
590 `in_place_subclassed_model_state_restoration(model)`.
591 optimizer_iterations: An iterations variable that will be incremented by the
592 optimizer if the clone is compiled. This argument is used when a Keras
593 model is cloned into an Estimator model function, because Estimators
594 create their own global step variable.
595 optimizer_config: Optimizer config dictionary or list of dictionary
596 returned from `get_config()`. This argument should be defined if
597 `clone_and_build_model` is called in a different graph or session from
598 the original model, and the optimizer is an instance of `OptimizerV2`.
599
600 Returns:
601 Clone of the model.
602
603 Raises:
604 ValueError: Cloning fails in the following cases
605 - cloning a subclassed model with `in_place_reset` set to False.
606 - compiling the clone when the original model has not been compiled.
607 """
608 # Grab optimizer now, as we reset-in-place for subclassed models, but
609 # want to maintain access to the original optimizer.
610 orig_optimizer = model.optimizer
611 if compile_clone and not orig_optimizer:
612 raise ValueError(
613 'Error when cloning model: compile_clone was set to True, but the '
614 'original model has not been compiled.')
615
616 if model._is_graph_network or isinstance(model, Sequential):
617 if custom_objects:
618 with CustomObjectScope(custom_objects):
619 clone = clone_model(model, input_tensors=input_tensors)
620 else:
621 clone = clone_model(model, input_tensors=input_tensors)

Callers

nothing calls this directly

Calls 9

CustomObjectScopeClass · 0.90
clone_modelFunction · 0.85
allFunction · 0.85
_set_inputsMethod · 0.80
placeholderMethod · 0.45
from_configMethod · 0.45
get_configMethod · 0.45
compileMethod · 0.45

Tested by

no test coverage detected