
    ij                     f   d dl Z d dlZd dlZd dl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#  G d dee      Z$d"dZ%d Z&d Z'd Z(d#dZ)d Z*d Z+d Z,d Z-d  Z.d! Z/y)$    N)backend)ops)tree)global_state)Input)
InputLayer)	InputSpec)Layer)saving_utils)serialization)Model)Function
_build_map)make_node_key)KerasHistory)Node)	Operation)serialization_lib)trackingc                   2    e Zd ZdZ fdZddZd Zd Zed        Z	e	j                  d        Z	ddZd fd		Z fd
Zd Zed        Zed        Z fdZddZd Zd Zd Zed        Zed        Zd Zed        Zej                  d        Zd Z xZS )
Functionala?
  A `Functional` model is a `Model` defined as a directed graph of layers.

    Three types of `Model` exist: subclassed `Model`, `Functional` model,
    and `Sequential` (a special case of `Functional`).

    A `Functional` model can be instantiated by passing two arguments to
    `__init__()`. The first argument is the `keras.Input` objects
    that represent the inputs to the model.
    The second argument specifies the output tensors that represent
    the outputs of this model. Both arguments can be a nested structure
    of tensors.

    Example:

    ```
    inputs = {'x1': keras.Input(shape=(10,), name='x1'),
              'x2': keras.Input(shape=(1,), name='x2')}
    t = keras.layers.Dense(1, activation='relu')(inputs['x1'])
    outputs = keras.layers.Add()([t, inputs['x2']])
    model = keras.Model(inputs, outputs)
    ```

    A `Functional` model constructed using the Functional API can also
    include raw Keras 3 ops.

    Example:

    ```python
    inputs = keras.Input(shape=(10,))
    x = keras.layers.Dense(1)(inputs)
    outputs = ops.nn.relu(x)
    model = keras.Model(inputs, outputs)
    ```

    A new `Functional` model can also be created by using the
    intermediate tensors. This enables you to quickly extract sub-components
    of the model.

    Example:

    ```python
    inputs = keras.Input(shape=(None, None, 3))
    processed = keras.layers.RandomCrop(width=32, height=32)(inputs)
    conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed)
    pooling = keras.layers.GlobalAveragePooling2D()(conv)
    feature = keras.layers.Dense(10)(pooling)

    full_model = keras.Model(inputs, feature)
    backbone = keras.Model(processed, conv)
    activations = keras.Model(conv, feature)
    ```

    Note that the `backbone` and `activations` models are not
    created with `keras.Input` objects, but with the tensors
    that are originated from `keras.Input` objects.
    Under the hood, the layers and weights will
    be shared across these models, so that user can train the `full_model`, and
    use `backbone` or `activations` to do feature extraction.
    The inputs and outputs of the model can be nested structures of tensors as
    well, and the created models are standard `Functional` model that support
    all the existing API.

    Args:
        inputs: List of input tensors (must be created via `keras.Input()`
            or originated from `keras.Input()`).
        outputs: List of output tensors.
        name: String, optional. Name of the model.
        trainable: Boolean, optional. If the model's variables should be
            trainable.
    c                 J    t        j                  | t        |   |             S N)typingcastsuper__new__)clsargskwargs	__class__s      p/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/models/functional.pyr   zFunctional.__new__c   s    {{3 455    c                    t        |t              rr|j                         D ]_  \  }}t        |t        j                        s!||j
                  k7  s1t        j                  d| d| d|j
                   d| d| d       a |j                  dd       }t        j                  |      }t        j                  |      }	|D ]9  }
t        |
t        j                        rt        d| d	|
 d
t        |
              |	D ]9  }
t        |
t        j                        rt        d| d	|
 d
t        |
              t        d |D              st        ||      \  }}t        j                          5  t#        j$                  | |||       d d d        ||| _        | j(                  D ]  }| j*                  j-                  |        | j/                  d        d| _        d| _        | j4                  D 
cg c]  }
|
j6                  d    }}
|D 
cg c]  }
|
j
                   c}
