a
    Sic                     @   s~  d Z ddlm  mZ ddlmZ ddlmZ ddl	m
Z
 ddl	mZ ddl	mZ ddl	mZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ej Z ej!Z!dd Z"dd Z#dd Z$dd Z%de#fddZ&dd Z'dd  Z(de#fd!d"Z)ed#d3d$d%Z*d&d' Z+d(d) Z,ed*g d+d,d- Z-ed.g d+d4d1d2Z.dS )5z7Code for model cloning, plus model-related API entries.    N)backend)metrics)
functional)
sequential)training)training_v1)	AddMetric)Layer)Input)
InputLayer)optimizer_v1)generic_utils)version_utils)CustomObjectScope)
tf_logging)keras_exportc                 C   s   | S N layerr   r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/models/cloning.pyshare_weights-   s    r   c                 C   s   | j |  S r   	__class__from_config
get_configr   r   r   r   _clone_layer1   s    r   c                    sJ   dd |D }|j  fddd dd |D | }| j|t|d dS )	z>Inserts ancillary layers into the model with the proper order.c                 S   s   g | ]}t |tr|qS r   
isinstancer   .0r   r   r   r   
<listcomp>8   s   z,_insert_ancillary_layers.<locals>.<listcomp>c                    s     | jS r   )indexmetric_namer   metrics_namesr   r   <lambda>;       z*_insert_ancillary_layers.<locals>.<lambda>)keyc                 S   s   g | ]}t |ts|qS r   r   r   r   r   r   r!   <   s   )relevant_nodesN)sort_insert_layerslist)modelancillary_layersr%   	new_nodesmetric_layersr   r$   r   _insert_ancillary_layers5   s    r1   c                    s*  t  }t|  }|jdd |D ] }| | }|D ]}|j}	|	|vr\||	}
|
||	< |
}	n||	 }	t|	trpq4t fddtj	
|jD r4tj	 fdd|j}tj	 fdd|j}|	|i |}tj	
|d }||	j|jj  ttj	
|jtj	
|D ]\}}| |< qq4q"|S )	a  Uses the layers in `layer_map` to make new nodes based on `nodes_by_depth`.

    Args:
      nodes_by_depth: Provides structure information to create new nodes.
      layer_fn: Function to clone layers.
      layer_map: Map from layers in `model` to new layers.
      tensor_map: Map from tensors in `model` to newly compute tensors.

    Returns:
      A set of new nodes. `layer_map` and `tensor_map` are updated.
    Treversec                 3   s   | ]}| v V  qd S r   r   )r    tensor
tensor_mapr   r   	<genexpr>f   s   z"_make_new_nodes.<locals>.<genexpr>c                    s     | | S r   gettr5   r   r   r&   l   r'   z!_make_new_nodes.<locals>.<lambda>c                    s     | | S r   r8   r:   r5   r   r   r&   o   r'   r   )setr,   keysr*   outbound_layerr   r   alltfnestflatteninput_tensorsmap_structure	call_argscall_kwargsadd_inbound_nodes_keras_history
node_indexzipoutput_tensors)nodes_by_depthlayer_fn	layer_mapr6   r/   
depth_keysdepthnodesnoder   	new_layerargskwargsrL   first_output_tensorxyr   r5   r   _make_new_nodesB   sH    


rZ   c                    sN  t  tstd  t  tr0td   jsDtd  i }|durtj|}t|D ]P\}} j	| }t
|s|j}t|d| d}|jj}|||< qd|jj||< qdt|std| t ||\}	}
tj|	|
d\}}}
 j}t|| jd	  fd
