
    BVhm                     2   d 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ddddddddddddejD                  dddfdZ#d Z$d Z%d Z&d Z'd"dZ(d"dZ)d Z*d Z+ ejX                  e#ejD                        Z- ejX                  e#ej\                  d      Z/ ejX                  e#ej`                  d      Z1 G d  d!ejd                        Z3y# e!$ r dZ Y w xY w)#z>Part of the Keras training engine related to plain array data.    N)iterator_ops)context)errors)backend)	callbacks)distributed_training_utils_v1)training_utils_v1)make_batches)slice_arrays)ModeKeys)
tf_logging)data)nest)issparse   TFstepsc                    d|v r|j                  d      }|rt        d|      d}d}t        |t        j                  t        j
                  f      }|r4|d}t        j                  | ||||      }t        || j                        }| j                  rFt        j                  | j                  |t        j                  k(  rdnd	      }|j                          |xs |du}|du}|xs |}|r|r|}nt        | ||||      }|st!        |||      }n|}t#        | ||       t%        | |      }d}t        |t        j                  t        j
                  f      rR|t        j                  | |||d
      }t        || j                        }t        | ||	|
t        j&                        }|}n-|xr% t)        j*                  |      d   j,                  d   xs d}|t        j                  k(  r|rt/        |||       |rdnd} t1        j2                  || |||||| ||
      }t4        c|sag }!t7        | |      }"t9        t;        ||"            D ];  \  }#\  }$}%t5        |$      st=        j>                  |%      r+|!jA                  |#       = |t        jB                  k(  rt        jD                  ||rdn||      }&nt        jF                  ||rdn||      }&| jH                  rt        jJ                  | |       d|jL                  _'        |jQ                  |       | jS                  ||      }tU        ||      D ]  }'|jL                  jN                  r nsi }(|t        jB                  k7  r| jW                          |t        j                  k(  r|jY                  |'|(       |r/|tZ        j\                  })n|})d}*|*|)k  r|*dd}+|j_                  |d|*|+       	 ta        |      r!| j                  rt        jb                  |       s|},n |       }, ||,      }-t        |-tn              s|-g}-| j                  r"t        jp                  | j                  |-| |      }-|*dk(  r|&js                  |-       |&ju                  |-       t1        jv                  | |+|-|      }+|j_                  |d|*|+       |*dz  }*|jL                  jN                  rn|*|)k  rnt[        jx                  |      }.|dk(  rt        jz                  |.|      }.n!|rtZ        j|                  j                  |.       t        ||      }/t9        |/      D ]%  \  }0\  }1}2|.|1|2 }3t        |/      dk(  r|}4n9	 |r*t        |d   t              rt        |dd |3      |d   gz   }4nt        ||3      }4t4        !D ]  }#|4|#   j                         |4|#<    |0t        |3      d}+|j_                  |d|0|+        ||4      }-t        |-tn              s|-g}-|0dk(  r|&js                  |-       |&ju                  |-|1|2       t1        jv                  | |+|-|      }+|j_                  |d|0|+       |jL                  jN                  s& n |&j                          |&j                  }5t1        jv                  | |(|5|      }(t        |5      dk(  r|5d   }5|rt        j                  ||'      r|jL                  jN                  s| jH                  r$t        j                  | t        j                         t        | ||	|