| _        y # 1 sw Y   xY wc c}
w c c}
w )NzyWhen providing `inputs` as a dict, all keys in the dict must match the names of the corresponding tensors. Received key 'z' mapping to value z which has name 'z'. Change the tensor name to 'z' (via `Input(..., name='z')`)	trainablez;All `inputs` values must be KerasTensors. Received: inputs=z including invalid value z	 of type z=All `outputs` values must be KerasTensors. Received: outputs=c              3   2   K   | ]  }t        |        y wr   )is_input_keras_tensor).0ts     r#   	<genexpr>z&Functional.__init__.<locals>.<genexpr>   s     A(+A   nameFTr   )
isinstancedictitemsr   KerasTensorr.   warningswarnpopr   flatten
ValueErrortypeallclone_graph_nodesr   DotNotTrackScoper   __init__r&   layers_trackertrackbuild_convert_input_args!_allow_non_tensor_positional_argsoutputs_keras_historyoutput_names)selfinputsrC   r.   r!   kvr&   flat_inputsflat_outputsxlayeroutput_layerss                r#   r<   zFunctional.__init__f   s@   fd# 1a!4!45!qvv+MM2234Gs K++,66( 337s$	@ JJ{D1	ll6*||G, 	Aa!4!45 $X%>qc B G9& 	  	Aa!4!45 &i'@ D G9& 	 A[AA/@OFG&&( 	@dFG$?	@  &DN[[ 	'EMM&	'

4#( 15.6:llC))!,CC-:;QVV;	@ 	@ D;s   0H,4H8H=,H5c                      y r    rF   s    r#   _lock_statezFunctional._lock_state   s     	r$   c                      y)Nr   rP   rQ   s    r#   	_obj_typezFunctional._obj_type   s    r$   c                 p    g }| j                   D ]$  }t        |t              s|j                  |       & |S r   )_operationsr/   r
   append)rF   r=   	operations      r#   r=   zFunctional.layers   s9    )) 	)I)U+i(	) r$   c                     t        d      )NzU`Model.layers` attribute is reserved and should not be used. Please use another name.)AttributeError)rF   _s     r#   r=   zFunctional.layers   s    '
 	
