
    &Vf|q                        d Z ddlZ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 eeeefZd
Z G d d          Z G d d          Z ed          d             Zd Z G d d          Zd Z d Z!d Z" eddg          d             Z#d Z$d$dZ%d$dZ&d Z'd Z( edd g          	 d%d"            Z)	 d$d#Z*dS )&z6Object config serialization and deserialization logic.    N)
api_export)backend)keras_export)global_state)object_registration)python_utils)
tensorflow)activationsconstraintsinitializerslossesmetrics
optimizersregularizersc                       e Zd Zd Zd ZdS )SerializableDictc                     || _         d S Nconfig)selfr   s     _/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/saving/serialization_lib.py__init__zSerializableDict.__init__!   s        c                 *    t          | j                  S r   )serialize_keras_objectr   r   s    r   	serializezSerializableDict.serialize$   s    %dk222r   N)__name__
__module____qualname__r   r    r   r   r   r       s2          3 3 3 3 3r   r   c                   &    e Zd ZdZddZd Zd ZdS )SafeModeScopezBScope to propagate safe mode flag to nested deserialization calls.Tc                     || _         d S r   )	safe_mode)r   r&   s     r   r   zSafeModeScope.__init__+   s    "r   c                 `    t                      | _        t          j        d| j                   d S Nsafe_mode_saving)in_safe_modeoriginal_valuer   set_global_attributer&   r   s    r   	__enter__zSafeModeScope.__enter__.   s*    *nn)*<dnMMMMMr   c                 :    t          j        d| j                   d S r(   )r   r,   r+   r   argskwargss      r   __exit__zSafeModeScope.__exit__2   s)    ) 3	
 	
 	
 	
 	
r   N)T)r   r    r!   __doc__r   r-   r2   r"   r   r   r$   r$   (   sO        LL# # # #N N N
 
 
 
 
r   r$   z*keras.config.enable_unsafe_deserializationc                  0    t          j        dd           dS )zADisables safe mode globally, allowing deserialization of lambdas.r)   FNr   r,   r"   r   r   enable_unsafe_deserializationr6   8   s     %&8%@@@@@r   c                  *    t          j        d          S r(   r   get_global_attributer"   r   r   r*   r*   >   s    ,-?@@@r   c                       e Zd ZdZd Zd ZdS )ObjectSharingScopez?Scope to enable detection and reuse of previously seen objects.c                 Z    t          j        di            t          j        di            d S Nshared_objects/id_to_obj_mapshared_objects/id_to_config_mapr5   r   s    r   r-   zObjectSharingScope.__enter__E   s0    )*H"MMM)*KRPPPPPr   c                 Z    t          j        dd            t          j        dd            d S r=   r5   r/   s      r   r2   zObjectSharingScope.__exit__I   s:    )*H$OOO)-t	
 	
 	
 	
 	
r   N)r   r    r!   r3   r-   r2   r"   r   r   r;   r;   B   s;        IIQ Q Q
 
 
 
 
r   r;   c                 ^    t          j        d          }||                    | d          S dS )z:Retrieve an object previously seen during deserialization.r>   N)r   r9   get)obj_idid_to_obj_maps     r   get_shared_objectrE   P   s=     5& M    ... ! r   c                     |d         dk    rd|d<   t          j        d          }|dS t          t          |                     }||vr|||<   dS ||d<   ||         }||d<   dS )z>Call after serializing an object, to keep track of its config.module__main__Nr?   shared_object_id)r   r9   intid)objr   id_to_config_maprC   prev_configs        r   !record_object_after_serializationrO   Y   s    h:%%x#8)  C\\F%%%#)   %+!"&v.*0&'''r   c                 @    t          j        d          }|dS | ||<   dS )zFCall after deserializing an object, to keep track of it in the future.r>   Nr8   )rL   rC   rD   s      r   #record_object_after_deserializationrQ   k   s4     5& M M&r   z#keras.saving.serialize_keras_objectz"keras.utils.serialize_keras_objectc                 	   | | S t          | t                    r| S t          | t          t          f          r2d | D             }t          | t                    rt          |          n|S t          | t                    rt          |           S t          | t                    rdd|                     d          idS t          | t                    r?dt          | j
                  t          | j                  t          | j                  ddS t          | t          t                              rd	i dS t          | t          j                  rEt#          | d
