
    2Vh                        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Zddl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 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.Z.	 ddl0Z0	 ddl1Z1dZ2dZ3dZ4e4dz   Z5e4dz   Z6dZ7dZ8dZ9d<dZ:d Z;d  Z<d! Z=d" Z>d=d#Z?d$ Z@d% ZAd& ZB	 d>d'ZC	 d?d(ZDd@d)ZEd* ZFd+ ZGd, ZHd- ZI	 	 	 	 dAd.ZJd/ ZKd0 ZL G d1 d2      ZM G d3 d4      ZN G d5 d6eN      ZO G d7 d8      ZPd9 ZQd: ZRd; ZSy# e/$ r dZ.Y w xY w# e/$ r dZ0Y w xY w# e/$ r dZ1Y w xY w)Bz3Python-based idempotent model-saving functionality.    N)backend)global_state)Layer)Loss)Metric)	Optimizer)ObjectSharingScope)deserialize_keras_object)serialize_keras_object)CompileMetrics)dtype_utils)
file_utils)io_utils)naming)
plot_model)check_pydot)readable_memory_size)weight_memory_size)__version__config.jsonzmetadata.jsonzmodel.weightsz.h5z.npzassetsg      ?a  
---
library_name: keras
---

This model has been uploaded using the Keras library and can be used with JAX,
TensorFlow, and PyTorch backends.

This model card has been generated automatically and should be completed by the
model author.
See [Model Cards documentation](https://huggingface.co/docs/hub/model-cards) for
more information.

For more details about the model architecture, check out
[config.json](./config.json).c                 J   |dk(  rt         t        d      | j                  st        j                  dd       t        |t        j                        rt        | ||       yt        |      }|j                  d      }|r|j                  d      st        d	|       |s|j                  d      rt        d
|       |r|rt        d| d|       |rt        | ||       y|st        | ||       yt        j                   |      r`t        j"                         }t        | ||       t        j$                  |d      5 }|j'                  |j)                                ddd       yt+        |d      5 }t        | ||       ddd       y# 1 sw Y   yxY w# 1 sw Y   yxY w)a  Save a zip-archive representing a Keras model to the given file or path.

    The zip-based archive contains the following structure:

    - JSON-based configuration file (config.json): Records of model, layer, and
        other saveables' configuration.
    - H5-based saveable state files, found in respective directories, such as
        model/states.npz, model/dense_layer/states.npz, etc.
    - Metadata file.

    The states of Keras saveables (layers, optimizers, loss, and metrics) are
    automatically saved as long as they can be discovered through the attributes
    returned by `dir(Model)`. Typically, the state includes the variables
    associated with the saveable, but some specially purposed layers may
    contain more such as the vocabularies stored in the hashmaps. The saveables
    define how their states are saved by exposing `save_state()` and
    `load_state()` APIs.

    For the case of layer states, the variables will be visited as long as
    they are either 1) referenced via layer attributes, or 2) referenced via a
    container (list, tuple, or dict), and the container is referenced via a
    layer attribute.
    h5Nz0h5py must be installed in order to save a model.zYou are saving a model that has not yet been built. It might not contain any weights yet. Consider building the model first by calling it on some data.   )
stacklevelhf://.keraszOInvalid `filepath` argument: expected a `.keras` extension. Received: filepath=zbWhen using `zipped=False`, the `filepath` argument should not end in `.keras`. Received: filepath=zaWhen saving to the Hugging Face Hub, you should not save the model as zipped. Received: filepath=z	, zipped=wb)h5pyImportErrorbuiltwarningswarn
isinstanceioIOBase_save_model_to_fileobjstr
startswithendswith
ValueError_upload_model_to_hf_save_model_to_dirr   is_remote_pathBytesIOFilewritegetvalueopen)modelfilepathweights_formatzippedis_hfzip_filepathfs          K/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/saving/saving_lib.py
save_modelr<   M   s   0 $,LMM;; 	
 (BII&uh?8}H(Eh''1""*-
 	
 h''133;*>
 	
 %33;*IfXO
 	
 E8^<5(N;$$X.::<L"5,G40 1A--/01 1 h% A&ua@A A1 1A As    F6FFF"c                    t               5  t        |       }d d d        t        j                        }t        j                  t        t
        j
                  j                         j                  d      d      }||fS # 1 sw Y   ixY w)Nz%Y-%m-%d@%H:%M:%S)keras_version
date_saved)r	   r   jsondumpsr>   datetimenowstrftime)r4   serialized_model_dictconfig_jsonmetadata_jsons       r;   _serialize_model_as_jsonrH      sx    		 > 6u =>**23KJJ*"++//1::;NO	
M %%> >s   A??Bc                 B   t        j                  |      st        j                  |       t        |       \  }}t	        t        j
                  |t              d      5 }|j                  |       d d d        t	        t        j
                  |t              d      5 }|j                  |       d d d        t        j
                  |t              }t        j
                  |t              }	 |dk(  rt        |d      }n!|dk(  rt        |d      }nt        d|       t        |d      }	t        | ||	dt!                      |j#                          |	j#                          y # 1 sw Y   xY w# 1 sw Y   xY w# j#                          	j#                          w xY w)Nwr   modenpzTUnknown `weights_format` argument. Expected 'h5' or 'npz'. Received: weights_format= weights_storeassets_store
inner_pathvisited_saveables)r   existsmakedirsrH   r3   join_METADATA_FILENAMEr1   _CONFIG_FILENAME_VARS_FNAME_H5_ASSETS_DIRNAME	H5IOStore
NpzIOStorer+   DiskIOStore_save_statesetclose)
r4   dirpathr6   rF   rG   r:   weights_filepathassert_dirpathrQ   asset_stores
             r;   r-   r-      sh   W%G$!9%!@K	joog'9:C	@ A		joog'78#	> !	!w?__Wo>NT!%&6SAMu$&'7cBM,,:+;= 
 ".s;'$!e	
 	7  0 	s%   E$E0*AE< $E-0E9<"Fc           	         t        |       \  }}t        j                  |d      5 }|j                  t        d      5 }|j                  |j                                d d d        |j                  t        d      5 }|j                  |j                                d d d        d }d }d }	d}
	 |dk(  rx	 t        |       rt        t        |d      }nXt        j                  |j                        j                  }t        j                   |      }t        |j                  d      }d}
n&|dk(  rt#        t$        |d      }nt'        d	|       t)        t*        |d      }	t-        | ||	d
t/                      	 |r|j3                          |	r|	j3                          |
r"|r |j                  |j                  t               |r|j3                          	 d d d        y # 1 sw Y   xY w# 1 sw Y   RxY w#  t        t        |d      }Y xY w#  d}
|rd |_         xY w# |r|j3                          |	r|	j3                          |
r"|r |j                  |j                  t               |r|j3                          w w xY w# 1 sw Y   y xY w)NrJ   Fr   archiverL   dirrK   TrM   rN   rO   rP   )rH   zipfileZipFiler3   rX   r1   encoderY   is_memory_sufficientr\   rZ   pathlibPathnameparenttempfileNamedTemporaryFiler]   _VARS_FNAME_NPZr+   r^   r[   r_   r`   rh   ra   )r4   fileobjr6   rF   rG   zfr:   weights_file_pathrQ   re   write_zfworking_dirs               r;   r'   r'      sw   !9%!@K	#	& G*"WW'- 	,GGM((*+	,WW%s+ 	*qGGK&&()	* !=	*%+E2 )2*BS) '.ll7<<&@&G&G,4,G,G +-) )2-22) $(  5( *#Rc! !00>/?A  &orLK+("%% ##%!!#-*//@ !'')OG* G*	, 	,	* 	*8 %.&%M.	H(,%##%!!#-*//@ !'') !MG* G*sz   J GJ: G*J+H1A6G7'AH9AJG'	"J*G4	/J7HHHH!!AI>>JJ
c                    t         t        d      |}|j                  d      r|dd  }|j                  d      dkD  rt	        d|       t        j
                  dt              }|j                  |d	
      }|j                  }t        j                         5 }t        | ||       t        }t               rJt        j                  |dd      }	t!        | |	d	d	d	       t#        | j$                        dk  r|dz  }n|dz  }t'        t        j                  |d      d      5 }