r$   c                    | j                  |      }|d gt        |      z  }nBt        j                  |      }t	        ||      D ]  \  }}|	t        j                  ||         | j                  |fd      }t        |      S )Nc                      t        | fdiS )Ntrainingoperation_fn)opr!   r^   s    r#   <lambda>z!Functional.call.<locals>.<lambda>   s    L%%%)/% r$   r_   )	_standardize_inputslenr   r6   zipr   set_keras_mask_run_through_graphunpack_singleton)rF   rG   r^   maskr!   masksrL   rC   s     ` `   r#   callzFunctional.call   s    ))&1<FS[(ELL&Evu- 44#**1d34 )) * 
  ((r$   c                 "    t         |   |      S r   )r   compute_output_spec)rF   rG   r^   ri   r"   s       r#   rm   zFunctional.compute_output_spec   s    w*622r$   c                 "    t         |   |      S r   )r   compute_output_shape)rF   input_shaper"   s     r#   ro   zFunctional.compute_output_shape   s    w+K88r$   c                     d| _         y )NT)built)rF   rp   s     r#   r@   zFunctional.build   s	    
r$   c                     t        j                  d | j                        }t        |t              rt        |      dk(  r|d   S |S )Nc                     | j                   S r   shaperL   s    r#   rb   z(Functional.input_shape.<locals>.<lambda>   s
    AGG r$      r   )r   map_structurerG   r/   listrd   )rF   input_shapess     r#   rp   zFunctional.input_shape   s?    ))*;T[[IlD)c,.?1.D?"r$   c                     t        j                  d | j                        }t        |t              rt        |      dk(  r|d   S |S )Nc                     | j                   S r   ru   rw   s    r#   rb   z)Functional.output_shape.<locals>.<lambda>   s
    QWW r$   rx   r   )r   ry   rC   r/   rz   rd   )rF   output_shapess     r#   output_shapezFunctional.output_shape   s@    **+<dllKmT*s=/AQ/F ##r$   c                 .   t        j                  |d         }t        || j                        D ]T  \  }}|	|j                  j
                  }t        |t              s0|j                  r=t        d|j                   d       t        t        | 6  | S )Nr   The input 'F' is not optional, but None was passed. Please provide a valid tensor.)r   r6   re   _inputsrD   rX   r/   r   optionalr7   r.   r   r   _assert_input_compatibility)rF   r    rJ   rL   input_tensorinput_layerr"   s         r#   r   z&Functional._assert_input_compatibility   s    ll47+";= 	OA|y*99CC{J7'00$),*;*;)< == = 	 UD=tDDr$   c                 d   	 t        j                  t        j                  |      t        j                  | j                               y #  t        j                  d | j                        }t        j                  d |      }d| d| }|rt        |      t        j                  |       Y y xY w)Nc                 "    | dS | j                   S )NNoner-   rw   s    r#   rb   z?Functional._maybe_warn_inputs_struct_mismatch.<locals>.<lambda>   s    AI& 166 r$   c                 *    | dS d| j                    dS )Nr   zTensor(shape=)ru   rw   s    r#   rb   z?Functional._maybe_warn_inputs_struct_mismatch.<locals>.<lambda>   s    AI& ]177)13M r$   zJThe structure of `inputs` doesn't match the expected structure.
Expected: z
Received: inputs=)r   assert_same_structurelists_to_tuples_inputs_structry   r7   r3   r4   )rF   rG   raise_exceptionmodel_inputs_structinputs_structmsgs         r#   "_maybe_warn_inputs_struct_mismatchz-Functional._maybe_warn_inputs_struct_mismatch   s    	 &&$$V,$$T%8%89	"&"4"49### !..MM
))<(= >$$1?4 
  o%MM#s   AA	 	A$B/c                    g }t        || j                        D ]  \  }}|yd }t        |d      r"|j                  r|j                  j                  }t        |t              r'|j                  s|j                  }t        d| d      |j                  |       |j                  t        j                  ||j                  |j                                |S )NrD   r   r   )dtypesparse)re   r   hasattrrD   rX   r/   r   r   r.   r7   rW   r   convert_to_tensorr   r   )rF   rJ   	convertedrL   r   r   
input_names          r#   _convert_inputs_to_tensorsz%Functional._convert_inputs_to_tensors  s    	";= !	OA|y
 #L*:;$33"."="="G"GKk:6&//%1%6%6
("-j\ :A !A    #  ))*00+227!	D r$   c                    g }t        |      D ]X  \  }}| j                  |   j                  }||j                  |       4t	        |j                        }t	        |      }||k(  r|j                  |       k||dz   k(  r9|j                  d   dk(  r'|j                  t        j                  |d             ||dz
  k(  r/|d   dk(  r'|j                  t        j                  |d             t        j                  | j                        }dj                  d ||   d   D              }	t        d| d| j                  |   j                   d	|	 d
| d|j                   
       t        t	        |            D ]\  }t        ||   d      r||   j                   ||   _        t#        j$                  ||         }
|
Dt#        j&                  ||   |
       ^ |S )Nrx   )axis.c              3   2   K   | ]  }t        |        y wr   )str)r)   ps     r#   r+   z0Functional._adjust_input_rank.<locals>.<genexpr>@  s     HqCFHr,   r   zInvalid input shape for input z with name 'z' and path 'z'. Expected shape z#, but input has incompatible shape rD   )	enumerater   rv   rW   rd   r   squeezeexpand_dimsr   flatten_with_pathr   joinr7   r.   ranger   rD   r   get_keras_maskrf   )rF   rJ   adjustedirL   	ref_shapex_rankref_rankflat_paths_and_inputspathri   s              r#   _adjust_input_rankzFunctional._adjust_input_rank+  s   k* 	DAqQ--Iy"\F9~H!"A%772;!#OOCKK$;<A%R=A%OOCOOAB$?@$($:$:4;N;N$O!88H,A!,DQ,GHHD0 4LLO(()dV;M+@	K )	4 s;'( 	:A{1~'78-8^-J-J*))+a.9D&&x{D9	: r$   c                 t   d}t        | j                  t              r2t        | j                        dk(  rt	        j
                  |      r|g}n)t        |t              rt        | j                  t              st        | j                  d      rt        d | j                  D              rpt        d | j                  D              }t        |j                               }|j                  |      r'| j                  D cg c]  }||j                      }}n_d}n\t        | j                  t        j                        r6| j                  j                  |v r|| j                  j                     g}nd}nd}t        | j                  t              rYt        |t              sIt        | j                  j                               t        | j                  j                               k7  rd}| j!                  ||       t#        j$                  |      }| j'                  |      }| j)                  |      S c c}w )NFrx   __len__c              3   P   K   | ]  }t        |t        j                           y wr   r/   r   r2   r)   r   s     r#   r+   z1Functional._standardize_inputs.<locals>.<genexpr>\  s"      ?78