|||dt        j&                  d|dud
      }6t        |6tn              s|6g}6t1        jv                  | |(|6|d      }(|r|'|dz
  k  rt        || j                         |t        j                  k(  r|j                  |'|(       |sm|'|dz
  k  swt        || j                          d| _L        |j                  |       | j                  r5| jH                  rt        j                  | |       j                  ddd       |t        j                  k(  r| j                  S 5S # td        jf                  $ rt |r@|r/d|jL                  _'        ti        jj                  d|||z  fz         n=|*dkD  r8|*}||&_6        n.d|jL                  _'        ti        jj                  d|||z  fz         Y ew xY w# t        $ r t        d      w xY w)a  Loop function for arrays of data with modes TRAIN/TEST/PREDICT.

  Args:
      model: Keras Model instance.
      inputs: Either a list or dictionary of arrays, or a dataset instance.
      targets: List/dictionary of input arrays.
      sample_weights: Optional list of sample weight arrays.
      batch_size: Integer batch size or None if unknown.
      epochs: Number of times to iterate over the data
      verbose: 0, 1, or 2. Verbosity mode.
        0 = silent, 1 = progress bar, 2 = one line per epoch.
        Note that the progress bar is not particularly useful when
        logged to a file, so verbose=2 is recommended when not running
        interactively (eg, in a production environment).
      callbacks: List of callbacks to be called during training
      val_inputs: Either a list or dictionary of arrays, or a dataset instance.
      val_targets: List/dictionary of target arrays.
      val_sample_weights: Optional list of sample weight arrays.
      shuffle: Whether to shuffle the data at the beginning of each epoch
        concatenation of list the display names of the outputs of `f` and the
        list of display names of the outputs of `f_val`.
      initial_epoch: Epoch at which to start training (useful for resuming a
        previous training run)
      steps_per_epoch: Total number of steps (batches of samples) before
        declaring one epoch finished and starting the next epoch. Ignored with
        the default value of `None`.
      validation_steps: Number of steps to run validation for (only if doing
        validation from data tensors). Ignored with the default value of
        `None`.
      validation_freq: Only relevant if validation data is provided. Integer or
        `collections.abc.Container` instance (e.g. list, tuple, etc.). If an
        integer, specifies how many training epochs to run before a new
        validation run is performed, e.g. `validation_freq=2` runs
        validation every 2 epochs. If a Container, specifies the epochs on
        which to run validation, e.g. `validation_freq=[1, 2, 10]` runs
        validation at the end of the 1st, 2nd, and 10th epochs.
      mode: One of ModeKeys.TRAIN/ModeKeys.TEST/ModeKeys.PREDICT.
      validation_in_fit: if true, then this method is invoked from within
        training iteration (for validation). In the case where `val_inputs` is
        a dataset, this flag indicates that its iterator and feed values are
        already created so should properly reuse resources.
      prepared_feed_values_from_dataset: if True, `inputs` is a list of feed
        tensors returned from `_prepare_feed_values` call on the validation
        dataset, so do not call it again on `inputs`. Should only be used for
        inline validation (i.e., only if `validation_in_fit` is also True).
      steps_name: The string name of the steps argument, either `steps`,
        `validation_steps`, or `steps_per_epoch`. Only used for error message
        formatting.
      **kwargs: Additional arguments for backwards compatibility.

  Returns:
      - In TRAIN mode: `History` object.
      - In TEST mode: Evaluation metrics.
      - In PREDICT mode: Outputs of the Model called on inputs.

  Raises:
      ValueError: in case of invalid arguments.
  r   zUnknown arguments: FNT)epochs
steps_namer   r   )strategylearning_phasevalidation_stepssamples)do_validation
batch_sizer   steps_per_epochr   
count_modeverbosemode)num_samplesr   )batchsizebeginzYour dataset ran out of data; interrupting training. Make sure that your dataset can generate at least `%s * epochs` batches (in this case, %d batches). You may need to use the repeat() function when building your dataset.zYour dataset iterator ran out of data; interrupting training. Make sure that your iterator can generate at least `%s * epochs` batches (in this case, %d batches). You may need touse the repeat() function when building your dataset.endr!   zPTypeError while preparing batch. If using HDF5 input data, pass shuffle="batch".)
targetssample_weightsr   r   r   r   r   validation_in_fit!prepared_feed_values_from_datasetr   val_)prefix)Ppop	TypeError
isinstance
data_types	DatasetV1	DatasetV2r	   infer_steps_for_dataset_get_iterator_distribution_strategyr   distributed_scoper   TRAIN	__enter___prepare_feed_values_get_num_samples_or_steps_update_sample_weight_mode_make_execution_functionTESTr   flattenshape_print_train_infocbksconfigure_callbacksr   _get_model_feed	enumeratezipr   	is_sparseappendPREDICTOutputsAggregatorMetricsAggregator_compile_distribution"_copy_weights_to_distributed_modelmodelstop_training_call_begin_hook#_maybe_load_initial_epoch_from_ckptrangereset_metricson_epoch_beginnpinf_call_batch_hookcallableis_distributing_by_cloningr   OutOfRangeErrorloggingwarningr   list_per_replica_aggregate_batchcreate	aggregate	make_logsarangebatch_shufflerandomshuffler
   lenintr   toarrayfinalizeresultsshould_run_validation_copy_weights_to_original_modelmodel_iteration_reinitialize_iteratoron_epoch_end_successful_loop_finish_call_end_hook__exit__history)7rL   inputsr&   r'   r   r   r   r   