d|
 D }|rJtjdd |
 D }t |||  S )a  Clone a functional `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.

    Input layers are always cloned.

    Args:
        model: Instance of `Model`.
        input_tensors: optional list of input tensors
            to build the model upon. If not provided,
            placeholders will be created.
        layer_fn: callable to be applied on non-input layers in the model. By
            default it clones the layer. Another example is to preserve the
            layer to share the weights. This is required when we create a
            per-replica copy of the model with distribution strategy; we want
            the weights to be shared but still feed inputs separately so we
            create new input layers.

    Returns:
        An instance of `Model` reproducing the behavior
        of the original model, on top of new inputs tensors,
        using newly instantiated weights.

    Raises:
        ValueError: in case of invalid `model` argument value or `layer_fn`
        argument value.
    zDExpected `model` argument to be a `Model` instance. Received: model=zdExpected `model` argument to be a functional `Model` instance, got a `Sequential` instance instead: zcExpected `model` argument to be a functional `Model` instance, but got a subclassed model instead: Ninput_wrapper_for_r4   nameBExpected `layer_fn` argument to be a callable. Received: layer_fn=)created_layers)r]   c                    s   g | ]}| j vr|qS r   )layersr   r-   r   r   r!      s   z+_clone_functional_model.<locals>.<listcomp>c                 S   s*   g | ]"}t |r |jd d n|jqS )   N)r   _should_skip_first_nodeinbound_nodesr   r   r   r   r!      s   )r   Model
ValueError
Sequential_is_graph_networkr@   rA   rB   	enumerate_input_layersr   is_keras_tensorr]   r
   rI   r   callable_clone_layers_and_model_configr   reconstruct_from_configr%   valuesr1   )r-   rC   rN   new_input_layersiinput_tensororiginal_input_layerr]   newly_created_input_layermodel_configsr_   rL   r%   r.   r/   r   ra   r   _clone_functional_model   s    




rv   c                    s,   i   fdd}t j|d}| fS )a  Clones all layers, and returns the model config without serializing layers.

    This function ensures that only the node graph is retrieved when getting the
    model config. The `layer_fn` used to clone layers might not rely on
    `layer.get_config()`, so some custom layers do not define `get_config`.
    Trying to retrieve the config results in errors.

    Args:
      model: A Functional model.
      input_layers: Dictionary mapping input layers in `model` to new input
        layers.
      layer_fn: Function used to clone all non-input layers.

    Returns:
      Model config object, and a dictionary of newly created layers.
    c                    sN   | v r|   | j < n2| jv r<tf i |   | j < n|  | j < i S r   )r]   rj   r   r   r   r_   input_layersrN   r-   r   r   _copy_layer  s    
z3_clone_layers_and_model_config.<locals>._copy_layer)serialize_layer_fn)r   get_network_config)r-   rx   rN   ry   configr   rw   r   rm      s    rm   c                    sr   g  | j s| fS dd | j D }|jdd |D ]$}| j| D ]} ||j  qDq6 fdd|D  fS )a  Removes and returns any ancillary layers from `layers` based on `model`.

    Ancillary layers are part of the model topology but not used to compute the
    model outputs, e.g., layers from `add_loss` and `add_metric`.

    Args:
      model: A Keras Model.
      layer_map: A map to from layers in the `model` to those in `layers`.
      layers: A list of all layers.

    Returns:
      Two lists of layers: (1) `layers` with the ancillary layers removed, and
      (2) the ancillary layers.
    c                 S   s   g | ]}|d k r|qS r   r   )r    rQ   r   r   r   r!   +  r'   z,_remove_ancillary_layers.<locals>.<listcomp>Tr2   c                    s   g | ]}| vr|qS r   r   )r    lr.   r   r   r!   1  r'   )rh   _nodes_by_depthr=   r*   appendr>   )r-   rO   r`   depthsrQ   rS   r   r   r   _remove_ancillary_layers  s    r   c                 C   s"  t | tstd|  t|s.td| g }i }| jdddD ]D}t |tr\|dur\qDt |trnt|n||}|| |||< qDt| ||\}}|du rt|| j	d}nt