1g112?   $&c              3   4   K   | ]  }|j                     y wr   r-   r   s     r#   r+   z1Functional._standardize_inputs.<locals>.<genexpr>_  s     #HqAFF#Hs   T)r   )r/   r   rz   rd   r   	is_tensorr0   r   r9   setkeysissubsetr.   r   r2   sortedr   r   r6   r   r   )rF   rG   r   expected_keysr   r   rJ   s          r#   rc   zFunctional._standardize_inputsO  s   t**D1D''(A-f%XF%j/

 t**I63 ?<@<O<O? < !$#HD4G4G#H H6;;=) ))$/6:6I6IJfQVVnJFJ&*OD//1D1DE&&++v5$T%8%8%=%=>?F&*O"&t**D1vt,T((--/0d))..012 #O//O 	0 	
 ll6*55kB&&{33/ Ks    H5c                     | j                   S r   )r   rQ   s    r#   inputzFunctional.input{  s    
 """r$   c                     | j                   S r   )_outputs_structrQ   s    r#   outputzFunctional.output  s    ###r$   c                     t         r   )NotImplementedError)rF   losss     r#   add_losszFunctional.add_loss  s    !!r$   c                    t        | d      r| j                  S d dfd	}t        | j                  t              rst        d | j                  j                         D              rHt        | j                  j                               }|D cg c]  } || j                  |   |       c}S y | j                  D cg c]
  } ||       c}S c c}w c c}w )N_manual_input_specc                 <    t        |       } | rd | d<   t        |       S )Nr   )rz   tuplerw   s    r#   shape_with_no_batch_sizez7Functional.input_spec.<locals>.shape_with_no_batch_size  s     QA!8Or$   c                     d}t        | j                  d   t              r| j                  d   j                  rd}t	         | j
                        d|| j                  d   j                  |      S ||      S )NFr   T)rv   allow_last_axis_squeezer.   r   )r/   rD   r   r   r	   rv   r.   )rL   r.   r   r   s      r#   make_spec_for_tensorz3Functional.input_spec.<locals>.make_spec_for_tensor  s    H!**1-z:##A&//#H.qww7(,15Q%%a(--!	  DH!	 r$   c              3   P   K   | ]  }t        |t        j                           y wr   r   )r)   rL   s     r#   r+   z(Functional.input_spec.<locals>.<genexpr>  s%       1g112r   r-   r   )
r   r   r/   r   r0   r9   valuesr   r   rG   )rF   r   namesr.   rL   r   s        @r#   
input_speczFunctional.input_spec  s    4-.***	
	 d))40 ,,335 
 t22779: !& ))<)<T)BN  15=A$Q'==
 >s   C>Cc                     || _         y r   )r   )rF   values     r#   r   zFunctional.input_spec  s
    "'r$   c                     t         j                        st        j                         S  j                   j
                  d}i  j                  D ]c  }t        |j                  t              rd}nd}t        |j                        D ]*  \  }}t        ||      }| j                  v s!||<   |dz  }, e g } j                  D ]  }g }t        |j                        D ]K  \  }}t        ||      }| j                  v s!t        | j                        }	|	;|j                  |	       M t        j                   }
t#        j$                  dd      rt&        j                   }
 |
|      }|j                  |d<   ||d<   |j                  |        ||d	<    fd
fd} | j(                        |d<    | j*                        |d<   t-        j.                  |      S )Nr.   r&   rx   r   )	own_nodesuse_legacy_configFr.   inbound_nodesr=   c                     | j                   d   }| j                   d   }| j                   d   }t        ||      }|j                  vrt        d| d      |   }|j                  ||gS )Nr   rx      z(Internal error: could not find node key r   )rD   r   _nodesRuntimeErrorr.   )tensorrX   
node_indextensor_indexnode_keynew_node_indexnode_reindexing_maprF   s         r#   get_tensor_configz0Functional.get_config.<locals>.get_tensor_config  s    --a0I..q1J!003L$Y
;Ht{{*">xjJ  1:NNNNLAAr$   c                 0    t        j                  |       S r   r   ry   )tensorsr   s    r#   map_tensorsz*Functional.get_config.<locals>.map_tensors  s    %%&7AAr$   input_layersrN   )functional_like_constructorr"   r   
get_configr.   r&   
operations
issubclassr   r   _inbound_nodesr   r   serialize_noderW   r   serialize_keras_objectr   get_global_attributelegacy_serializationr   r   copydeepcopy)rF   configrX   
kept_nodesoriginal_node_indexnoder   layer_configsfiltered_inbound_nodes	node_dataserialize_obj_fnlayer_configr   r   r   s   `            @@r#   r   zFunctional.get_config  s   *4>>: ##D)) II
 ! 	$I)--z: 

-6((. $)#T )4GHt{{*4>'1!OJ$	$"  	/I%'"-6((. 	A)#T )4GHt{{* !/tt{{ KI ,.55i@	A  1GG001DeL#7#N#N +I6L#,>>L ,BL)  .)	/* )x
	B	B "-T-@-@!A~"-d.B.B"C}}V$$r$   r   )NN)F)__name__
__module____qualname____doc__r   r<   rR   rT   propertyr=   setterrk   rm   ro   r@   rp   r   r   r   r   r   rc   r   r   r   r   r   __classcell__)r"   s   @r#   r   r      s   EN6.<`   ]]
 
)$39    E$4$L"H*4X # # $ $" "> ">H ( (J%r$   r   c                    i i fdfd}fd}i }dD ]  }|j                  |      ||<    dD ]   }||v r|j                  |      ||<   d||<   " |d   D ]
  } ||        rd}|d   D ]k  }|d	      }	|	v s|	   }
d
}|t        |
      k  r#|
|   }	  ||	|       |dz  }|t        |
      k  r#|t        |
      k\  rd}|	= \|d
kD  sbd}|
|d |	<   m |st        d       r|d	   }|d   }fdfd |d         } |d         } | d||||d|S # t        $ r Y |w xY w)aI  Instantiates a Functional model from its config (from `get_config()`).

    Args:
        cls: Class of the model, e.g. a custom subclass of `Model`.
        config: Output of `get_config()` for the original model instance.
        custom_objects: Optional dict of custom objects.

    Returns:
        An instance of `cls`.
    c                 D    | vr|g| <   y|    j                  |       y)zAdd node to layer list

        Arg:
            layer: layer object
            node_data: Node data specifying layer call
        N)rW   )rM   r   unprocessed_nodess     r#   add_unprocessed_nodez4functional_from_config.<locals>.add_unprocessed_node  s.     ))(1{e$e$++I6r$   c                 4    t        |      \  }} | |i | y)zReconstruct node by linking to inbound layers

        Args:
            layer: Layer to process
            node_data: List of layer configs
        N)deserialize_node)rM   r   r    r!   created_layerss       r#   process_nodez,functional_from_config.<locals>.process_node"  s$     (	>Bf 	tvr$   c                     | d   }d| vrt        j                  |       }nt        j                  |       }t	        |t
              st        dt        |             ||<   | d   }|D ]  } ||        y)ztDeserializes a layer and index its inbound nodes.

        Args:
            layer_data: layer config dict.
        r.   module)custom_objectszMUnexpected object from deserialization, expected a layer or operation, got a r   N)r   model_from_configr   deserialize_keras_objectr/   r   r7   r8   )
layer_data
layer_namerM   inbound_nodes_datar   r
  r  r  s        r#   process_layerz-functional_from_config.<locals>.process_layer.  s      '
 :% !22>E &>>>E %+$$(K=2  &+z" (8+ 	3I
 !	2	3r$   )r=   r   rN   r   Nr=   Fr.   r   rx   TzInvalid Functional model configuration. The graph of the Functional model either has loops or disconnected nodes. The following nodes failed to be resolved: r&   c                     | vrt        d|  d      |    }t        |t              r|dz  }|j                  |   j                  }||   S )Nz%Internal error: could not find layer r   rx   )r   r/   r   r   output_tensors)r  r   r   rM   layer_output_tensorsr  s        r#   
get_tensorz*functional_from_config.<locals>.get_tensor  se    ^+7
|1E  z*eZ(!OJ$33J?NN#L11r$   c                    t        | t              r&t        |       dk(  rt        | d   t              r |  S t        | t              r*| j                         D ci c]  \  }}| |       c}}S t        | t              rt        | D cg c]
  } |       c}      S | D cg c]
  } |       c}S c c}}w c c}w c c}w )N   r   )r/   rz   rd   r   r0   r1   r   )r   rH   rI   r  r   s      r#   r   z+functional_from_config.<locals>.map_tensors  s    w%G!71:s+ w''gt$29--/B$!QA{1~%BBgu%':Q+a.:;;(/01A00 C:0s   B7B=%Cr   rN   )rG   rC   r.   r&   rP   )r5   rd   
IndexErrorr7   )r   r   r  r  r  functional_configkeyr  some_nodes_processedrM   node_data_listr   r   r.   r&   input_tensorsr  r
  r  r  r   r	  s     `              @@@@@r#   functional_from_configr$     s    N 
7
!3L : 1!'C#1$ *&=%+ZZ_c"%)c"	* (1 "
j!" $+H5 	KJ":f#56E ))!25!9 
 3~#66 .z :I$UI6 !OJ !3~#66 ^!44+/()%0!^+/(/=jk/J%e,9	K< $> %%' C R V$D!+.I
21   1. ABM !2?!CDN 	
  o & s   $	D22	D>=D>c                       fd}|S )z.Wraps each op to inject the call-context args.c                  t    j                         D ]  \  }}|t        di       v s||||<     | i |S )N_call_context_args)r1   getattr)r    r!   r.   r   call_context_argsrX   s       r#   rk   zoperation_fn.<locals>.call  sR    ,224 	%KD%	+?DD%$t	% $)&))r$   rP   )rX   r)  rk   s   `` r#   r`   r`     s    	* Kr$   c                     t        j                  | j                        }|j                  dd  }t        j                  t        j                        j                  dd  }||k(  ry|j
                  |j                  yy)Nrx   TF)inspectgetfullargspecr<   r    r   varargsvarkw)r   	init_spec	init_argsfunctional_init_argss       r#   r   r     sq    &&s||4Iqr"I"11*2E2EFKKABO(($)Dr$   c                 X    t        | t        t        f      rt        |       dk(  r| d   S | S )Nrx   r   )r/   rz   r   rd   rw   s    r#   rh   rh     s(    !dE]#A!tHr$   c                 .   | j                   sy fd}| j                  j                  }| j                  j                  }t	        j
                  ||      }t	        j
                  ||      }t        j                  |      t        j                  |      dS )Nc                 H   t        | t        j                        r| j                  \  }}}d}t	        |j
                  d |       D ]  \  }}t        ||      }|	vs|dz  } t        |||z
  |      | _        t        j                  |       }t        |||      | _        |S | S )Nr   rx   )
r/   r   r2   rD   r   r   r   r   r   r   )
rL   rX   r   r   irrelevant_node_countr   r   r   
serializedr   s
            r#   serialize_keras_tensorz.serialize_node.<locals>.serialize_keras_tensor  s     a,,-232B2B/Iz<$%!$Y%=%=kz%JK /4(A69,)Q.)/  ,:(==| A +AA!DJ+Iz<PAr$   )r    r!   )r#  	argumentsr    r!   r   ry   r   r   )r   r   r7  r    r!   s    `   r#   r   r     s    $ >>D^^""F4d;D 6?F!88>#::6B r$   c           	         | sg i fS t        | t              rg }| D ]  }|d   }|d   }|d   }t        |      dk(  ri }nt        |      dk(  r|d   }nt        d      |vrt        d|       |   }t        |j                        |k  rt        d| d	|j                   d
|       |j                  |   }	|j                  |	j                  |           t        |      gfS t        j                  | d         }
t        j                  | d         }fd}t        j                  ||
      }
t        j                  ||      }|
|fS )z1Return (args, kwargs) for calling the node layer.r   rx   r   r     z3Cannot deserialize the model (invalid config data?)z6Invalid Functional model configuration. Missing node: 0Layer node index out of bounds.
inbound_layer =  
inbound_layer._inbound_nodes = 
inbound_node_index = r    r!   c                 h   t        | t        j                        r| j                  }|| S j	                  |d   d       }|t        d|d          |d   }|d   }t        |j                        |k  rt        d| d|j                   d|       |j                  |   }|j                  |   S | S )Nr   zUnknown layer: rx   r   r;  r<  r=  )
r/   r   r2    _pre_serialization_keras_historygetr7   rd   r   r  r  )rL   historyrM   inbound_node_indexinbound_tensor_indexinbound_noder  s         r#   convert_revived_tensorz0deserialize_node.<locals>.convert_revived_tensor*  s    a,,-88G"&&wqz48E} ?71:,!?@@!(#*1: 5''(,>> '',g .66;6J6J5K L,,>+?A  !//0BCL../CDDr$   )r/   rz   rd   r7   r   r  rW   r  rh   r   r  r   ry   )r   r  r#  
input_datainbound_layer_namerB  rC  r!   inbound_layerrD  r    rE  s    `          r#   r  r    s   2v)T"# !	J!+A!+A#-a= :!#ZA%#A I  "7 L)*,  ++=>M =//04FF ''4o 66$334 5,,>+?	A  )778JKL  ++,@A?!	D !/0&8855i6GHD77	(8KLF* 4d;D 6?F<r$   c                 X    | j                   \  }}}|j                  |   }|j                  S r   )rD   r   is_input)rL   rX   r   r[   r   s        r#   r(   r(   D  s5    
 	
		##J/D==r$   c                     t        j                  | j                  | j                  | j                  | j
                   d      S )N_clone)rv   r   r   r.   )r   r2   rv   r   r   r.   rw   s    r#   clone_single_keras_tensorrM  N  s5    ggQWWQXXqvvhf<M r$   c                 :    fd}t        j                  ||       S )Nc                     t        | t        j                        s| S t        |       v rt        |          S t	        |       }|t        |       <   |S r   )r/   r   r2   idrM  )rL   new_xkt_id_mappings     r#   swapz!clone_keras_tensors.<locals>.swapU  sP    !W001Ha5M! A'')!,$ber$   r   )r   rR  rS  s    ` r#   clone_keras_tensorsrT  T  s     dG,,r$   c                 $    t        | |      \  }}|S r   r   )rG   rC   nodesr[   s       r#    find_nodes_by_inputs_and_outputsrW  a  s    &'*HE1Lr$   c                    t        | |      }g }g }i }i }t        j                  |       D ]  }t        |      r |j	                  |       ||t        |      <   .|j                  j                  }d}	t        |t              r|j                  }	t        |j                  |j                  |j                  |j                   d|	      }
|j	                  |
       |
|t        |      <   |
j                  d   |t        |j                  d         <    t        j                   | |      }t        j                  |      D ]=  }t#        |      }|j                  |_        |j	                  |       ||t        |      <   ? t        j                   ||      }|D ]  }t        |j                        |v r|t        |j                           }n|j                  }t%        |j&                  |      }t        |t              sAt%        |j(                  j*                  |      }t%        |j(                  j,                  |      }nd}i }t/        ||||        ||fS )a  Clone the `Node` between the inputs and output tensors.

    This function is used to create a new functional model from any intermediate
    Keras tensors. The clone of the nodes mimic the behavior of reconstructing
    the functional graph network by re-executing all the `__call__()` methods.
    The cloned nodes will be appended to the layers.

    Note that a new `keras.Input` will be created for any items in the
    `inputs`

    Args:
    inputs: A nested structure of `KerasTensor` instances.
    outputs: A nested structure of `KerasTensor` instances.

    Returns:
        A pair of inputs and outputs, with cloned `KerasTensor` instances.
        They can be used to create a new functional model.
    FCLONE)batch_shaper   r   r.   r   r   rP   )	call_argscall_kwargsrC   )rW  r   r6   r(   rW   rP  rD   rX   r/   r   r   r   rv   r   r   r.   pack_sequence_asrM  rT  r  r8  r    r!   r   )rG   rC   nodes_to_clonecloned_inputscloned_outputsrR  op_id_mappingkt_inputoriginal_opr   cloned_input	kt_outputcpyr   rX   output_copycall_args_copycall_kwargs_copys                     r#   r:   r:   f  s8   & 6fgFNMN MMLL(  *  **2M"X,' #11;;KH+z2&// $NNnn e,!L   .*6M"X,'++A. "X44Q789). ))&-@M\\'* +	'	2&55c"'*bm$+ **7NCN 
dnn.%b&89II *$*=*=}M)Z00##]N  3%%}   N! 	$(		
/
: .((r$   r   )rP   )0r   r+  r   r3   	keras.srcr   r   r   keras.src.backend.commonr   !keras.src.layers.core.input_layerr   r   keras.src.layers.input_specr	   keras.src.layers.layerr
   keras.src.legacy.savingr   r   r   keras.src.models.modelr   keras.src.ops.functionr   r   r   keras.src.ops.noder   r   keras.src.ops.operationr   keras.src.savingr   keras.src.utilsr   r   r$  r`   r   rh   r   r  r(   rM  rT  rW  r:   rP   r$   r#   <module>rv     s           1 3 8 1 ( 0 I ( + - 0 + # - . $b%5 b%Jtn"DFR
-
\)r$   