
    &Vf,                     F    d Z ddlmZ ddlmZ d Zd Zd Zd Zd Z	d	 Z
d
S )aJ  Utilities for handling Keras model in graph plugin.

Two canonical types of Keras model are Functional and Sequential.
A model can be serialized as JSON and deserialized to reconstruct a model.
This utility helps with dealing with the serialized Keras model.

They have distinct structures to the configurations in shapes below:
Functional:
  config
    name: Name of the model. If not specified, it is 'model' with
          an optional suffix if there are more than one instance.
    input_layers: Keras.layers.Inputs in the model.
    output_layers: Layer names that are outputs of the model.
    layers: list of layer configurations.
      layer: [*]
        inbound_nodes: inputs to this layer.

Sequential:
  config
    name: Name of the model. If not specified, it is 'sequential' with
          an optional suffix if there are more than one instance.
    layers: list of layer configurations.
      layer: [*]

[*]: Note that a model can be a layer.
Please refer to https://github.com/tensorflow/tfjs-layers/blob/master/src/keras_format/model_serialization.ts
for more complete definition.
    )GraphDef)dtypesc              #   \  K   d| fV  |                      d                               d          ry|                      d                               d          }|                      d                               d          D ]*}t          |          D ]\  }}|r|d|n|}||fV  )dS dS )aY  Walks the nested keras layer configuration in preorder.

    Args:
      keras_layer: Keras configuration from model.to_json.

    Yields:
      A tuple of (name_scope, layer_config).
      name_scope: a string representing a scope name, similar to that of tf.name_scope.
      layer_config: a dict representing a Keras layer configuration.
     configlayersname/N)get_walk_layers)keras_layer
name_scopelayersub_name_scopesublayers        a/var/www/html/software/conda/lib/python3.11/site-packages/tensorboard/plugins/graph/keras_util.pyr   r   0   s       {
x  $$X.. 	1 __X..226::
 __X..228<< 	1 	1E.:5.A.A 1 1* &$zzz>>::# 
 &x000001	1 	1	1 	1    c                     | r| d|S |S )a/  Returns scoped name for a node as a string in the form '<scope>/<node
    name>'.

    Args:
      name_scope: a string representing a scope name, similar to that of tf.name_scope.
      node_name: a string representing the current node name.

    Returns
      A string representing a scoped name.
    r
    )r   	node_names     r   _scoped_namer   H   s#      1$**ii00r   c                 V    |                      d                               d          duS )zReturns True if layer is a model.

    Args:
      layer: a dict representing a Keras model configuration.

    Returns:
      bool: True if layer is a model.
    r   r   N)r   )r   s    r   	_is_modelr   X   s)     99X""8,,D88r   c                 D    t          | d         t          f          r| n| gS )a  Normalizes to a list of layers.

    Args:
      maybe_layers: A list of data[1] or a list of list of data.

    Returns:
      List of list of data.

    [1]: A Functional model has fields 'inbound_nodes' and 'output_layers' which can
    look like below:
    - ['in_layer_name', 0, 0]
    - [['in_layer_is_model', 1, 0], ['in_layer_is_model', 1, 1]]
    The data inside the list seems to describe [name, size, index].
    r   )
isinstancelist)maybe_layerss    r   _norm_to_list_of_layersr   d   s'      #<?TG<<P<.r   c                    |                     d          }|                     d          st          d          t          | |                     d                    |                     d          }|                     d          }|                     d          }t          |o|          }	t          |          }
|
rD|	rBt	          ||          D ]0\  }}t          |          }t          | |d                   }|||<   1nw|
r%|	s#t          | |d         d         d                   }nP|
sN|rL|	rJt          |          d	k    sJ d
t          |          z              |d         }t          |          }|||<   |	r%|r#t          |          }fd|D             }||<   nY|                     d          d         }|                     d                               d          }t          |          }|g|<   |||fS )a  Updates input_to_in_layer, model_name_to_output, and prev_node_name
    based on the model_layer.

    Args:
      name_scope: a string representing a scope name, similar to that of tf.name_scope.
      model_layer: a dict representing a Keras model configuration.
      input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
      model_name_to_output: a dict mapping Keras Model name to output layer of the model.
      prev_node_name: a string representing a previous, in sequential model layout,
                      node name.

    Returns:
      A tuple of (input_to_in_layer, model_name_to_output, prev_node_name).
      input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
      model_name_to_output: a dict mapping Keras Model name to output layer of the model.
      prev_node_name: a string representing a previous, in sequential model layout,
                      node name.
    r   r   zlayer is not a model.r	   input_layersoutput_layersinbound_nodesr      zhCannot have multi-input Functional model when parent model is not Functional. Number of input layers: %dc                 <    g | ]}t          |d                    S )r   )r   ).0r   r   s     r   