|
j)                  |       d d d        |j+                  ||d       t-        j.                  d| d| d       d d d        y # 1 sw Y   BxY w# 1 sw Y   y xY w)NzWTo save models to the Hugging Face Hub, you must install the `huggingface_hub` package.r      /   zVInvalid `hf_path` argument: expected `namespace/model_name` format. Received: hf_path=keras)library_namelibrary_versionT)exist_okr   zsummary_plot.png)to_fileshow_layer_namesshow_shapes
show_dtype
   z 

![](./assets/summary_plot.png)zCA plot of the model can be found [here](./assets/summary_plot.png).z	README.mdrJ   zSave model using Keras.)repo_idfolder_pathcommit_messagez%Model saved to the Hugging Face Hub: z<
To load back the model, use `keras.saving.load_model('hf://z')`)huggingface_hubr    r)   countr+   HfApir>   create_repor   rs   TemporaryDirectoryr-   _MODEL_CARD_TEMPLATEr   r   rW   r   lenlayersr3   r1   upload_folderr   	print_msg)r4   hf_pathr6   original_hf_pathapirepo_urlr   tmp_dir
model_card	plot_pathr:   s              r;   r,   r,     s   >
 	

 '"!"+}}SA**:);=
 	

 

mC w6HG		$	$	& "
'5'>:)
=";MNI!!%  5<< B&BB
9

 *//';7= 	 GGJ	  	4 	 	

 	3H: >..5Yc;	
="
 "
,	  	 -"
 "
s%   BF"E548F5E>	:FF
c                 Z   t        | t        j                        rt        | |||      S t	        |       j                  d      r@t        t        d      | dd }t        j                  |dt              }t        ||||      S t	        |       } | j                  d      s^t        j                  |       xr) t        j                  t        j                  | d            }|rt        | |||      S t!        d	|        t#        | d
      5 }t        ||||      cddd       S # 1 sw Y   yxY w)z.Load a zip archive representing a Keras model.r   NzYTo load models from the Hugging Face Hub, you must install the `huggingface_hub` package.r|   r   )r   r   r   r   r   zDInvalid filename: expected a `.keras` extension. Received: filepath=rb)r$   r%   r&   _load_model_from_fileobjr(   r)   r   r    snapshot_downloadr>   _load_model_from_dirr*   r   isdirrU   rW   r+   r3   )r5   custom_objectscompile	safe_moder   r   is_keras_dirr:   s           r;   
