B
    ӻdB|                 @   s  d 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 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#e	j$Z$ej%Z%ej&Z&ej'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$ Z0de+fd%d&Z1e!d'd7d(d)Z2d*d+ Z3d,d- Z4e!d.g d/d0d1 Z5e!d2g d/d8d5d6Z6dS )9z7Code for model cloning, plus model-related API entries.    )ops)backend)metrics)optimizer_v1)
functional)
sequential)training)training_v1)	AddMetric)Layer)Input)
InputLayer)model_config)save)generic_utils)version_utils)CustomObjectScope)
tf_logging)nest)keras_exportc             C   s   | S )N )layerr   r   P/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/keras/models.pyshare_weights4   s    r   c             C   s   | j |  S )N)	__class__from_config
get_config)r   r   r   r   _clone_layer8   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>@   s    z,_insert_ancillary_layers.<locals>.<listcomp>c                s     | jS )N)indexZmetric_name)r   )metrics_namesr   r   <lambda>B       z*_insert_ancillary_layers.<locals>.<lambda>)keyc             S   s   g | ]}t |ts|qS r   )r   r
   )r   r   r   r   r   r    D   s    )Zrelevant_nodesN)sortZ_insert_layerslist)modelancillary_layersr"   	new_nodesZmetric_layersr   )r"   r   _insert_ancillary_layers<   s
    r+   c                s&  t  }t|  }|jdd x |D ]}| | }x|D ]}|j}	|	|kr`||	}
|
||	< |
}	n||	 }	t|	trtq8t fddt	|j
D r8t fdd|j}t fdd|j}|	||}t	|d }||	j|jj  x.tt	|jt	|D ]\}}| |< qW q8W q&W |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   | ]}| kV  qd S )Nr   )r   tensor)
tensor_mapr   r   	<genexpr>n   s    z"_make_new_nodes.<locals>.<genexpr>c                s     | | S )N)get)t)r.   r   r   r#   p   r$   z!_make_new_nodes.<locals>.<lambda>c                s     | | S )N)r0   )r1   )r.   r   r   r#   r   r$   r   )setr'   keysr&   outbound_layerr   r   allr   flatteninput_tensorsZmap_structure	call_argsZcall_kwargsaddZ_inbound_nodes_keras_historyZ
node_indexzipoutput_tensors)Znodes_by_depthlayer_fn	layer_mapr.   r*   Z
depth_keysdepthnodesnoder   Z	new_layerargskwargsr<   Zfirst_output_tensorxyr   )r.   r   _make_new_nodesI   s8    


rF   Nc                s6  t  tstd t  tr(td  js6tdi }|dk	rt|}xXt|D ]L\}} j| }t	
|s|j}t|d| d}|jj}|||< qV|||< qVW t|stdt ||\}	}
tj|	|
d\}}}
 j}t|| jd	  fd
d|
 D }|r2t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.
  z8Expected `model` argument to be a `Model` instance, got zcExpected `model` argument to be a functional `Model` instance, got a `Sequential` instance instead:z`Expected `model` argument to be a functional `Model` instance, but got a subclass model instead.Ninput_wrapper_for_)r-   namez.Expected `layer_fn` argument to be a callable.)created_layers)rH   c                s   g | ]}| j kr|qS r   )layers)r   r   )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   Z_should_skip_first_nodeZinbound_nodes)r   r   r   r   r   r       s   )r   Model
