o
    i ex                     @   sx  d Z ddlZddlZddlm  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 zddlmZ W n ey\   dZY nw ddddddddddddddejdddfddZdd Zdd Z dd Z!dd Z"d'ddZ#d'ddZ$dd  Z%d!d" Z&ej'eejd#Z(ej'eej)dd$Z*ej'eej+dd$Z,G d%d& d&ej-Z.dS )(z>Part of the Keras training engine related to plain array data.    N)backend)	callbacks)distributed_training_utils_v1)training_utils_v1)io_utils)make_batches)slice_arrays)ModeKeys)
tf_logging)issparse   TFstepsc           7      K   sD  d|v r	| d}|rtd| d}d}t|tjjjjtjjf}|r;|du r5d}tj	| ||||d}t
|| j}| jrQtj| j|tjkrIdndd	}|  |pV|du}|du}|p^|}|rf|rf|}nt| ||||}|swt|||}n|}t| || t| |}d}t|tjjjjtjjfr|du rtj	| |||d
d}t
|| j}t| ||	|
tj}|}n|rtj|d jd pd}|tjkr|rt||| |rdnd} tj|| |||||| ||d
}tdur|sg }!t| |}"tt||"D ]\}#\}$}%t|$rt !|%s|!"|# q|tj#kr%tj$||rdn||d}&ntj%||r-dn||d}&| j&r<t'| | d|j(_)|*| | +||}t,||D ](}'|j(j)r[ ni }(|tj#krg| -  |tjkrs|.|'|( |r2|du rt/j0})n|})d}*|*|)k r1|*dd}+|1|d|*|+ zt2|r| jrt3| s|},n| },||,}-W n< tj4j5y   |r|rd|j(_)t67d||| f  n|*dkr|*}||&_8nd|j(_)t67d||| f  Y nEw t|-t9s|-g}-| jrt:| j|-| |}-|*dkr|&;|- |&<|- |=| |+|-|}+|1|d|*|+ |*d7 }*|j(j)r,n|*|)k snt/>|}.|dkrCt?|.|}.n	|rLt/j@A|. tB||}/t|/D ]\}0\}1}2|.|1|2 }3tC|/dkrk|}4n.z!|rt|d tDrtE|dd |3|d g }4ntE||3}4W n ty   tdw tdur|!D ]}#|4|# F |4|#< q|0tC|3d}+|1|d|0|+ ||4}-t|-t9s|-g}-|0dkr|&;|- |&<|-|1|2 |=| |+|-|}+|1|d|0|+ |j(j)r nqU|&G  |&jH}5|=| |(|5|}(tC|5dkr|5d }5|r\tI||'r\|j(j)s\| j&r&tJ| tj tK| ||	|
|||dtjd|dud
d}6t|6t9sB|6g}6|j=| |(|6|dd}(|r\|'|d k r\tL|| j |tjkrh|M|'|( |rx|'|d k rxtL|| j qPd| _N|O| | jr| j&rtJ| | |Pddd |tjkr| jQS |5S )a8  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 to use 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)Rpop	TypeError
isinstancetfcompatv1dataDatasetr   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TESTnest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_cloningerrors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)7rI   inputsr    r!   r   r   r   r   
val_inputsval_targetsval_sample_weightsra   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_batchrf   val_results r   R/var/www/myenv/lib/python3.10/site-packages/keras/src/engine/training_arrays_v1.pyri   )   s  S











	
#






M









ri   c                 C   s*   |t jkr
| j}|S | j| j | j }|S N)r	   rD   _feed_inputs_feed_targets_feed_sample_weights)rI   r   r   r   r   r   r?     s   
r?   c                 C   sB   |rdnd}d|  d| }|r|d| d| 7 }t | d S )Nr   r   z	Train on  z, validate on )r   	print_msg)r}   r   ry   	incrementmsgr   r   r   r<   	  s
   r<   c                 C   s   |r|S t | ||dS )zEReturns total number of samples when training in batch mode or steps.r   )r   check_num_samples)r|   r   r   r   r   r   r5     s
   r5   c                    s   j r*t tjjjjtjjfrt j   fdd}t	 r'|S | S t tjjjjtjjtjjjj