t|dkrtd| nt |trt|}t|d	 }	t|	r:|	jj}
t |
tr*t|
g| | j	d}ntd
| n2t|	dt|	j	 d}|jj}t|g| | j	d}|sv|S i }|j D ]j\}}| j| }t||D ]J\}}t |jtrt|jD ]\}}|||j| < qn|j||j< qqtdd | j D |||}t||| j| |S )aq  Clone a `Sequential` 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.

    Args:
        model: Instance of `Sequential`.
        input_tensors: optional list of input tensors
            to build the model upon. If not provided,
            placeholders will be created.
        layer_fn: callable to be applied on non-input layers in the model. By
            default it clones the layer. Another example is to preserve the
            layer to share the weights. This is required when we create a
            per-replica copy of the model with distribution strategy; we want
            the weights to be shared but still feed inputs separately so we
            create new input layers.

    Returns:
        An instance of `Sequential` reproducing the behavior
        of the original model, on top of new inputs tensors,
        using newly instantiated weights.

    Raises:
        ValueError: in case of invalid `model` argument value or `layer_fn`
        argument value.
    zOExpected `model` argument to be a `Sequential` model instance. Received: model=r^   Finclude_self	recursiveN)r`   r]   rb   zpTo clone a `Sequential` model, we expect at most one tensor as part of `input_tensors`. Received: input_tensors=r   zCannot clone a `Sequential` model on top of a tensor that comes from a Keras layer other than an `InputLayer`. Use the Functional API instead. Received: input_tensors=r[   r\   c                 S   s   i | ]\}}|d k r||qS r}   r   )r    rQ   rR   r   r   r   
<dictcomp>  s   z+_clone_sequential_model.<locals>.<dictcomp>)r   rg   rf   rl   _flatten_layersr   r   r   r   r]   lenr   to_listtupler,   r   rk   rI   r   r
   strr   itemsrK   rL   ri   rZ   r1   r%   )r-   rC   rN   r`   rO   r   cloned_layerr.   cloned_modelrX   origin_layerrr   input_layerr6   rQ   cloned_nodesrR   cloned_noderS   joutput_tensorr/   r   r   r   _clone_sequential_model4  s    







