o
    2hE#                     @   s   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  ad	Zdd
dZdddZdddZdd Zdd Zdd Zdd Zdd ZdS )    N)logging)backend)losses)metrics)tree)serialization)object_registration)modulefunction_typeoutput_shape_typeoutput_shape_modulec           
      C   s  t | trtd|  dttds:ddlm} ddlm} |jt_	|j
tj	d< |jtj	d< |jtj	d	< |jtj	d
< | d dd}|durY| d dkrS|| d d< n|| d d< | d dd}|durt |tr{t|dkr{t|d | d d< nt |ttfrt|| d d< | d dkrtD ]
}| d |d}q| d d }t |trdi d}	|d |	d d< |d |	d d< |d |	d d< |	| d d< tj| tj	|d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_OBJECTSr   )layers)models
InputLayer
FunctionalModel
Sequentialconfigbatch_input_shapeN
class_namebatch_shapeinput_shapeaxis   Lambdafunction
__lambda__r   r   codedefaults   closurelayer)module_objectscustom_objectsprintable_module_name)
isinstancelist	TypeErrorhasattrMODULE_OBJECTS	keras.srcr   r   __dict__r   r   r   r   r   poplenintfloatLAMBDA_DEP_ARGSr   deserialize_keras_object)
r   r%   r   r   r   r   dep_arg_function_configfunction_dict r8   _/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/legacy/saving/saving_utils.pymodel_from_config   sR   



r:   Tc           	   
   C   s   ddl m} d| jji}z|  |d< W n ty* } z
|r |W Y d}~nd}~ww tt|t |d}t	| ddrd|rd| j
rd| jj}|dd t||d	< t| jj| j d
}||d	 d< |S )z3Returns a dictionary containing the model metadata.r   )__version__r   r   N)keras_versionr   model_config	optimizerFtraining_configr   optimizer_config)r,   r;   	__class____name__
get_configNotImplementedErrordictstrr   getattrcompiled_compile_configr   r.   _serialize_nested_configr   get_registered_namer>   )	modelinclude_optimizerrequire_configr<   r=   emetadatar?   r@   r8   r8   r9   model_metadata[   s:   rQ   c                 C   s   |du ri }t |a ddlm} | d }||}t|||}d}| dd}|dur9ttj|}t||t}d}| dd}|durPtt	|}t||t
}d}	| dd}
|
duratt	|
}	| d }W d   n1 sow   Y  t||||	|d	S )
z4Return model.compile arguments from training config.Nr   )
optimizersr@   lossr   weighted_metricsloss_weights)r>   rS   r   rT   rU   )r   CustomObjectScoper,   rR   deserialize!_resolve_compile_arguments_compatget_deserialize_nested_configr   _deserialize_metricmetrics_modulerE   )r?   r%   rR   r@   r>   rS   loss_configr   metrics_configrT   weighted_metrics_configrU   r8   r8   r9   !compile_args_from_training_config|   sL   

(r`   c                 C   s   dd }t || S )z/Serialized a nested structure of Keras objects.c                 S   s   t | r	t| S | S N)callabler   serialize_keras_objectobjr8   r8   r9   _serialize_fn   s   
z/_serialize_nested_config.<locals>._serialize_fn)r   map_structure)r   rf   r8   r8   r9   rJ      s   rJ   c                    st   dd }|du r
dS ||r |S t |tr" fdd| D S t |ttfr2 fdd|D S td| d	)
z=Deserializes arbitrary Keras `config` using `deserialize_fn`.c                 S   s(   t | trd| v rdS t | trdS dS )Nr   TF)r'   rE   rF   rd   r8   r8   r9   _is_single_object   s
   
z5_deserialize_nested_config.<locals>._is_single_objectNc                    s   i | ]
\}}|t  |qS r8   rZ   ).0kvdeserialize_fnr8   r9   
<dictcomp>   s    
z._deserialize_nested_config.<locals>.<dictcomp>c                    s   g | ]}t  |qS r8   ri   )rj   re   rm   r8   r9   
<listcomp>   s    
z._deserialize_nested_config.<locals>.<listcomp>zrSaved configuration not understood. Configuration should be a dictionary, string, tuple or list. Received: config=.)r'   rE   itemstupler(   
ValueError)rn   r   rh   r8   rm   r9   rZ      s$   


rZ   c                 C   s   | dv r| S t | S )z7Deserialize metrics, leaving special strings untouched.)accuracyacccrossentropyce)r\   rW   )metric_configr8   r8   r9   r[      s   
r[   c                 C   s*   t | tr| |jvr||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'   rF   ALL_OBJECTS_DICTrY   )re   
obj_configr	   r8   r8   r9   rX      s   rX   c                 C   sT   z| j js| j | j | jjs| j| j| j W d S W d S    td Y d S )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)rL   r8   r8   r9   try_build_compiled_arguments   s   
r   ra   )TT)	threadingabslr   r,   r   r   r   r\   r   keras.src.legacy.savingr   keras.src.savingr   localr+   r2   r:   rQ   r`   rJ   rZ   r[   rX   r   r8   r8   r8   r9   <module>   s$    

D
!6