frFj dd\ t   tpQg tpWg    }tjkrptt tsp|dg7 }|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                      s   t  S r   )r   r4   r   rp   r   rI   r!   r    r   r   get_distributed_inputs-  s   
z4_prepare_feed_values.<locals>.get_distributed_inputsT)extract_tensors_from_dataset)r0   r(   r)   r*   r+   r,   r-   r   get_iteratorexecuting_eagerlyIterator_standardize_user_datar   ModelInputsas_listrY   r	   r2   r   symbolic_learning_phaserc   )rI   rp   r    r!   r   r   r|   r   r   r   r4     s8   


r4   c                 C   s   |rt | |S t| S r   )r   r   r   )rp   distribution_strategyr   r   r   r/   U  s
   
r/   c                 C   s"   |r
t | | d S t|  d S r   )r   initialize_iteratorr   )iteratorr   r   r   r   rj   ]  s
   rj   c                 C   s   | j r	t| |S | |S )z2Makes function to run one step of model execution.)r0   r   r7   )rI   r   r   r   r   r7   f  s
   
r7   c                 C   s   |t jkrdS d}t|s6|t| jt| j d }|t jko'tt	 t
 }|r0|dd }| j|d | jrBt| || dS dS )z0Updates the sample_weight_mode of a given model.Nr   )r!   )r	   rD   rS   rb   r   r   r2   r(   r   r   rc   _update_sample_weight_modesr0   r   )rI   r   rp   r!   has_learning_phase_plr   r   r   r6   o  s$   
r6   )r   )r   ra   c                   @   sb   e Zd ZdZ															dddZ							dd	d
Z				dddZdS )ArrayLikeTrainingLoopa=  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.
    Nr           Tr   c                 K   s  | |||}|j|||||dd|||
d
\}}}|	r&||	||\}}}n(|rCd|  k r2dk rCn nt||||\}}}}}}n|rIt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_splitra   r   g      ?zH`validation_steps` should not be specified if `validation_data` is None.)NNNrp   r    r!   r   r   r   r   rq   rr   rs   ra   rt   r   ru   r   )_validate_or_infer_batch_sizer   _prepare_validation_datar   "split_training_and_validation_data
ValueErrorfit_loop)selfrI   xyr   r   r   r   r   validation_datara   r   r   rt   r   r   ru   rv   r!   val_xval_yrs   r   r   r   fit  s   
	
zArrayLikeTrainingLoop.fitc	              
   K   sD   | |||}|j||||dd|d\}}}
t||||
||||dS )NTr   )r   r   r   r   r   )rp   r    r!   r   r   r   r   )r   r   	test_loop)r   rI   r   r   r   r   r   r   r   rv   r!   r   r   r   evaluate  s(   	zArrayLikeTrainingLoop.evaluatec           	      K   s:   | |||}|j|dd|d\}}}t||||||dS )NTr   )r   r   r   )r   r   r   r   )r   r   predict_loop)	r   rI   r   r   r   r   r   rv   _r   r   r   predict  s   
zArrayLikeTrainingLoop.predict)NNNr   r   Nr   NTNNr   NNr   )NNNr   NNN)Nr   NN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r     s>    
S
%r   r   )/r   	functoolsnumpyrP   tensorflow.compat.v2r*   v2r)   	keras.srcr   r   r=   keras.src.distributer   keras.src.enginer   keras.src.utilsr   keras.src.utils.generic_utilsr   r   keras.src.utils.mode_keysr	   tensorflow.python.platformr
   rW   scipy.sparser   ImportErrorr2   ri   r?   r<   r5   r4   r/   rj   r7   r6   partialr   r8   r   rD   r   TrainingLoopr   r   r   r   r   <module>   sp   
   W	
;
		!