r   zkeras.models.clone_modelc                 C   sz   t  ^ |du rt}t| tr<t| ||dW  d   S t| ||dW  d   S W d   n1 sl0    Y  dS )a	  Clone a Functional or Sequential `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.

    Note that
    `clone_model` will not preserve the uniqueness of shared objects within the
    model (e.g. a single variable attached to two distinct layers will be
    restored as two separate variables).

    Args:
        model: Instance of `Model`
            (could be a Functional model or a Sequential model).
        input_tensors: optional list of input tensors or InputLayer objects
            to build the model upon. If not provided,
            new `Input` objects will be created.
        clone_function: Callable to be used to clone each layer in the target
            model (except `InputLayer` instances). It takes as argument the
            layer instance to be cloned, and returns the corresponding layer
            instance to be used in the model copy. If unspecified, this callable
            defaults to the following serialization/deserialization function:
            `lambda layer: layer.__class__.from_config(layer.get_config())`.
            By passing a custom callable, you can customize your copy of the
            model, e.g. by wrapping certain layers of interest (you might want
            to replace all `LSTM` instances with equivalent
            `Bidirectional(LSTM(...))` instances, for example).

    Returns:
      An instance of `Model` reproducing the behavior
      of the original model, on top of new inputs tensors,
      using newly instantiated weights. The cloned model may behave
      differently from the original model if a custom `clone_function`
      modifies the layer.

    Example:

    ```python
    # Create a test Sequential model.
    model = keras.Sequential([
        keras.Input(shape=(728,)),
        keras.layers.Dense(32, activation='relu'),
        keras.layers.Dense(1, activation='sigmoid'),
    ])
    # Create a copy of the test model (with freshly initialized weights).
    new_model = clone_model(model)
    ```

    Note that subclassed models cannot be cloned, since their internal
    layer structure is not known. To achieve equivalent functionality
    as `clone_model` in the case of a subclassed model, simply make sure
    that the model class implements `get_config()`
    (and optionally `from_config()`), and call:

    ```python
    new_model = model.__class__.from_config(model.get_config())
    ```
    N)rC   rN   )r   DisableSharedObjectScoper   r   rg   r   rv   )r-   rC   clone_functionr   r   r   clone_model  s    <

r   c                 C   s  | j r
J t| jtjtjtjj	
  i }t| D ]}|dks4|dkrJq4zt| |}W n tttfyt   Y q4Y n0 t|tr|||< || jv sJ t|dr|jrtd| q4t|ttfr4|dvr4|r4tdd |D r4td| q4d	d
 | D }t| jddd}| j}d| _g | _|D ]^}| }t|tjr\|j s\td| |j|}	|| }t| ||	 | j|	 q,t| dr| jdu r| j rg d}
|
D ]}t| |||< q|| _t!|  || _dS )a   Substitute for model cloning that works for subclassed models.

    Subclassed models cannot be cloned because their topology is not
    serializable. To "instantiate" an identical model in a new TF graph, we
    reuse the original model object, but we clear its state.

    After calling this function on a model instance, you can use the model
    instance as if it were a model clone (in particular you can use it in a new
    graph).

    This method clears the state of the input model. It is thus destructive.
    However the original state can be restored fully by calling
    `_in_place_subclassed_model_state_restoration`.

    Args:
      model: Instance of a Keras model created via subclassing.

    Raises:
      ValueError: In case the model uses a subclassed model as inner layer.
    
submodules_self_tracked_trackablesr`   zeWe do not support the use of nested layers in `model_to_estimator` at this time. Found nested layer: )r`   _layersr   _compile_metric_functions_output_loss_metricsc                 s   s   | ]}t |tV  qd S r   )r   r	   )r    valr   r   r   r7   ;  r'   z3_in_place_subclassed_model_reset.<locals>.<genexpr>zWe do not support the use of list-of-layers attributes in subclassed models used with `model_to_estimator` at this time. Found list model: c                 S   s   i | ]\}}||qS r   r   )r    r(   valuer   r   r   r   D  r'   z4_in_place_subclassed_model_reset.<locals>.<dictcomp>Fr   zpWe do not support the use of nested subclassed models in `model_to_estimator` at this time. Found nested model: _original_attributes_cacheN)inputsoutputs
total_loss	optimizertrain_functiontest_functionpredict_function_training_endpoints_collected_trainable_weights_feed_inputs_feed_input_names_feed_input_shapes)"rh   r   
swap_classr   r   re   r   r@   compatv1#executing_eagerly_outside_functionsdirgetattrAttributeErrorrf   	TypeErrorr   r	   r`   hasattrr,   r   r?   r   r   _setattr_trackingr   r   r   setattrr   r   built_reset_build_compile_trackers)r-   attributes_cacher]   r   layers_to_namesoriginal_layerssetattr_trackingr   r|   fresh_layerattributes_to_cacher   r   r    _in_place_subclassed_model_reset  s|    


	r   c                 C   s4   d| _ d| _d| _d| _tjj s*d| _d| _	dS )a  Reset state trackers for model.

    Note that we do not actually zero out attributes such as optimizer,
    but instead rely on the expectation that all of the attrs will be
    over-written on calling build/compile/etc. This is somewhat fragile,
    insofar as we check elsewhere for the presence of these attributes as
    evidence of having been built/compiled/etc. Pending a better way to do this,
    we reset key attributes here to allow building and compiling.

    Args:
      model: the model that is being reset
    FN)
r   r   r   _is_compiledr@   r   r   r   _v1_compile_was_calledr   ra   r   r   r   r   v  s    r   zEkeras.__internal__.models.in_place_subclassed_model_state_restoration)r   c                 C   s   | j r
J t| drt| jdurt| j}d| _g | _| j D ]*\}}t| || t|tr:| j	| q:d| _|| _nt
|  dS )an  Restores the original state of a model after it was "reset".

    This undoes this action of `_in_place_subclassed_model_reset`, which is
    called in `clone_and_build_model` if `in_place_reset` is set to True.

    Args:
      model: Instance of a Keras model created via subclassing, on which
        `_in_place_subclassed_model_reset` was previously called.
    r   NF)rh   r   r   r   r   r   r   r   r	   r   r   )r-   r   r]   r   r   r   r   +in_place_subclassed_model_state_restoration  s    