load_modelr   M  sG   (BII&'ngy
 	
 
X	!	!'	*"B 
 12,%77 )

 $)
 	
 x=  *%++H5 *:K:K-8;L +ngy  &&.Z1  (D! 	Q+>7I	 	 	s   	D!!D*c           	         t        j                  |       st        d|        t        j                  |       st        d|        t	        t        j
                  | t              d      5 }|j                         }d d d        t        |||      }t        j                  |       }	 t        |v r(t        j
                  | t              }t        |d      }	nJt        |v r(t        j
                  | t              }t        |d      }	nt        dt         dt         d      t        |      dkD  r&t        t        j
                  | t               d      }
nd }
t#               }i }t%        ||	|
d	t#               ||
       |	j'                          |
r|
j'                          	 |rt)        |       |S # 1 sw Y   6xY w# 	j'                          
r|
j'                          w w xY w)NzDirectory doesn't exist: zPath isn't a directory: rrK   Expected a  or  file.   rO   rQ   rR   rS   rT   failed_saveables
error_msgs)r   rU   r+   r   r3   rW   rY   read_model_from_configlistdirrZ   r\   ru   r]   r   r^   r[   r`   _load_statera   _raise_loading_failure)rb   r   r   r   r:   rF   r4   all_filenamesrx   rQ   re   r   r   s                r;   r   r   w  s   W%4WI>??G$3G9=>>	joog'78#	> !ffh{NGYOE&&w/M" ]* * H%&7cBM- * I&'8sCMn-T/1B&I  }!%9K
 K5
'$!e-!	
 	z*LW J 	 s   +F0(CF= 0F:=%G"c                     t        j                  |       }|sd |d<   t               5  t        |||      }d d d        |S # 1 sw Y   S xY w)Ncompile_config)r   )r@   loadsr	   r
   )rF   r   r   r   config_dictr4   s         r;   r   r     sU     **[)K(,$%		 
(9

 L	
 Ls   A  A
c           
         t        j                  | d      5 }|j                  t        d      5 }|j	                         }d d d        t        |||      }|j                         }d }	d }
d }	 t        |v r	 t        |      rEt        j                  |j                  t        d      j	                               }t        |d      }
nt        j                  t        j                  | j                         j"                        }	|j%                  t        |	j                          t        t        j                  |	j                   t              d      }
n5t&        |v rt)        t&        |d      }
nt+        dt         dt&         d      t-        |      dkD  rt/        t0        |d      }t3               }i }t5        ||
|d	t3               ||
       |
r|
j7                          |r|j7                          |	r|	j9                          	 |rt;        |       d d d        |S # 1 sw Y   xY w#  t        t        |d      }
Y xY w# |
r|
j7                          |r|j7                          |	r|	j9                          w w xY w# 1 sw Y   S xY w)Nr   rK   ri   r   r   r   r   rg   rO   r   )rk   rl   r3   rY   r   r   namelistrZ   rn   r%   r/   r\   rs   r   ro   rp   rq   rr   extractru   r]   r+   r   r^   r[   r`   r   ra   cleanupr   )rv   r   r   r   rw   r:   rF   r4   r   extract_dirrQ   re   io_filer   r   s                  r;   r   r     sS   	#	& E/"WW%s+ 	#q&&(K	# #)
 6	&.L+E2 #%**GGNC8==?# )2'(D
 '/&A&A 'W\\ : A A' 

>;3C3CD(1#LL)9)9>J!$) !M1 *?BS I !.!1o5FfM  =!A%)/2CP"uJ+("%%!1% ##%!!###%":.KE/L LK	# 	#BL %.nbs$KM. ##%!!###% CE/L LsT   I=H,I=,I5CH(A<IAI=H%	 I=(H><I9I::I==Jc                    | j                   st        d      t        |      }d}d}||j                  d      st        d|       ||j                  d      st        d|       	 t	        j
                  |      rKt               }t        j                  j                  |t        j                  j                  |            }|}|}|t        ||d      }nt        |d      }|t        d	 |D              }	n
t               }	t        | |dd
|	       |j                          |,t	        j                   ||       t#        j$                  |       yy# |,t	        j                   ||       t#        j$                  |       w w xY w)zjSave only the weights of a model to a target filepath.

    Supports both `.weights.h5` and `.keras`.
    zYou are saving a model that has not yet been built. Try building the model first by calling it on some data or by using `build()`.N.weights.h5z;The filename must end in `.weights.h5`. Received: filepath=)
weights.h5weights.jsonz`The filename must end in `.weights.json` when `max_shard_size` is specified. Received: filepath=rJ   rK   c              3   2   K   | ]  }t        |        y wNid.0os     r;   	<genexpr>z$save_weights_only.<locals>.<genexpr>-       #CaBqE#C   rO   rP   )r!   r+   r(   r*   r   r.   get_temp_dirospathrW   basenameShardedH5IOStorer\   r`   r_   ra   copyshutilrmtree)
r4   r5   max_shard_sizeobjects_to_skipfilepath_strr   remote_filepathlocal_filepathrQ   rT   s
             r;   save_weights_onlyr     s    ;;"
 	
 x=LGOl&;&;M&J"".1
 	
 
	#L,A,A&- --9N<
 	
#$$X."nGWW\\'2773C3CH3MNN&O%H%,X~CPM%hS9M& ##C?#C C #'/	
 	OOHo6MM'" 7OOHo6MM'" s   *B=E 0Fc           
      t   | j                   st        d      d}d}t        |      }	 t        j                  |      rat               }t        j                  j                  |t        j                  j                  |            }t        j                  ||       |x}}|j                  d      rt        |d      }nX|j                  d      rt        |d      }n9|j                  d      r(t        j                  |d      }t        t         |d      }t#               }	|t#        d	 |D              }