d          }|rt          |          }|d         j        |d<   d| j        | j        |ddS t*          j        r7t          | t*          j                  r| j        |                                 ndS t          j        |           rCdt          j        |                                           t          j        | j                  ddS t          |           j        t>          j         k    rjt          | t>          j!                  r<| j"        dk    r1d|                                 t          j        | j                  ddS | #                                S t*          j        r!t          | t*          j$                  r| j        S t          | tJ          j&                  rO| j         dk    rDtO          j(        dtS          j*        |            d           ddtW          j,        |           idS t*          j        rht          | t*          j-                  rN| .                                }t          t_          d |                    }d| j0        j         | j0        j        |ddS tc          |           }te          | j0        |          }|"tg          | |           ti          | |           |S t          | tJ          j&                  r| j        }n| j0        j        }| j0        j         }|dk    rd}nHt          | tJ          j&                  rtk          j6        |           }ntk          j6        | j0                  }||||d}	tg          | |	           ti          | |	           |	S )a	  Retrieve the config dict by serializing the Keras object.

    `serialize_keras_object()` serializes a Keras object to a python dictionary
    that represents the object, and is a reciprocal function of
    `deserialize_keras_object()`. See `deserialize_keras_object()` for more
    information about the config format.

    Args:
        obj: the Keras object to serialize.

    Returns:
        A python dict that represents the object. The python dict can be
        deserialized via `deserialize_keras_object()`.
    Nc                 ,    g | ]}t          |          S r"   r   ).0xs     r   
<listcomp>z*serialize_keras_object.<locals>.<listcomp>   s!    ===A,Q//===r   	__bytes__valueutf-8)
class_namer   	__slice__)startstopstep__ellipsis___keras_historyr   __keras_tensor__)shapedtypekeras_history
__tensor__)rY   rd   	__numpy__z<lambda>a  The object being serialized includes a `lambda`. This is unsafe. In order to reload the object, you will have to pass `safe_mode=False` to the loading function. Please avoid using `lambda` in the future, and use named Python functions instead. This is the `lambda` being serialized:    )
stacklevel
__lambda__c                     t          | t          j                  r|                                 n"t          | t          j                  r| j        n| S r   )
isinstancetfTensorShapeas_listDTypenamerV   s    r   <lambda>z(serialize_keras_object.<locals>.<lambda>   sB    !!R^44DAIIKKK$.q"($;$;B!&& r   __typespec__)r[   	spec_namerG   r   registered_namebuiltinsrG   r[   r   rv   )7rl   PLAIN_TYPESlisttupledictserialize_dictbytesdecodeslicer   r]   r^   r_   typeEllipsisr   KerasTensorgetattrrq   rc   rd   rm   	availablern   _dimsro   	is_tensorconvert_to_numpytoliststandardize_dtyper    npr   ndarrayndimitemrp   typesFunctionTypewarningswarninspect	getsourcer   	func_dumpTypeSpec
_serializemap	__class___get_class_or_fn_configserialize_with_public_classget_build_and_compile_configrO   r   get_registered_name)
rL   
config_arrhistory	ts_configinner_configconfig_with_public_classrG   r[   rv   r   s
             r   r   r   u   s   * {
#{## 
#e}%% K=====
$.sE$:$:JuZ   
J#t #c""" #u 
%

7 3 34
 
 	
 #u 
%/	::.sx88.sx88 
 
 	
 #tH~~&& <,;;;#w*++ 
#/66 	)7mmG GAJ,!( 
 
 	
 
| @
377 @ #	 5s{{}}}4? 
& 1#66==?? 239== 
 
 	
 Cyyr{**c2:&& 
	38a<<) ZZ\\$6syAA    88::	| 
311 x#u)** 
s|z/I/IO
 7>6G6L6LO O 	
 	
 	
 	
 '/44
 
 	
 
| 

344 
NN$$	 
  	
 	
	 )/m.#
 
 	
 +3//L:|     +$S*BCCC)#/GHHH'' #u)** *)'Jc5-.. 	1EcJJOO1E O
  *	 F !f---%c6222Mr   c                     t          | d          r(|                                 }|t          |          |d<   t          | d          r(|                                 }|t          |          |d<   d S )Nget_build_configbuild_configget_compile_configcompile_config)hasattrr   r}   r   )rL   r   r   r   s       r   r   r     s    s&'' B++--#%3L%A%AF>"s()) F//11%'5n'E'EF#$