r   z/keras.__internal__.models.clone_and_build_modelTFc              	      s~  | j }|r|std|  |r6|    fdd| _t|p>i  | jrZt| |d}	nt| trt| |d}	|	js| jdurt	j
j r|	| j n|	tj| j| jd jd nz| j|  }	W n> ty   td |std	|  d
| }	t|	 Y n0 |durHt|ttfr>t|dkr>|d }|	| W d   n1 s^0    Y  |rzt|tjrt|j |}
t|
 nt|ttfs|g}|du rdd |D }
n4t|t r|d j|g}
ndd t!||D }
|dur|
D ]}||_"qt|
dkr,|
d }
|
 d< |durF| d< t#$ d  d< t#$ d  d< |	j%f i   |	S )aG  Clone a `Model` and build/compile it with the same settings used before.

    This function can 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 work
    with TPU DistributionStrategy. Try at your own risk.

    Args:
      model: `tf.keras.Model` object. Can be Functional, Sequential, or
        sub-classed.
      input_tensors: Optional list or dictionary of input tensors to build the
        model upon. If not provided, placeholders will be created.
      target_tensors: Optional list of target tensors for compiling the model.
        If not provided, placeholders will be created.
      custom_objects: Optional dictionary mapping string names to custom classes
        or functions.
      compile_clone: Boolean, whether to compile model clone (default `True`).
      in_place_reset: Boolean, whether to reset the model in place. Only used if
        the model is a subclassed model. In the case of a subclassed model,
        this argument must be set to `True` (default `False`). To restore the
        original model, use the function
        `in_place_subclassed_model_state_restoration(model)`.
      optimizer_iterations: An iterations variable that will be incremented by
        the optimizer if the clone is compiled. This argument is used when a
        Keras model is cloned into an Estimator model function, because
        Estimators create their own global step variable.
      optimizer_config: Optimizer config dictionary or list of dictionary
        returned from `get_config()`. This argument should be defined if
        `clone_and_build_model` is called in a different graph or session from
        the original model, and the optimizer is an instance of `OptimizerV2`.

    Returns:
      Clone of the model.

    Raises:
      ValueError: Cloning fails in the following cases
        - cloning a subclassed model with `in_place_reset` set to False.
        - compiling the clone when the original model has not been compiled.
    zyError when cloning model: `compile_clone` was set to True, but the original model has not been compiled. Received: model=c                      s    S r   r   r   compile_argsr   r   r&     r'   z'clone_and_build_model.<locals>.<lambda>)rC   Nr   )dtypezvThis model is a subclassed model. Please implement `get_config` and `from_config` to better support cloning the model.zThis model (aA  ) is a subclassed model. Such a model cannot be cloned, but there is a workaround where the model is reset in-place. To use this, please set the argument `in_place_reset` to `True`. This will reset the attributes in the original model. To restore the attributes, call `in_place_subclassed_model_state_restoration(model)`.rb   c                 S   s   g | ]}|j | qS r   r   )r    optr   r   r   r!   1  s   z)clone_and_build_model.<locals>.<listcomp>c                 S   s   g | ]\}}|j |qS r   )r   r   )r    r   
opt_configr   r   r   r!   <  s   r   target_tensorsr   weighted_metrics)&r   rf   _get_compile_argsr   rh   r   r   rg   _build_input_shaper@   r   r   r   build_set_inputsr   placeholderr   r   r   r   r   NotImplementedErrorloggingwarningr   r,   r   r   r   TFOptimizertrack_tf_optimizerdictrK   
iterationsmetrics_moduleclone_metricscompile)r-   rC   r   custom_objectscompile_clonein_place_resetoptimizer_iterationsoptimizer_configorig_optimizercloner   r   r   r   r   clone_and_build_model  s    5





*



r   )NN)NNNTFNN)/__doc__Ztensorflow.compat.v2r   v2r@   kerasr   r   r   keras.enginer   r   r   r   Zkeras.engine.base_layerr   r	   keras.engine.input_layerr
   r   Zkeras.optimizersr   keras.utilsr   r   keras.utils.generic_utilsr   tensorflow.python.platformr   r    tensorflow.python.util.tf_exportr   re   rg   r   r   r1   rZ   rv   rm   r   r   r   r   r   r   r   r   r   r   r   <module>   s\   Ap$ Jw
"
       