n
t#               }
i }t%        | dd
||
|	|       |j'                          |r|j'                          |	rt)        ||       |t+        j,                  |       yy# |t+        j,                  |       w w xY w)znLoad the weights of a model from a filepath (.keras or .weights.h5).

    Note: only supports h5 for now.
    zYou are loading weights into a model that has not yet been built. Try building the model first by calling it on some data or by using `build()`.Nr   r   rK   r   r   rg   c              3   2   K   | ]  }t        |        y wr   r   r   s     r;   r   z$load_weights_only.<locals>.<genexpr>c  r   r   rO   )rQ   rR   rS   skip_mismatchrT   r   r   )	warn_only)r!   r+   r(   r   r.   r   r   r   rW   r   r   r*   r\   r   rk   rl   rZ   r`   r   ra   r   r   r   )r4   r5   r   r   rh   r   r   r   rQ   r   rT   r   s               r;   load_weights_onlyr   >  s    ;;"
 	
 GGx=L)#$$\2"nGWW\\)),7N OOL.9&44L8  .%hS9M"">2,XC@M""8,ooh4G%ngCPM5& ##C?#C C #
''/-!		
 	MMO":GMM'" 7MM'" s   EF F7c                    t        | j                               d   }| |   \  }}dt        |        d| d| d| j                         D cg c]  }|d   	 c} }|rt	        j
                  |       y t        |      c c}w )Nr   zA total of z? objects could not be loaded. Example error message for object z:

z,

List of objects that could not be loaded:
)listkeysr   valuesr"   r#   r+   )r   r   	first_keyex_saveableex_errorxmsgs          r;   r   r   |  s    Z__&'*I&y1K
c*o& 'e* 6$++-.QAaD.
/	1  co /s   
A>c                 :   t        j                  |      s| j                  ||       y t        j                  |      D ][  }t        j                  ||      j                  dd      }t        j                  ||      j                  dd      }t        | ||       ] y N\r}   )r   r   r1   r   rW   replace_write_to_zip_recursively)zipfile_to_savesystem_pathzip_path	file_namesystem_file_pathzip_file_paths         r;   r   r     s    K(k84#++K8 		I){IFNNc  'OOHi@HHcM &!1=		    c                 2    | j                  d      rd| z   S | S )z3Make sure that private attributes are visited last._~)r)   )rq   s    r;   	_name_keyr     s    sTzKr   c              #   j  K   ddl m} t        | |      st        d|  dt	        |              | j                         }t        |      }|dv rd| j                  f t        t        |       d       D ]+  }|j                  d	      s||v r	 t        | |      }||f - y # t        $ r Y :w xY ww)
Nr   KerasSaveablez>Expected object to be an instance of `KerasSaveable`, but got 	 of type )
Sequential
Functionalr   c                     t        |       S r   )r   )r   s    r;   <lambda>z _walk_saveable.<locals>.<lambda>  s
    )A, r   )key__)keras.src.saving.keras_saveabler   r$   r+   type	_obj_typeget_attr_skipsetr   sortedrj   r)   getattr	Exception)saveabler   obj_typeattr_skipset
child_attr	child_objs         r;   _walk_saveabler    s     =h.*Id8n%57
 	
 !!#H#H-L
 //''S]0FG $
  &**D	*5I )##$
  		s*   BB3B$	B3$	B0-B3/B00B3c           
         ddl m} t        |t        t        t
        f      st        d| dt        |             t        |t        t        d       f      st        d| dt        |             t        |       |v ry t        | d      r[|rYt        | d      r)t        | j                  t              rd| j                  i}nd }| j                  |j                  ||             t        | d	      r"|r | j                  |j                  |             |j!                  t        |              t#        |       D ]  \  }}t        ||      r5t%        |||t'        j(                  ||      j+                  d
d      |       Gt        |t,        t.        t0        t2        f      sht5        |||t'        j(                  ||      j+                  d
d      |        y )Nr   r   iExpected `weights_store` to be an instance of `H5IOStore`, `ShardedH5IOStore` or `NpzIOStore`. Received: r   PExpected `assets_store` to be an instance of `DiskIOStore` or `None`. Received: save_own_variablesrq   metadatasave_assetsr   r}   rS   rT   )r  r   r$   r\   r   r]   r+   r  r^   r   hasattrrq   r(   r  maker  addr  r_   r   rW   r   r   dicttupler`   _save_container_state)	r  rQ   rR   rS   rT   r   r  r  r  s	            r;   r_   r_     s    >mi1A:%NO&ym1D0EG
 	

 l[$t*$=>%i\0B/CE
 	
 
(|((x-.=8V$HMM3)G.HH##zH=	
 x'L\..z:;"X,' "0!9 
Ii/%??:zBJJ# #4 	D$s#;<!%??:zBJJ# #4r   c                    ddl m} t        |t        t        t
        f      st        d| dt        |             t        |t        t        d       f      st        d| dt        |             |rt        |       |v ry d}	t        | d      rH|rF|s|"	 | j                  |j                  |             n | j                  |j                  |             t        | d	      rH|rF|s|"	 | j                  |j                  |             n | j                  |j                  |             |t!        |      }nd}t#        |       D ]  \  }}t        ||      r8t%        |||t'        j(                  ||      j+                  d
d      ||||       Jt        |t,        t.        t0        t2        f      skt5        |||t'        j(                  ||      j+                  d
d      ||||        |t!        |      |z
  }nd}|	sd||dk  r|j                  t        |              t        |       |v r5|j7                  t        |              |j9                  t        |              y y y # t        $ r7}