Fr   c                     t          j        |           }|)t          j        |           }|dS | j        | j        ||dS |                    d          }d                    |dd                   |d         |ddS )zSerializes classes from public Keras API or object registration.

    Called to check and retrieve the config of any class that has a public
    Keras API or has been registered as serializable via
    `keras.saving.register_keras_serializable()`.
    Nrx   .)r   get_name_from_symbolr   r   r    r   splitjoin)clsr   keras_api_namerv   partss        r   r   r   &  s      4S99N -A#FF"4 n,".	
 
 	
   %%E((5":&&Bi	  r   c                    |r|d||dS t          j        |           }|r7|                    d          }d                    |dd                   d||dS t	          j        |           }|s| j        dk    sdS | j        d||dS )aO  Serializes functions from public Keras API or object registration.

    Called to check and retrieve the config of any function that has a public
    Keras API or has been registered as serializable via
    `keras.saving.register_keras_serializable()`. If function's module name
    is already known, returns corresponding config.
    functionrx   r   Nr   rw   )r   r   r   r   r   r   r    )fnr   fn_module_namer   r   rv   s         r   serialize_with_public_fnr   I  s      
$$%	
 
 	
  4R88N 
$$S))hhuSbSz**$%	
 
 	
 .A"EE 	r}
'B'B4m$.	
 
 	
r   c                    t          | t          j                  r| j        S t	          | d          rM|                                 }t          |t                    st          d|  d|           t          |          S t	          | d          rt          j
        |           S t          d|  dt          |            d          )z1Return the object's config depending on its type.
get_configzThe `get_config()` method of z$ should return a dict. It returned: r   zCannot serialize object z	 of type zG. To be serializable, a class must implement the `get_config()` method.)rl   r   r   r   r   r   r|   	TypeErrorr}   r   r   r   )rL   r   s     r   r   r   m  s     #u)** |sL!! 
!!&$'' 	1 1 1(.1 1   f%%%	j	!	! 
"6s;;;@s @ @T#YY @ @ @
 
 	
r   c                 >    d |                                  D             S )Nc                 4    i | ]\  }}|t          |          S r"   rT   )rU   keyrY   s      r   
<dictcomp>z"serialize_dict.<locals>.<dictcomp>  s'    MMM:3C'..MMMr   )items)rL   s    r   r}   r}     s    MMMMMMr   z%keras.saving.deserialize_keras_objectz$keras.utils.deserialize_keras_objectTc           
      v   t                      }||n|                    dd          }pi t          j        di           }t          j        }i ||| dS t          | t                    rr                    |           |          S t          | t          t          f          rfd| D             S |\d\  }}	}