ValueError
Sequential_is_graph_networkr   r6   	enumerate_input_layersr   is_keras_tensorrH   r   r:   r   callable_clone_layers_and_model_configr   Zreconstruct_from_configr"   valuesr+   )r(   r7   r=   Znew_input_layersiinput_tensorZoriginal_input_layerrH   Znewly_created_input_layerZmodel_configsrI   r<   r"   r)   r*   r   )r(   r   _clone_functional_model   sF    





rX   c                s,   i   fdd}t j|d}| fS )ad  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                sJ   | kr|   | j < n.| jkr8tf |   | j < n|  | j < i S )N)rH   rQ   r   r   )r   )rI   input_layersr=   r(   r   r   _copy_layer   s    
z3_clone_layers_and_model_config.<locals>._copy_layer)Zserialize_layer_fn)r   Zget_network_config)r(   rY   r=   rZ   configr   )rI   rY   r=   r(   r   rT      s
    
rT   c                sz   g  | j s| fS dd | j D }|jdd x0|D ](}x"| j| D ]} ||j  qHW q8W  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   r?   r   r   r   r      s    z,_remove_ancillary_layers.<locals>.<listcomp>T)r,   c                s   g | ]}| kr|qS r   r   )r   l)r)   r   r   r      s    )rO   _nodes_by_depthr3   r&   appendr4   )r(   r>   rJ   Zdepthsr?   rA   r   )r)   r   _remove_ancillary_layers   s    
r_   c             C   s  t | tstd| t|s$tdg }i }xV| jdddD ]D}t |trT|dk	rTq<t |trft|n||}|| |||< q<W t| ||\}}|dk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}|sd|S i }x|j D ]r\}}| j| }x\t||D ]N\}}t |jtrx4t|jD ]\}}|||j| < qW n|j||j< qW qtW tdd | j D |||}t||| j| |S )aE  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.
  zGExpected `model` argument to be a `Sequential` model instance, but got:z.Expected `layer_fn` argument to be a callable.F)include_self	recursiveN)rJ   rH   rK   zXTo clone a `Sequential` model, we expect  at most one tensor as part of `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.rG   )r-   rH   c             S   s   i | ]\}}|d k r||qS )r   r   )r   r?   r@   r   r   r   
<dictcomp>v  s   z+_clone_sequential_model.<locals>.<dictcomp>)r   rN   rM   rS   _flatten_layersr   r   r^   r_   rH   lenr   Zto_listtupler'   r   rR   r:   r   r   strr]   itemsr;   r<   rP   rF   r+   r"   )r(   r7   r=   rJ   r>   r   Zcloned_layerr)   Zcloned_modelrD   Zorigin_layerrW   Zinput_layerr.   r?   Zcloned_nodesr@   Zcloned_noderA   jZoutput_tensorr*   r   r   r   _clone_sequential_model  s^    







ri   zkeras.models.clone_modelc          	   C   sJ   t  8 |dkrt}t| tr.t| ||dS t| ||dS W dQ R X 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)r7   r=   )r   ZDisableSharedObjectScoper   r   rN   ri   rX   )r(   r7   Zclone_functionr   r   r   clone_model  s    <

rj   c             C   s  | j r
tt| jtjtjt	  i }xt
| D ]}|dks2|dkrHq2yt| |}W n tttfk
rr   w2Y nX t|tr|||< || jkstt|dr|jrtd| q2t|ttfr2|dkr2|r2tdd |D r2td| q2W d	d
 | D }t| jddd}| j}d| _g | _xd|D ]\}| }t|tjrX|j sXtd| |j|}	|| }t| ||	 | j|	 q*W t| dr| jdkr| jrddddddddddddg}
x|
D ]}t| |||< qW || _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.
  Z
submodules_self_tracked_trackablesrJ   zgWe do not support the use of nested layers in `model_to_estimator` at this time. Found nested layer: %s)rJ   Z_layersr   Z_compile_metric_functionsZ_output_loss_metricsc             s   s   | ]}t |tV  qd S )N)r   r   )r   valr   r   r   r/     s    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: %sc             S   s   i | ]\}}||qS r   r   )r   r%   valuer   r   r   rb      s    z4_in_place_subclassed_model_reset.<locals>.<dictcomp>F)r`   ra   zrWe do not support the use of nested subclassed models in `model_to_estimator` at this time. Found nested model: %s_original_attributes_cacheNinputsoutputsZ
total_loss	optimizerZtrain_functionZtest_functionZpredict_functionZ_training_endpointsZ_collected_trainable_weightsZ_feed_inputsZ_feed_input_namesZ_feed_input_shapes)!rO   AssertionErrorr   Z
swap_classr   r   rL   r	   r   #executing_eagerly_outside_functionsdirgetattrAttributeErrorrM   	TypeErrorr   r   rJ   hasattrr'   re   r5   rg   rc   _setattr_trackingrk   r   r   setattrr^   rn   built_reset_build_compile_trackers)r(   Zattributes_cacherH   rm   Zlayers_to_namesZoriginal_layerssetattr_trackingr   r[   Zfresh_layerZattributes_to_cacher   r   r    _in_place_subclassed_model_reset  sr    