|j                  t        |              | |
f|t        |       <   d}	Y d }
~
d }
~
ww xY w# t        $ r7}
|j                  t        |              | |
f|t        |       <   d}	Y d }
~
d }
~
ww xY w)Nr   r   r  r   r  Fload_own_variablesTload_assetsr   r}   rS   r   rT   r   r   )r  r   r$   r\   r   r]   r+   r  r^   r   r  r   getr
  r  r!  r   r  r   r   rW   r   r   r  r  r`   _load_container_stateremovepop)r  rQ   rR   rS   r   rT   r   r   r   failureecurrently_failedr  r  newly_faileds                  r;   r   r     s    >mi1A:%NO&ym1D0EG
 	

 l[$t*$=>%i\0B/CE
 	
 R\->>Gx-.=,8++M,=,=j,IJ ''(9(9*(EFx'L,8$$\%5%5j%AB   !1!1*!=>#/0 "0!9 
Ii/%??:zBJJ# ,"3!1% 	D$s#;<!%??:zBJJ# ,"3!1%8 #+,/??(\Q->!!"X,/h<++##BxL1NN2h<( , s   $$R\2+3Q;
2h<(   $$R\2+3Q;
2h<(s0    I$ * J' $	J$-,JJ$'	K'0,K""K'c           
         ddl m} i }t        | t              rt	        | j                               } | D ]  }t        ||      st        j                  |j                  j                        }||v r||xx   dz  cc<   | d||    }nd||<   t        |||t        j                  ||      j                  dd      |        y )Nr   r   r~   r   r   r}   r  )r  r   r$   r  r   r   r   to_snake_case	__class____name__r_   r   rW   r   )		containerrQ   rR   rS   rT   r   
used_namesr  rq   s	            r;   r  r  d  s     >J)T"))+,	 h. ''(:(:(C(CDDz!4 A% qD!1 23#$
4 %??:t<DDT3O"3r   c                    ddl m} i }	t        | t              rt	        | j                               } | D ]  }
t        |
|      st        j                  |
j                  j                        }||	v r|	|xx   dz  cc<   | d|	|    }nd|	|<   t        |
||t        j                  ||      j                  dd      ||||        y )Nr   r   r~   r   r   r}   r"  )r  r   r$   r  r   r   r   r,  r-  r.  r   r   rW   r   )r/  rQ   rR   rS   r   rT   r   r   r   r0  r  rq   s               r;   r$  r$    s     >J)T"))+,	 h.''(:(:(C(CDDz!4 A% qD!1 23#$
4 %??:t<DDT3O+"3!1%	r   c                   *    e Zd ZdZddZd Zd Zd Zy)r^   zAsset store backed by disk storage.

    If `archive` is specified, then `root_path` refers to the filename
    inside the archive.

    If `archive` is not specified, then `root_path` refers to the full path of
    the target directory.
    Nc                    || _         || _        || _        d | _        | j                  rt	               | _        | j                   dk(  r&| j                  j                  | j                         t        j                  | j                  | j                        j                  dd      | _	        | j                   dk(  r t        j                  | j                         y y |dk(  r|| _	        y t	               | _        t        j                  | j                  | j                        j                  dd      | _	        t        j                  | j                         y )Nr   )r   r   r}   rJ   )rL   	root_pathrh   r   r   
extractallr   rW   r   rz   rV   selfr4  rh   rL   s       r;   __init__zDiskIOStore.__init__  s   	"<<'>DLyyC''T\\':)dnn gdC   yyC##D$4$45   s{#, +~#-??LL$..$'$$   ##D$4$45r   c                     |s| j                   S t        j                  | j                   |      j                  dd      }t        j                  |      st        j
                  |       |S r   )rz   r   rW   r   rU   rV   r7  r   s     r;   r  zDiskIOStore.make  sU    ###t//6>>tSI  &%r   c                     |s| j                   S t        j                  | j                   |      j                  dd      }t        j                  |      r|S y r   )rz   r   rW   r   rU   r:  s     r;   r#  zDiskIOStore.get  sJ    ###t//6>>tSIT"Kr   c                 (   | j                   dk(  r7| j                  r+t        | j                  | j                  | j                         | j
                  r@t        j                  | j
                        r t        j                  | j
                         y y y NrJ   )	rL   rh   r   rz   r4  r   r   rU   r   r7  s    r;   ra   zDiskIOStore.close  sd    99%d.. <<J--dll;dll+ <<r   NN)r.  
__module____qualname____doc__r8  r  r#  ra    r   r;   r^   r^     s    60,r   r^   c                   p    e Zd ZdZddZd Zd ZddZd Zd Z	dd	Z
d
 Zd Zd Zd Zd Zd Zd Zd Zy)r\   a  Numerical variable store backed by HDF5.

    Args:
        path_or_io: `str`, `pathlib.Path` or `io.BytesIO` object. The path where
            to save the model.
        archive: Optional `zipfile.ZipFile` object. If specified, the h5 file
            will be saved inside the archive and `path_or_io` will be used as
            the filename.
        mode: `str`. One of {`"r"`, `"w"`}. The mode to open the h5 file.
            Defaults to `"r"`.
    Nc                    |dvrt        d|       t        |t        t        j                  f      rt        j                  |      | _        nJt        |t        j                        r|t        d      || _        nt        d| dt        |       d      || _
        || _        d | _        | j                  | j
                        | _        d | _        i | _        d | _        d| _        y )NrJ   r   .`mode` should be either 'w' or 'r'. Received: zHWhen `path_or_io` is an `io.BytesIO` object, `archive` should be `None`.z\`path_or_io` should be a `str`, `pathlib.Path` or `io.BytesIO` object. Received: path_or_io=r   .F)r+   r$   r(   ro   rp   
path_or_ior%   r/   	TypeErrorr  rL   rh   r   _get_h5_fileh5_file_h5_entry_path_h5_entry_group_h5_entry_metadata_h5_entry_initialized)r7  rI  rh   rL   s       r;   r8  zH5IOStore.__init__  s    z!@G  j3"56%ll:6DO
BJJ/" (  )DO==GL IZ(), 
 	 ((9 #!"&%*"r   c                 6    | j                   j                         S r   )rL  __bool__r>  s    r;   rR  zH5IOStore.__bool__  s     ||$$&&r   c                 X   | j                   r~| j                  dk(  rt        j                         | _        n*| j                   j                  t        |      d      | _        t        j                  | j                  | j                        S t        j                  || j                        S )NrJ   r   rK   )	rh   rL   r%   r/   r   r3   r(   r   r0   )r7  rI  s     r;   rK  zH5IOStore._get_h5_file  sl    <<yyC!zz|#||00Z#F99T\\		::99Zdii88r   c                     | j                   dk7  rt        d      t        |t        t	        d      f      st        d|       || _        |r| j                  ||       | S i | _        d| _        | S )a  Make a new H5 entry group.

        This method is only available in write mode. It defers the creation of
        the H5 entry group until `__setitem__` is called, preventing the
        creation of empty groups.

        Args:
            path: `str`. The variable path.
            metadata: Optional `dict`. The metadata to save with the H5 entry
                group. Defaults to `None`.
        rJ   z%`make` is only allowed in write mode.Nz1`metadata` should be a dict or `None`. Received: r  F)	rL   r+   r$   r  r  rM  _create_h5_grouprN  rP  r7  r   r  s      r;   r  zH5IOStore.make  s     99DEE(T4:$67CH:N  #!!$!:  $&D ).D&r   c                    | j                   dk7  rt        d      || _        i | _        |s#d| j                  v r| j                  d   | _        n|| j                  v r)d| j                  |   v r| j                  |   d   | _        nVd| j                  v rH|j                  dd      }|| j                  v r(d| j                  |   v r| j                  |   d   | _        d| _        | S )zGet the H5 entry group.

        This method is only available in read mode.

        Args:
            path: `str`. The variable path.
        r   z#`get` is only allowed in read mode.vars_layer_checkpoint_dependenciesr   T)rL   r+   rM  rN  rL  r   rP  r:  s     r;   r#  zH5IOStore.get:  s     99BCC"!%'+||F';$T\\!fT0B&B#'<<#5f#=D  04<<?||H.NO4<<'Fdll46H,H+/<<+=f+ED(%)"r   c                 J   | j                   j                          | j                  dk(  rS| j                  rG| j                  j	                  t        | j                        | j                  j                                | j                  r| j                  j                          y y r=  )	rL  ra   rL   rh   writestrr(   rI  r   r2   r>  s    r;   ra   zH5IOStore.closeU  sj    99LL!!#doo"68M8M8OP<<LL  r   c                    |s!| j                   j                  d      | _        n/| j                   j                  |      j                  d      | _        |r1|j                         D ]  \  }}|| j                  j                  |<     d| _        y )NrX  T)rL  create_grouprN  itemsattrsrP  )r7  r   r  kvs        r;   rU  zH5IOStore._create_h5_group^  s    #'<<#<#<V#DD #'<<#<#<T#B#O#O$D   ( 2101$$**1-2 &*"r   c                 6    | j                   j                         S r   )rN  __len__r>  s    r;   rc  zH5IOStore.__len__k  s    ##++--r   c                 6    | j                   j                         S r   )rN  r   r>  s    r;   r   zH5IOStore.keysn  s    ##((**r   c                 6    | j                   j                         S r   )rN  r^  r>  s    r;   r^  zH5IOStore.itemsq  s    ##))++r   c                 6    | j                   j                         S r   )rN  r   r>  s    r;   r   zH5IOStore.valuest  s    ##**,,r   c                     | j                   |   }t        |d      rEd|j                  v r7|j                  d   dk(  r%t        j                  |t
        j                        }|S )Nr_  dtypebfloat16)rh  )rN  r  r_  nparray	ml_dtypesri  )r7  r  values      r;   __getitem__zH5IOStore.__getitem__w  sT    $$S)E7#5;;&G$
