
    2Vh$                         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  ej"                         adZddZddZddZd Zd Zd Zd Zd Zd Zy)    N)logging)backend)layers)losses)metrics)models)
optimizers)tree)serialization)object_registration)modulefunction_typeoutput_shape_typeoutput_shape_modulec                     t        | t              rt        d|  d      t        t        d      st
        j                  t        _        t
        j                  t        j                  d<   t        j                  t        j                  d<   t        j                  t        j                  d<   t        j                  t        j                  d<   | d   j                  d	d
      }|| d   dk(  r	|| d   d<   n|| d   d<   | d   j                  dd
      }|Zt        |t              r#t        |      dk(  rt        |d         | d   d<   n't        |t        t         f      rt        |      | d   d<   | d   dk(  rft"        D ]  }| d   j                  |d
      } | d   d   }t        |t              r.di d}|d   |d   d<   |d   |d   d<   |d   |d   d<   || d   d<   t%        | dd      } t'        j(                  | t        j                  |d      S )a  Instantiates a Keras model from its config.

    Args:
        config: Configuration dictionary.
        custom_objects: Optional dictionary mapping names
            (strings) to custom classes or functions to be
            considered during deserialization.

    Returns:
        A Keras model instance (uncompiled).

    Raises:
        TypeError: if `config` is not a dictionary.
    zG`model_from_config` expects a dictionary, not a list. Received: config=z8. Did you meant to use `Sequential.from_config(config)`?ALL_OBJECTS
InputLayer
FunctionalModel
Sequentialconfigbatch_input_shapeN
class_namebatch_shapeinput_shapeaxis   r   Lambdafunction
__lambda__r   r   codedefaults   closurezkeras.layer)module_objectscustom_objectsprintable_module_name)
isinstancelist	TypeErrorhasattrMODULE_OBJECTSr   __dict__r   r   r   r   r   r   poplenintfloatLAMBDA_DEP_ARGS_find_replace_nested_dictr   deserialize_keras_object)r   r(   r   r   dep_arg_function_configfunction_dicts           T/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/legacy/saving/saving_utils.pymodel_from_configr<      s(    &$  &x (00
 	
 >=1%+__"393D3D""<0393D3D""<0.4ll""7+393D3D""<0x(,,-@$G$,</.?F8]+.?F8]+(-DdD!c$i1n'*47|F8V$sEl+'*4yF8V$ lx'& 	4Gx $$Wd3A	4 *:6ot,+72FM.=a.@M(#F+2A!2DM(#J/1@1CM(#I.+8F8Z( 'vxBF11%11%%	     c                 *   ddl m} d| j                  j                  i}	 | j	                         |d<   t        t        |      t        j                         |      }t        | dd      r|r| j                  r| j                  j                  }|j                  dd       t        |      |d	<   t        j                   | j"                  j                        | j"                  j	                         d
}||d	   d<   |S # t
        $ r}|r|Y d}~d}~ww xY w)z3Returns a dictionary containing the model metadata.r   )__version__r   r   N)keras_versionr   model_config	optimizerFtraining_configr!   optimizer_config)	keras.srcr?   	__class____name__
get_configNotImplementedErrordictstrr   getattrcompiled_compile_configr   r0   _serialize_nested_configr   get_registered_namerB   )	modelinclude_optimizerrequire_configr@   rA   emetadatarC   rD   s	            r;   model_metadatarV   `   s   6 %//":":;L!&!1!1!3X
 -(!!H
 uk5).?>>#33::OT2*B+H&' 2EEOO--  //446	  ?OH&'(:;O/  G s   C; ;	DDDc                    |i }t        j                  |      5  | d   }t        j                  |      }t	        ||t              }d}| j                  dd      }|+t        t        j                  |      }t	        ||t              }d}| j                  dd      }|!t        t        |      }t	        ||t              }d}| j                  dd      }	|	t        t        |	      }| d   }
ddd       t        
      S # 1 sw Y   xY w)z4Return model.compile arguments from training config.NrD   lossr   weighted_metricsloss_weights)rB   rX   r   rY   rZ   )r   CustomObjectScoper	   deserialize!_resolve_compile_arguments_compatget_deserialize_nested_configr   _deserialize_metricmetrics_modulerJ   )rC   r(   rD   rB   rX   loss_configr   metrics_configrY   weighted_metrics_configrZ   s              r;   !compile_args_from_training_configre      s?   		.	.~	> $7*+=>**+;<	5'
	
 %))&$7"-f.@.@+ND4T;OD (,,Y=%0#^G 8G
  "1"5"56H$"O".9#%<  '~6I$7L )! M$7 $7s   CDD
c                 4    d }t        j                  ||       S )z/Serialized a nested structure of Keras objects.c                 F    t        |       rt        j                  |       S | S N)callabler   serialize_keras_objectobjs    r;   _serialize_fnz/_serialize_nested_config.<locals>._serialize_fn   s    C= 77<<
r=   )r
   map_structure)r   rm   s     r;   rO   rO      s    
 mV44r=   c           	      >   d }|y ||      r | |      S t        |t              r.|j                         D ci c]  \  }}|t        | |       c}}S t        |t        t
        f      r|D cg c]  }t        | |       c}S t        d| d      c c}}w c c}w )z=Deserializes arbitrary Keras `config` using `deserialize_fn`.c                 P    t        | t              rd| v ryt        | t              ryy)Nr   TF)r*   rJ   rK   rk   s    r;   _is_single_objectz5_deserialize_nested_config.<locals>._is_single_object   s&    c4 \S%8c3r=   NzrSaved configuration not understood. Configuration should be a dictionary, string, tuple or list. Received: config=.)r*   rJ   itemsr_   tupler+   
ValueError)deserialize_fnr   rq   kvrl   s         r;   r_   r_      s     ~ f%%	FD	! 
1 ).!<<
 	
 
FUDM	*GM
@C&~s;
 	
 	??Eha	I 


s   B/Bc                 8    | dv r| S t        j                  |       S )z7Deserialize metrics, leaving special strings untouched.)accuracyacccrossentropyce)ra   r\   )metric_configs    r;   r`   r`      s$    AA %%m44r=   c                 ~    t        j                  |       }|j                  ||      }t        j                  |      } | S rh   )jsondumpsreplaceloads)r   findr   dict_strs       r;   r5   r5      s5    zz&!Hg.HZZ!FMr=   c                 p    t        | t              r%| |j                  vr|j                  |d   d         } | S )aC  Resolves backwards compatibility issues with training config arguments.

    This helper function accepts built-in Keras modules such as optimizers,
    losses, and metrics to ensure an object being deserialized is compatible
    with Keras 3 built-ins. For legacy H5 files saved within Keras 3,
    this does nothing.
    r   name)r*   rK   ALL_OBJECTS_DICTr^   )rl   
obj_configr   s      r;   r]   r]      s8     #s6+B+B BjjH-f56Jr=   c                 B   	 | j                   j                  s%| j                   j                  | j                         | j                  j                  s1| j                  j                  | j                  | j                         y y #  t        j                  d       Y y xY w)NzCompiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.)compiled_lossbuiltbuildoutputscompiled_metricsr   warning)rQ   s    r;   try_build_compiled_argumentsr      sx    

""((%%emm4%%++""((F ,
5	
s   BB Brh   )TT)r   	threadingabslr   rE   r   r   r   r   ra   r   r	   r
   keras.src.legacy.savingr   keras.src.savingr   localr.   r4   r<   rV   re   rO   r_   r`   r5   r]   r    r=   r;   <module>r      so          /     1 0 "BJB1h5<5

r=   