val_inputsval_targetsval_sample_weightsrc   initial_epochr   r   validation_freqr   r(   r)   r   kwargsreset_dataset_after_each_epochinput_iterator
is_datasetscope	use_stepsr   insnum_samples_or_stepsfval_iteratorval_samples_or_stepsr   indices_for_conversion_to_densefeedi
input_datafeed_tensor
aggregatorepoch
epoch_logstarget_stepsstep
batch_logsactual_inputs
batch_outsindex_arraybatchesbatch_indexbatch_start	batch_end	batch_ids	ins_batchrh   val_resultss7                                                          a/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/engine/training_arrays_v1.pyrk   rk   *   s	   ` jj)O
v7
88 $) .&%//1E1EFH*
 '+$)AA
JPo"65+G+GHN !!);;--!X^^3=E 
OO7O47)D(- #V&<
 C
ufg~t
LC
 
4S*5DF + UD#.
 ud+! ,
Z11:3G3GHI
 +BB


') !U-I-IJL%|[*<hmmMJ , &  $,,++uQ+  (#'  
X^^*,@*M $w*&&!%"
) )&(#5$'D(1#c4.(A 2$$J	*	g&7&7&D'..q12
 
X"44+D1EJ
 #44+D1EJ
   !DDt #()//T";;M4P-]F+ yKe$$ Jx x~~uj1		 vv 'd<#Q/
""4$
C%	#5#?#?'D$>$>u(FMEM'*> *d+"|*''+HH..
E4I 
 19


J
'Z( ^^E:z4H
""4jA	??((
 <D II23k	G	'55k:N
		+&1:>g3<W3E -
/+/YI6	w<1
 )	5z#b'3/&s3Bx;s2wiGi&sI6i 2 2a$Q<//1IaL2  +C	NC
""4+zJ y\
*d+"|* !


J
'Zi@ ^^E:z4H
""4ZH??((
[-^   Gz7DAJ
7|q
g 	//GOO))		$	$ 	&EE8>>	# $

+*}} -9-E')k T*"m>>
[$v?j	%&1*,|U-I-IJx~~UJ/ &%&1**<^U-I-IJsyKv #'%4 
!!""#CCE4P	NN4t$	X^^==	.C %% 	
 .2ioo+oo+
  6!9:;<  $o!0j -1IOO)OO
 )/F*BCDE 9	T  5 4 5 55s   >a8cBc	c	c!c                     |t         j                  k(  r| j                  }|S | j                  | j                  z   | j                  z   }|S N)r   rG   _feed_inputs_feed_targets_feed_sample_weights)rL   r   r   s      r   rB   rB     sM    	XD 
+ 	U00053M3MM 		+    c                 |    |rdnd}dj                  | |      }|r|dj                  ||      z  }t        |       y )Nr   r   zTrain on {0} {increment})	incrementz, validate on {0} {increment})formatprint)r   r   r{   r   msgs        r   r?   r?     sQ    #g)"))i 	* 	1#*11	 2 3 3C*r   c                 :    |r|S t        j                  | ||d      S )zGReturns total number of samples (when training in batch mode) or steps.r   )r	   check_num_samples)r~   r   r   s      r   r9   r9     s)    		,	,S*o->
@ @r   c                      j                   rpt        t        j                  t        j                  f      r t        j                   j                          fd}t        j                         r|S  |       S t        t        j                  t        j                  t        j                  f      r j                  d      \  t        j                        j                         t        xs g       t        xs g       z   z   }t         j"                  k(  r(t        t%        j&                         t(              s|dgz  }|S )a|  Prepare feed values to the model execution function.

  Args:
    model: Model to prepare feed values for.
    inputs: List or dict of model inputs.
    targets: Optional list of model targets.
    sample_weights: Optional list of sample weight arrays.
    mode: One of ModeKeys.TRAIN/ModeKeys.TEST/ModeKeys.PREDICT.

  Returns:
    Feed values for the model in the given mode.
  c                  6    t        j                         S r   )r   r8   )rr   r   rL   r'   r&   s   r   get_distributed_inputsz4_prepare_feed_values.<locals>.get_distributed_inputs  s!    *??
.$8 8r   T)extract_tensors_from_dataset)r4   r.   r/   r0   r1   r   get_iteratorr   executing_eagerlyr   Iterator_standardize_user_datar	   ModelInputsas_listr[   r   r6   r   symbolic_learning_phasere   )rL   rr   r&   r'   r   r   r~   s   `````  r   r8   r8     s.    !!&://1E1EFG,99
%..0f8 8   "###%%--z/C/C%..0 1&+&B&B%) 'C '+#FG^ ((088:&B',"-.>)#	X^^J%%'%.D6MC	*r   c                 \    |rt        j                  | |      S t        j                  |       S r   )r   r   r	   )rr   distribution_strategys     r   r3   r3     s/    (55%' '		'	'	//r   c                 `    |rt        j                  | |       y t        j                  |        y r   )r   initialize_iteratorr	   )iteratorr   s     r   rl   rl     s)    !55') ))(3r   c                 h    | j                   rt        j                  | |      S | j                  |      S )z2Makes function to run one step of model execution.)r4   r   r;   )rL   r   s     r   r;   r;   #  s/    
!!(AA%NN		'	'	--r   c                    |t         j                  k(  ryd}t        |      s|t        | j                        t        | j
                        z   d }|t         j                  k(  xr# t        t        j                         t               }|r|dd }| j                  |       | j                  rt        j                  | ||       yy)z0Updates the sample_weight_mode of a given model.Nr%   )r'   )r   rG   rV   rd   r   r   r6   r.   r   r   re   _update_sample_weight_modesr4   r   )rL   r   rr   r'   has_learning_phase_pls        r   r:   r:   *  s     
X
.
 
&	C 2 23c%:M:M6NNOPN!X^^3 1!+G,K,K,M,/"1 1  %cr*n	%%^%D !!!==eT>LN "r   )r   )r   rc   c                   \    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 ddZ	 	 	 	 ddZy)	ArrayLikeTrainingLoopa3  TrainingLoop that handle inputs like array.

  This is the default handler for most of the input data types, includes
  symbolic tensors or Numpy array-like, Datasets and iterators in graph mode
  (since they generate symbolic tensors). This Function is used to handle model
  with `run_eagerly` = False.
  Nc                    |j                  |||      }|j                  |||||dd|||

      \  }}}|	r|j                  |	||      \  }}}nC|r.d|cxk  rdk  r#n n t        j                  ||||      \  }}}}}}n|rt        d      d\  }}}t        |fi d|d	|d