2HHU)*<*<=Er   c                 l   | j                   dk7  rt        d      | j                  s| j                  | j                         t        j                  |      }t        j                  |j                        dk(  r-| j                  j                  ||      }d|j                  d<   y || j                  |<   y )NrJ   z.Setting a value is only allowed in write mode.ri  )datarh  )rL   r+   rP  rU  rM  r   convert_to_numpystandardize_dtyperh  rN  create_datasetr_  )r7  r  rm  dss       r;   __setitem__zH5IOStore.__setitem__  s    99MNN))!!$"5"56((/$$U[[1Z?%%44Su4EB *BHHW(-D  %r   c                 R    | j                   dk7  rt        d      | j                  |= y )NrJ   z/Deleting a value is only allowed in write mode.)rL   r+   rN  )r7  r  s     r;   __delitem__zH5IOStore.__delitem__  s(    99NOO  %r   c                     || j                   v S r   )rN  )r7  items     r;   __contains__zH5IOStore.__contains__  s    t++++r   Nr   r   )r.  r@  rA  rB  r8  rR  rK  r  r#  ra   rU  rc  r   r^  r   rn  ru  rw  rz  rC  r   r;   r\   r\     sS    
+B'
9:6!*.+,-.&
,r   r\   c                   @     e Zd ZdZddZ fdZd Zd Z fdZ xZ	S )r   aV  Sharded numerical variable store backed by HDF5.

    Args:
        path_or_io: `str` or `pathlib.Path` object. The path where to save the
            model.
        max_shard_size: `int` or `float`. Maximum size in GB for each sharded
            file. If `None`, no sharding will be done. Defaults to `None`.
        archive: Optional `zipfile.ZipFile` object. If specified, the h5 file
            will be saved inside the archive and `path_or_io` will be used as
            the filename.
        mode: `str`. One of {'r', 'w'}. The mode to open the h5 file. Defaults
            to `"r"`.
    c           	         |dvrt        d|       t        |t        t        j                  f      st        d| dt        |       d      t        j                  |      | _        || _        || _	        d | _
        t        |      | _        | j                  j                  j                  dd      | _        | j                  j                   dk7  ro| j                  d	k(  rd
nd}| j                  j#                  d      }t%        j&                  | dt        | j                         dt        |       d       || _        d | _        i | _        d | _        d| _        d| _        d| _        d| _        d | _        | j                  d	k(  rddii d| _        n| j                  rVt;        j<                  | j                  j?                  t        | j                        d      jA                               | _        n9t?        | j                  d      5 }t;        jB                  |      | _        d d d        | jE                         | _#        y # 1 sw Y   xY w)NrF  rG  zL`path_or_io` should be a `str`, `pathlib.Path` object. Received: path_or_io=r   rH  z.weightsrO   z.jsonrJ   SavingLoadingzH sharded weights requires `*.json` as the extension. The original path: z will be renamed to Fr   
total_size)r  
weight_mapr   )$r+   r$   r(   ro   rp   rJ  r  r   rL   rh   r   floatr   stemr   	base_namesuffixwith_suffixr"   r#   rM  rN  rO  rP  current_shard_indexcurrent_shard_sizetotal_shard_sizecurrent_shard_pathsharding_configr@   r   r3   r   load_create_new_shard_filerL  )r7  rI  r   rh   rL   methodnew_pathmap_files           r;   r8  zShardedH5IOStore.__init__  s   z!@G  *sGLL&9:((2|9T*=M<NaQ  LL,		#N3//
B?99w&!%c!1XyFyy,,W5HMM( 114TYY0@ A!(m_A/
 !DI #!"&%*" $% "# !"&99 !! !	$D  ||'+zzLL%%c$))nc:??A($ $))S) ?X+/99X+>D(?224? ?s   IIc                 V   |sd}n|}| j                   d   }|j                  |      xs |j                  d|z   dz         }|X|| j                  j                  k7  r?| j	                          | j                  | j                  j                  |            | _        t        |   |      S )zGet the H5 entry group.

        This method is only available in read mode. If the path is not found in
        the current shard, it will switch to the correct shard.

        Args:
            path: `str`. The variable path.
        z/varsr  r}   )
r  r#  r  rq   ra   rK  r   	with_namerL  super)r7  r   parsed_pathr  filenamer-  s        r;   r#  zShardedH5IOStore.get  s     !KK )),7
>>+. 
*..+'3
 H0G0G0L0L$LJJL,,TYY-@-@-JKDLw{4  r   c                    | j                   j                          | j                  dk(  r| j                  | j                  d   d<   t        j                  | j                  d      }| j                  rw| j                  j                  t        | j                        |       | j                  j                  t        | j                        | j                  j                                n0t        | j                  d      5 }|j                  |       d d d        | j                  r| j                  j                          y y # 1 sw Y   1xY w)NrJ   r  r     )indent)rL  ra   rL   r  r  r@   rA   rh   r[  r(   r   r  r   r2   r3   r1   )r7  json_strr:   s      r;   ra   zShardedH5IOStore.close  s    99%%   ,\: zz$"6"6qAH||%%c$))nh?%%//0$,,2G2G2I $))S) &QGGH%&<<LL  & &s    EEc                     | j                    d| j                  dd}| xj                  dz  c_        | j                  j                  |      | _        | j                  | j                        S )Nr   05r   r~   )r  r  r   r  r  rK  )r7  new_shard_paths     r;   r  z'ShardedH5IOStore._create_new_shard_file  sf    ~~a 8 8<KH 	 	  A% "&))"5"5n"E  !8!899r   c           	      V   t        j                  |      }t        j                  |j                        }t	        j
                  |j                        }t        j                  |      }||z  dz  }| xj                  ||z  dz  z  c_	        || j                  kD  r;t        |dz        }t        | j                  dz        }t        d| d| d| d      | xj                  |z  c_        | j                  | j                  kD  rG| j                          | j                         | _        | j#                  | j$                         || _        t&        