t          | t                    rd| v r| d         }d| vrt          d|            r=| d         v s0|                     d	          v st          |t                    r|v rd
}
nl| d         dk    r | d         }	|	dk    r	| d         } nI| d	         } n@|                     dd          "t          d| d          d| d          d          | d         } |
sr| |vr| S t          ||          t          j                  r&t!          t#          ||          | |	                    S t!          t%          ||          |                    S t          | t&                    r| S t          | t                    st          d|            d| vsd| vr!fd|                                 D             S | d         }| d         pi }pi |dk    r1t+          j        |d         |d                   }|d         |_        |S |dk    r"t+          j        |d         |d                   S |dk    r"t3          j        |d         |d                   S | d         dk    r|d                             d          S | d         d k    rt8          S | d         d!k    rSt;          t!          |d"         #          t!          |d$         #          t!          |d%         #                    S | d         d&k    r+rt          d'          t=          j        |d                   S t@          f| d         d(k    rZtC          | d)         | d	         | d         d*| +          }tE          d, |          }|#                    t          |                    S |                     dd          }|                     d	|          }|dk    r|}tC          |||d| +          S d-| v rtI          | d-                   }||S tC          |||d*| +          }t          |t          j                  r|S tK          |d.          st          d/| d0|            t	          j&                  }tO                    }|5  |5  	 |(                    |          }n)# t          $ r}t          | d1|  d2|           d}~ww xY w|                     d3d          }|r#|j)        s|*                    |           d
|_)        |                     d4d          }|r|+                    |           d
|_,        ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   d-| v rt[          || d-                    |S )5a:  Retrieve the object by deserializing the config dict.

    The config dict is a Python dictionary that consists of a set of key-value
    pairs, and represents a Keras object, such as an `Optimizer`, `Layer`,
    `Metrics`, etc. The saving and loading library uses the following keys to
    record information of a Keras object:

    - `class_name`: String. This is the name of the class,
      as exactly defined in the source
      code, such as "LossesContainer".
    - `config`: Dict. Library-defined or user-defined key-value pairs that store
      the configuration of the object, as obtained by `object.get_config()`.
    - `module`: String. The path of the python module. Built-in Keras classes
      expect to have prefix `keras`.
    - `registered_name`: String. The key the class is registered under via
      `keras.saving.register_keras_serializable(package, name)` API. The
      key has the format of '{package}>{name}', where `package` and `name` are
      the arguments passed to `register_keras_serializable()`. If `name` is not
      provided, it uses the class name. If `registered_name` successfully
      resolves to a class (that was registered), the `class_name` and `config`
      values in the dict will not be used. `registered_name` is only used for
      non-built-in classes.

    For example, the following dictionary represents the built-in Adam optimizer
    with the relevant config:

    ```python
    dict_structure = {
        "class_name": "Adam",
        "config": {
            "amsgrad": false,
            "beta_1": 0.8999999761581421,
            "beta_2": 0.9990000128746033,
            "decay": 0.0,
            "epsilon": 1e-07,
            "learning_rate": 0.0010000000474974513,
            "name": "Adam"
        },
        "module": "keras.optimizers",
        "registered_name": None
    }
    # Returns an `Adam` instance identical to the original one.
    deserialize_keras_object(dict_structure)
    ```

    If the class does not have an exported Keras namespace, the library tracks
    it by its `module` and `class_name`. For example:

    ```python
    dict_structure = {
      "class_name": "MetricsList",
      "config": {
          ...
      },
      "module": "keras.trainers.compile_utils",
      "registered_name": "MetricsList"
    }

    # Returns a `MetricsList` instance identical to the original one.
    deserialize_keras_object(dict_structure)
    ```

    And the following dictionary represents a user-customized `MeanSquaredError`
    loss:

    ```python
    @keras.saving.register_keras_serializable(package='my_package')
    class ModifiedMeanSquaredError(keras.losses.MeanSquaredError):
      ...

    dict_structure = {
        "class_name": "ModifiedMeanSquaredError",
        "config": {
            "fn": "mean_squared_error",
            "name": "mean_squared_error",
            "reduction": "auto"
        },
        "registered_name": "my_package>ModifiedMeanSquaredError"
    }
    # Returns the `ModifiedMeanSquaredError` object
    deserialize_keras_object(dict_structure)
    ```

    Args:
        config: Python dict describing the object.
        custom_objects: Python dict containing a mapping between custom
            object names the corresponding classes or functions.
        safe_mode: Boolean, whether to disallow unsafe `lambda` deserialization.
            When `safe_mode=False`, loading an object has the potential to
            trigger arbitrary code execution. This argument is only
            applicable to the Keras v3 model format. Defaults to `True`.

    Returns:
        The object described by the `config` dictionary.
    Nmodule_objectscustom_objects_scope_dictc                 4    g | ]}t          |           S custom_objectsr&   deserialize_keras_object)rU   rV   r   r&   s     r   rW   z,deserialize_keras_object.<locals>.<listcomp>  sA     
 
 
  %.I  
 
 
r   )NNFr   r[   z%Unknown `config` as a `dict`, config=rv   Tr   rG   rw   _z#Cannot deserialize object of type `z`. If `zj` is a custom class, please register it using the `@keras.saving.register_keras_serializable()` decorator.r   )r   zCould not parse config: c                 <    i | ]\  }}|t          |           S r   r   )rU   r   rY   r   r&   s      r   r   z,deserialize_keras_object.<locals>.<dictcomp>R  sH     
 
 
 U )n	  
 
 