|d|d|d|d|d|d|d|d|
d|d|d|d|ddS )NTr   )sample_weightclass_weightr   check_stepsr   r   validation_splitrc           g      ?zH`validation_steps` should not be specified if `validation_data` is None.)NNNrr   r&   r'   r   r   r   r   rs   rt   ru   rc   rv   r   rw   r   )_validate_or_infer_batch_sizer   _prepare_validation_datar	   "split_training_and_validation_data
ValueErrorfit_loop)selfrL   xyr   r   r   r   r   validation_datarc   r   r   rv   r   r   rw   rx   r'   val_xval_yru   s                         r   fitzArrayLikeTrainingLoop.fitV  s   $ 44Z5DaIJ !77		#!$) 8 
Aq. ).)G)G
:'7*9&eU&	b#38b8

>
>
Q 02q!^UE+= 
 6 7 	7)9&eU&&& & &	&
 & & & & & & .& & $& (& *&  (!&" %#& &r   c	           
          |j                  |||      }|j                  ||||dd|      \  }}}
t        ||||
||||      S )NTr   )r   r   r   r   r   )rr   r&   r'   r   r   r   r   )r   r   	test_loop)r   rL   r   r   r   r   r   r   r   rx   r'   s              r   evaluatezArrayLikeTrainingLoop.evaluate  sp     44ZJJ 77		# 8 Aq. % r   c                 |    |j                  |||      }|j                  |dd|      \  }}}t        ||||||      S )NTr   )r   r   r   )r   r   r   r   )r   r   predict_loop)	r   rL   r   r   r   r   r   rx   _s	            r   predictzArrayLikeTrainingLoop.predict  s[     44ZJJ**	tu + >GAq!	 r   )NNNr   r   Nr   NTNNr   NNr   )NNNr   NNN)Nr   NN)__name__
__module____qualname____doc__r   r   r    r   r   r   r   M  sl     !?&F !@ r   r   r   )4r   	functoolsnumpyrS   tensorflow.python.data.opsr   tensorflow.python.eagerr   tensorflow.python.frameworkr   tensorflow.python.kerasr   r   r@   "tensorflow.python.keras.distributer   tensorflow.python.keras.enginer	   +tensorflow.python.keras.utils.generic_utilsr
   r   'tensorflow.python.keras.utils.mode_keysr   tensorflow.python.platformr   rY   tensorflow.python.typesr   r/   tensorflow.python.utilr   scipy.sparser   ImportErrorr6   rk   rB   r?   r9   r8   r3   rl   r;   r:   partialr   r<   r   rG   r   TrainingLoopr   r   r   r   <module>r      s2   E   3 + . + 5 L < D D < < 6 '# !#'#"# $'+ "#$(%)$%!&+6;&'Zz@/d04.N8 9_8>>BI(--8	 y  (**E;x-:: xO  (s   D DD