| Q  ||       | j*                  j,                  }	|	| j.                  d   vr'| j0                  j,                  | j.                  d   |	<   y y )	Ng       B      @zThe size of z is z& which exceeds the maximum shard size zJ. You can increase the `max_shard_size` parameter to accommodate the size.r  )r   rq  rr  rh  mathprodshaper   
dtype_sizer  r   r   r+   r  ra   r  rL  r  rM  r  ru  rN  rq   r  r  )r7  r  rm  rh  weight_countsper_param_size
value_sizevalue_size_strmax_shard_size_strvariable_pathr-  s             r;   ru  zShardedH5IOStore.__setitem__  s   ((/))%++6		%++.$//6"^3}E
!?!!CC+++1*w2FGN!5##g-" se4'7 822D1E F  	:-""T%8%88JJL668DLIId))*&0D#C',,11 4 4\ BB'',,   .}= Cr   )r|   Nr   )
r.  r@  rA  rB  r8  r#  ra   r  ru  __classcell__)r-  s   @r;   r   r     s'    65p!2!(:" "r   r   c                   (    e Zd ZddZddZd Zd Zy)r]   Nc                     || _         || _        || _        |dk(  ri | _        y| j                  r|j	                  |d      | _        nt	        |d      | _        t        j                  | j
                        | _        y)a  Numerical variable store backed by NumPy.savez/load.

         If `archive` is specified, then `root_path` refers to the filename
        inside the archive.

        If `archive` is not specified, then `root_path` refers to the path of
        the npz file on disk.
        rJ   r   rK   r   N)r4  rL   rh   contentsr3   r:   rj  r  r6  s       r;   r8  zNpzIOStore.__init__;  sc     #	3;DM|| ic:id3GGDFFODMr   c                 ~    |si | j                   d<   | j                   d   S i | j                   |<   | j                   |   S N__root__)r  rV  s      r;   r  zNpzIOStore.makeP  s?    (*DMM*%==,, d}}T""r   c                     |s(d| j                   v rt        | j                   d         S i S || j                   v r| j                   |   j                         S i S r  )r  r  tolistr:  s     r;   r#  zNpzIOStore.getW  sS    T]]*DMM*566I4== ==&--//	r   c                 V   | j                   dk(  r| j                  r.| j                  j                  | j                  dd      | _        nt        | j                  d      | _        t        j                  | j                  fi | j                   | j                  j                          y )NrJ   T)rL   force_zip64r   rK   )	rL   rh   r3   r4  r:   rj  savezr  ra   r>  s    r;   ra   zNpzIOStore.close`  su    99||**NN$ +  dnn48HHTVV-t}}-r   r{  r   )r.  r@  rA  r8  r  r#  ra   rC  r   r;   r]   r]   :  s    ,*#	r   r]   c                  z    t        j                         } t        j                  |       }|j                          | S )Nri   )rs   mkdtempTemporaryFilera   )temp_dirtestfiles     r;   r   r   l  s/    !H%%(3HNNOr   c                    t        j                  d|  d       }||S t        dg      }| dk(  r&t               }|j	                  t        |             n| dk(  r*t               }|j	                  t        |      ddgz          n| dk(  r)t               }|j	                  t        |      dgz          n| d	k(  r=t               }t        g g       }|j	                  t        |      t        |      z          nu| d