r   rb   rc   rd   )rd   re   rf   rY   rg   rX   rZ   r`   r\   r]   r   r^   r_   rj   aR  Requested the deserialization of a `lambda` object. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the saved model, you can pass `safe_mode=False` to the loading function in order to allow `lambda` loading, or call `keras.config.enable_unsafe_deserialization()`.rt   ru   class)obj_typefull_configr   c                     t          | t                    rt          j        |           n=t	          t          j        t          |                     rt          t          |           n| S r   )rl   rz   rm   rn   r   dtypesstrr   rr   s    r   rs   z*deserialize_keras_object.<locals>.<lambda>  sO    a&&Kq!!!(/	3q66(B(BIgb!nnn r   rI   from_configz&Unable to reconstruct an instance of 'zM' because the class is missing a `from_config()` method. Full object config: z could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by `get_config()` are explicitly deserialized in the model's `from_config()` method.

config=z.

Exception encountered: r   r   ).r*   popr   r9   r   GLOBAL_CUSTOM_OBJECTSrl   r   rB   rz   r{   r|   
ValueErrorr   r   r   r   r   r   ry   r   r   r    _pre_serialization_keras_historyconvert_to_tensorr   arrayencoder   r   r   	func_loadrm   _retrieve_class_or_fnr   _deserializerE   r   CustomObjectScoper$   r   builtbuild_from_configcompile_from_configcompiledrQ   )r   r   r&   r1   safe_scope_argr   tlcogcor   r   has_custom_objectr[   rL   rG   rv   fn_namer   custom_obj_scopesafe_mode_scopeinstanceer   r   s    ``                    r   r   r     s   P "^^N"0"<)IZZ 0$77N#)rN,-H"MMD

3C66$6#6N~t 	63&& v&&2 f%%&4-(( 

 
 
 
 
 	
 
 
 	
 !:K7n&7fd## &	.6!!%h/6)) DFDD  
  .|$66::/00NBB|S11 C %66 %)!! %33!'!1!Z//#H-FF#$56FF ::h,,4#%"<0% %"<0% % %    -  	^++.0%2DEE /,&v.  $2	    ,+"6*    .	    &+&& fd## =;6;;<<<6!!XV%;%;
 
 
 
 
 %llnn	
 
 
 	
 %J(#)rL#)rN '''!!g)>
 
 
 0<O/L,
\!!(!g)>
 
 
 	
 [  xW-\'5JKKKKl{**G$++G444l~--l{**$W%-#  
 %V$-#  
 %V$-#  
 
 	
" l|++ 	J   %l7&;<<<	~&..@@#;$%8)
 
 
  
 
 
 l 3 3444 ZZ$''Fjj!2J??OZ$)
 
 
 	
 V##'9 :;;?J
%  C #u)** 
3&& 
,Z , ,#), ,
 
 	
 +<^LL#I..O	 % %? % %
	|44HH 	 	 	 F F
 %F F
 CDF F  	 zz.$77 	" 	"&&|444!HN$4d;; 	%((888 $H'% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %* V##+f/0	
 	
 	
 Os[   >XW=UW=
U?#U::U??A2W=1X=X	XX	XXXc                 J   |dk    rt          j        | |          }nt          j        ||          }||S |rQ|dk    s|                    d          r |dz   | z   }t          j        |          }||S |dk    r|dk    r}t
          D ]%}	t          j        d|	z   dz   | z             }||c S &fd|                                D             }
|
r.t          t          |
	                                                    S 	 t          j        |          }	n)# t          $ r t          d| d	|  d
| d           w xY wt          |	                              | d           }|%|#t          |	                              |d           }||S t          d| d	|  d           )Nr   r   keraszkeras.r   rw   c                 R    i | ]#\  }}|                     d                     ||$S r   )endswith)rU   kvr   s      r   r   z)_retrieve_class_or_fn.<locals>.<dictcomp>  sF       Aq::k(3441  r   zCould not deserialize z 'z' because its parent module z) cannot be imported. Full object config: zCould not locate zr'. Make sure custom classes are decorated with `@keras.saving.register_keras_serializable()`. Full object config: )r   get_registered_object
startswithr   get_symbol_from_nameBUILTIN_MODULESr   nextitervalues	importlibimport_moduleModuleNotFoundErrorr   varsrB   )rq   rv   rG   r   r   r   
custom_objapi_namerL   modfiltered_dicts       `      r   r   r     sk   
 :(>
 
 


 )>N
 
 

  2
 W 1 1( ; ;|d*H1(;;C
 z!!f
&:&:&   5sNS(4/  ?JJJ #   *0022  M
  :D!5!5!7!788999	)&11CC" 	 	 	5 5 5T 5 5%+5 5'25 5  	 3iimmD$'' ;?6s))--66C?J
	-H 	- 	- 	- 	-  +	- 	-  s   D &D<r   )NT)+r3   r   r   r   r   numpyr   	keras.srcr   r   keras.src.api_exportr   keras.src.backend.commonr   keras.src.savingr   keras.src.utilsr   keras.src.utils.module_utilsr	   rm   r   rJ   floatboolry   r   r   r$   r6   r*   r;   rE   rO   rQ   r   r   r   r   r   r}   r   r   r"   r   r   <module>r     s   < <                              - - - - - - 1 1 1 1 1 1 0 0 0 0 0 0 ( ( ( ( ( ( 9 9 9 9 9 9C%3 3 3 3 3 3 3 3
 
 
 
 
 
 
 
  :;;A A <;A
A A A
 
 
 
 
 
 
 
/ / /1 1 1$      -, \ \ \~	 	 	       F!
 !
 !
 !
H
 
 
0N N N /.  ,0V V V Vt
 JNI I I I I Ir   