<listcomp>z!_update_dicts.<locals>.<listcomp>   s'    MMMU|IuQx88MMMr   )r   
ValueErrorr   boolziplenr   )r   model_layerinput_to_in_layermodel_name_to_outputprev_node_namelayer_configr    r!   r"   is_functional_modelis_parent_functional_modelinput_layerinbound_nodeinput_layer_nameinbound_node_namer   layer_names
last_layerlast_layer_nameoutput_noder   s                       @r   _update_dictsr;   x   sm   2 ??8,,LH%% 20111Z)9)9&)A)ABBI##N33L $$_55MOOO44M|=>> "&m!4!4! =&9 =+.|]+K+K 	D 	D'[,+I{CC ,Za I I2C.//	D 
$ =,? = &j-2B12Ea2HII&==  =
 <  A%%%<>A+>N>NO &%% #1o'	;??.<*+ 8} 8(77MMMMfMMM*5Y''!%%h//3
$..2266v>>"9o>>+6-Y'3^DDr   c                    i }i }t                      }d}t          |           D ]=\  }}t          |          rt          |||||          \  }}}-|                    d          }t          ||                    d                    }|j                                        }	||	_        |                    d          :|                    d          	                    d          }
|
|	j
        d         _        |                    d          }|St          |t                    s>t          j        |                    d                    }|j        |	j
        d         _        |                    d          |                    d          D ]}t%          |          }|D ]k\  }}}}t          ||          }|                    ||g          }|t'          |          k     r||         n|d	         }|	j                            |           ln||	j                            |           ||v r-|	j                            |                    |                     |	j        }?|S )
a  Returns a GraphDef representation of the Keras model in a dict form.

    Note that it only supports models that implemented to_json().

    Args:
      keras_layer: A dict from Keras model.to_json().

    Returns:
      A GraphDef representation of the layers in the model.
    Nr   r	   
class_nameasciikeras_classdtyper"   r'   )r   r   r   r;   r   r   nodeaddr	   encodeattrsr   dictr   as_dtypeas_datatype_enumtyper   r+   inputappend)r   input_to_layerr.   gr/   r   r   r0   r   node_defkeras_cls_namedtype_or_policytf_dtypemaybe_inbound_noder"   r	   sizeindex_inbound_nameinbound_node_names
input_names                         r   keras_model_to_graph_defrY      s    N

A N+K88 D' D'UU 	
 $ 	$ yy** \-=-=f-E-EFF	6::<<!99\""."YY|44;;GDDN-;HM-(*&**733 &zT0
 0
& |'7'7'@'@AAH*2*CHM'"'99_%%1&+ii&@&@ 6 6" 78J K K.; 6 6*T4#/
D#A#AL
 *>)A)A$|n* *& !3'9#:#::: +511/3 
 N))*55551666 'N!!.111&&N!!."4"4Y"?"?@@@!Hr   N)__doc__"tensorboard.compat.proto.graph_pb2r   "tensorboard.compat.tensorflow_stubr   r   r   r   r   r;   rY   r   r   r   <module>r]      s     8 8 7 7 7 7 7 5 5 5 5 5 51 1 10   	9 	9 	9  (FE FE FERY Y Y Y Yr   