r~   c             C   s0   d| _ d| _d| _d| _t 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{   ro   rp   Z_is_compiledr   rs   Z_v1_compile_was_calledrq   )r(   r   r   r   r|   -  s    r|   zEkeras.__internal__.models.in_place_subclassed_model_state_restoration)Zv1c             C   s   | j r
tt| drx| jdk	rx| j}d| _g | _x8| j D ]*\}}t| || t|t	r<| j
| q<W d| _|| _nt|  dS )ab  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.
  rn   NF)rO   rr   rx   rn   ry   rk   rg   rz   r   r   r^   r|   )r(   r}   rH   rm   r   r   r   +in_place_subclassed_model_state_restorationE  s    



r   z/keras.__internal__.models.clone_and_build_modelTFc          	      sT  | j }|r|std|r0|    fdd| _t|p8i  | jrRt| |d}	nt| trt| |d}	|	js| jdk	rt	
 r|	| j n|	tj| j| jd jd ny| j|  }	W n6 tk
r   td |std	| }	t|	 Y nX |dk	r4t|ttfr*t|d
kr*|d }|	| W dQ R X |rPt|tjrlt|j |}
t|
 nt|ttfs|g}|dkrdd |D }
n4t|tr|d j|g}
ndd t||D }
|dk	rx|
D ]}||_ qW t|
d
kr|
d }
|
 d< |dk	r | d< t!" d  d< t!" d  d< |	j#f   |	S )a  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.
  zfError when cloning model: compile_clone was set to True, but the original model has not been compiled.c                  s    S )Nr   r   )compile_argsr   r   r#     r$   z'clone_and_build_model.<locals>.<lambda>)r7   Nr   )dtypezvThis model is a subclassed model. Please implement `get_config` and `from_config` to better support cloning the model.aJ  This model 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)`.rK   c             S   s   g | ]}|j | qS r   )r   r   r   )r   optr   r   r   r      s   z)clone_and_build_model.<locals>.<listcomp>c             S   s   g | ]\}}|j |qS r   )r   r   )r   r   Z
opt_configr   r   r   r      s   rq   target_tensorsr   Zweighted_metrics)$rq   rM   Z_get_compile_argsr   rO   rj   r   rN   Z_build_input_shaper   rs   buildZ_set_inputsr   placeholderro   r   r   r   r   NotImplementedErrorloggingwarningr~   r'   re   rd   r   ZTFOptimizerZtrack_tf_optimizerdictr;   Z
iterationsmetrics_moduleZclone_metricscompile)r(   r7   r   Zcustom_objectsZcompile_cloneZin_place_resetZoptimizer_iterationsZoptimizer_configZorig_optimizerclonerq   r   r   )r   r   clone_and_build_modelh  sv    /








r   )NN)NNNTFNN)7__doc__Ztensorflow.python.frameworkr   Ztensorflow.python.kerasr   r   r   r   Ztensorflow.python.keras.enginer   r   r   r	   Z)tensorflow.python.keras.engine.base_layerr
   r   Z*tensorflow.python.keras.engine.input_layerr   r   Ztensorflow.python.keras.savingr   r   Ztensorflow.python.keras.utilsr   r   Z+tensorflow.python.keras.utils.generic_utilsr   Ztensorflow.python.platformr   r   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   rL   rN   Z
FunctionalZ
save_modelZ
load_modelZmodel_from_configZmodel_from_yamlZmodel_from_jsonr   r   r+   rF   rX   rT   r_   ri   rj   r~   r|   r   r   r   r   r   r   <module>   s^   8X!iHd!
  