k(  r7t        d      }|j	                  t        |             |j                  d       n9| dk(  r%t               }|j	                  t        |             nt        d| d      t        j                  d|  |       |S )Nsaving_attr_skiplist_$_self_unconditional_dependency_namesr   r   
operations_operationsr   _functionalr   r   g      ?	variablesr   z&get_attr_skipset got invalid obj_type=zi. Accepted values for `obj_type` are ['Layer', 'Functional', 'Sequential', 'Metric', 'Optimizer', 'Loss']saving_attr_skipset_)r   get_global_attributer`   r   updaterj   r   r   r   r%  r   r+   set_global_attribute)r  skipsetref_obj	ref_obj_a	ref_obj_bs        r;   r  r  s  sn   //
z*DG 2	
G
 7's7|$	\	!'s7||]&CCD	\	!'s7|}o56	X	H	"2r*	s9~I67	[	 C.s7|${#	V	&s7|$5H; 7# #
 	
 %%
xj)7 Nr   c                     t         d}nt        j                         j                  }t        | j                        |t
        z  k  S )zCheck if there is sufficient memory to load the model into memory.

    If psutil is installed, we can use it to determine whether the memory is
    sufficient. Otherwise, we use a predefined value of 1 GB for available
    memory.
    r  )psutilvirtual_memory	availabler   r  _MEMORY_UPPER_BOUND)r4   available_memorys     r;   rn   rn     sC     ~-!002<<5??+
0
0	1r   )r   T)NTTr?  )FN)F)FNNN)TrB  rB   r%   r@   r  r   ro   r   rs   r"   rk   rl  numpyrj  	keras.srcr   keras.src.backend.commonr   keras.src.layers.layerr   keras.src.losses.lossr   keras.src.metrics.metricr   keras.src.optimizers.optimizerr   "keras.src.saving.serialization_libr	   r
   r    keras.src.trainers.compile_utilsr   keras.src.utilsr   r   r   r   r   #keras.src.utils.model_visualizationr   keras.src.utils.summary_utilsr   r   keras.src.versionr   r>   r   r    r  r   rY   rX   _VARS_FNAMErZ   ru   r[   r  r   r<   rH   r-   r'   r,   r   r   r   r   r   r   r   r   r   r  r_   r   r  r$  r^   r\   r   r]   r   r  rn   rC  r   r;   <module>r     s   9  	   	         1 ( & + 4 A G E ; ' & $ " & ; > < :
 ! $ u$& ! "FAR
&DJ*Z8
v'T1hGV ;?7#v ;?;#|" $<<H `)F:!H8, 8,vt, t,n`y `F/ /d+\u+  D  F  Os6   &E +E 0E EEEEE&%E&