o
    2h9                     @   s8  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 G dd dej Z!G dd deZ"dd Z#dd Z$d*ddZ%dd Z&dd Z'dd Z(dd Z)d d! Z*d"d# Z+d$d% Z,d&d' Z-d(d) Z.dS )+    N)context)	callbacks)metrics)
optimizers)tree)config)loss)trainer)array_slicing)data_adapter_utils)EpochIterator)traceback_utilsc                       sN  e Zd Z fddZedd Zedd Zejdd Zdd	 Zd
d Z	dd Z
dd Zd5ddZd5ddZd5ddZej																d6ddZej								d7dd Zej	d8d!d"Z				d9d#d$Z			d:d%d&Zd'd( Zed)d* Zd;d+d,Z	d<d-d.Zd;d/d0Zd<d1d2Z fd3d4Z  ZS )=TensorFlowTrainerc                    sF   t    d | _d | _d | _d| _tj rtj	 | _
d S d | _
d S N   )super__init__train_functiontest_functionpredict_functionunrolled_steps_per_executiontf
distributehas_strategyget_strategy_distribute_strategyself	__class__ _/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/trainer.pyr      s   


zTensorFlowTrainer.__init__c                 C   s   | j ptj S N)r   r   r   r   r   r    r    r!   distribute_strategy(   s   z%TensorFlowTrainer.distribute_strategyc                 C   s
   | j pdS )Nauto_distribute_reduction_methodr   r    r    r!   distribute_reduction_method,      
z-TensorFlowTrainer.distribute_reduction_methodc                 C   s
   || _ d S r"   r%   )r   valuer    r    r!   r'   0   r(   c           
      C   s   t |\}}}t @}| jr| |dd}n| |}| j||||dd}| jjt	|t
t|d d d | jd urE| j|}W d    n1 sOw   Y  | jrj| j}|||}	| jt|	| ntd | j||||dS )NTtrainingxyy_predsample_weightr+   r   r0   z.The model does not have any trainable weights.)r   unpack_x_y_sample_weightr   GradientTape_call_has_training_arg_compute_loss_loss_trackerupdate_stateloss_moduleunscale_loss_for_distributionshaper   flatten	optimizer
scale_losstrainable_weightsgradientapply_gradientszipwarningswarncompute_metrics)
r   datar-   r.   r0   taper/   r   r>   	gradientsr    r    r!   
train_step4   s4   


zTensorFlowTrainer.train_stepc                 C   s|   t |\}}}| jr| |dd}n| |}| j||||dd}| jjt|t	t
|d d d | j||||dS )NFr*   r,   r   r1   )r   r2   r4   r5   r6   r7   r8   r9   r   r:   r   r;   rD   )r   rE   r-   r.   r0   r/   r   r    r    r!   	test_stepW   s   
zTensorFlowTrainer.test_stepc                 C   s2   t |\}}}| jr| |dd}|S | |}|S )NFr*   )r   r2   r4   )r   rE   r-   _r/   r    r    r!   predict_stepf   s   zTensorFlowTrainer.predict_stepc                    sp   t jjjfddjst jdjdt jjjfdd js.t j dd  fdd	}|S )
Nc                    s&    j j| fd}t| j dd}|S )z/Runs a single training step on a batch of data.argsr$   	reductionr#   runreduce_per_replicarE   outputs)r   step_functionr    r!   one_step_on_datao   s   z:TensorFlowTrainer._make_function.<locals>.one_step_on_dataTreduce_retracingjit_compilec                    s   j dkrtjj S tjjd  fdd} fddfdd}td} }tj	||| |gd	\}}} j
|_|S )
Nr   c                    s   t t |  j| S r"   )r   logical_andlesssteps_per_execution	has_value)execution_stepoptional_outputsnext_optional_inputsr   r    r!   cond   s   zNTensorFlowTrainer._make_function.<locals>.multi_step_on_iterator.<locals>.condc              	      sx    fdd}fdd}t t t | j  ||}| d |t t | d jfdd fddfS )	Nc                     s"   t jj } | j _| S r"   )r   experimentalOptional
from_value	get_valueelement_spec_element_spec)next_optional_outputs)empty_outputsr`   rV   r    r!   has_next   s   
zfTensorFlowTrainer._make_function.<locals>.multi_step_on_iterator.<locals>.inner_body.<locals>.has_nextc                      s    j _ S r"   )rg   r    )ri   r_   r    r!   no_has_next   s   ziTensorFlowTrainer._make_function.<locals>.multi_step_on_iterator.<locals>.inner_body.<locals>.no_has_nextr   c                      s      S r"   )get_next_as_optionalr    iteratorr    r!   <lambda>   s    zfTensorFlowTrainer._make_function.<locals>.multi_step_on_iterator.<locals>.inner_body.<locals>.<lambda>c                      s    S r"   r    r    )r`   r    r!   ro      s    )r   ra   rZ   r[   r\   r]   )r^   r_   r`   rj   rk   rh   )ri   rn   rV   r   )r`   r_   r!   
inner_body   s$   	


zTTensorFlowTrainer._make_function.<locals>.multi_step_on_iterator.<locals>.inner_bodyc                    s4   t tjjD ]} | ||\} }}q	| ||fS r"   )rangeminr   r\   )r^   r_   r`   rJ   )rp   r   r    r!   body   s   

zNTensorFlowTrainer._make_function.<locals>.multi_step_on_iterator.<locals>.bodyr   )	loop_vars)r\   r   rb   rc   rd   get_nextemptyconstantrl   
while_looprf   rg   )rn   ra   rs   r^   r`   rJ   final_optional_outputsrV   r   )ri   rp   rn   r!   multi_step_on_iterator   s"   

%
z@TensorFlowTrainer._make_function.<locals>.multi_step_on_iteratorrX   c                    sX   t | tjjtjjfr | }| st| S t	t
j| D ]\}}|}q!|S r"   )
isinstancer   rE   Iteratorr   DistributedIteratorr]   StopIterationre   rA   rq   r\   )rn   opt_outputssteprE   rT   )r{   rV   r   r    r!   function   s   

z2TensorFlowTrainer._make_function.<locals>.function)r   	autographrb   do_not_convertrun_eagerlyr   rY   )r   rU   r   r    )r{   rV   r   rU   r!   _make_functionn   s    
Qz TensorFlowTrainer._make_functionFc                 C   &   | j d ur
|s
| j S | | j| _ d S r"   )r   r   rH   r   forcer    r    r!   make_train_function      z%TensorFlowTrainer.make_train_functionc                 C   r   r"   )r   r   rI   r   r    r    r!   make_test_function   r   z$TensorFlowTrainer.make_test_functionc                    s   j d ur
|s
j S tjjjfdd js#jr#tj ddd tjjj fddtjjjfdd}jd	krB|}n}jsNtj|dd
}|_ d S )Nc                    s
     | S )z,Runs a predict test step on a batch of data.)rK   )rE   r   r    r!   rV      s   
zATensorFlowTrainer.make_predict_function.<locals>.one_step_on_dataTrW   c                    s.   | d } j j | fd}t|j dd}|S )Nr   rL   concatrN   rP   rS   rz   r    r!   one_step_on_data_distributed  s   zMTensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_distributedc                    sB    | d d }| dd  D ]} |g}t dd ||}q|S )Nr   c                 S   s   t | |gS r"   )r   )t1t2r    r    r!   ro     s    zUTensorFlowTrainer.make_predict_function.<locals>.multi_step_on_data.<locals>.<lambda>)r   map_structure)rE   rT   single_step_datastep_outputs)r   r    r!   multi_step_on_data  s   

zCTensorFlowTrainer.make_predict_function.<locals>.multi_step_on_datar   r|   )	r   r   r   rb   r   r   rY   r   r\   )r   r   r   r   r    )rV   r   r   r!   make_predict_function   s(   
	
z'TensorFlowTrainer.make_predict_functionNr   r$           Tr   c                 C   s~  |  d t }|r||k rtd|  |}d | _|r0|d u r0tj|||f|d\\}}}}|d ur<t	|\}}}t
||||||	|
| j| jd	}| j|d |  t|tjsjtj|d|dk|||j| d}d	| _|   |  d }i }| jp}|}t||D ]}|   || | % |D ]\}}}|| | |}||| | jr nqW d    n1 sw   Y  t| |}|d ur|  ||rt!| d
d d u rt
||||p|| j| j|d	d| _| j"||||p|||ddd}dd |# D }|$| |%|| |}| jr nqt| j&t'j(r+|dkr+| j&)| j* t!| d
d d ur6| `|j+|d | j,S )NfitzLimiting epochs to %d)validation_split)	r-   r.   r0   
batch_sizesteps_per_epochshuffleclass_weightr#   r\   rm   Tr   )add_historyadd_progbarverboseepochsstepsmodelF_eval_epoch_iterator)r-   r.   r0   r   r#   r\   r   r   )r-   r.   r0   r   r   r   return_dict_use_cached_eval_datasetc                 S   s   i | ]	\}}d | |qS )val_r    ).0namevalr    r    r!   
<dictcomp>  s    z)TensorFlowTrainer.fit.<locals>.<dictcomp>)logs)-_assert_compile_calledr   
max_epochsrB   rC   r   r
   train_validation_splitr   r2   TFEpochIteratorr#   r\   _maybe_symbolic_buildresetr}   callbacks_moduleCallbackListnum_batchesstop_trainingr   on_train_begin_initial_epochrq   reset_metricson_epoch_begincatch_stop_iterationon_train_batch_beginr   on_train_batch_enddict_get_metrics_result_or_logs_should_evalgetattrevaluateitemsupdateon_epoch_endr<   optimizers_module	Optimizerfinalize_variable_valuesr>   on_train_endhistory)r   r-   r.   r   r   r   r   r   validation_datar   r   r0   initial_epochr   validation_stepsvalidation_batch_sizevalidation_freqr   val_xval_yval_sample_weightepoch_iteratortraining_logsr   epoch
begin_stepend_steprn   
epoch_logsval_logsr    r    r!   r   $  s   







	



zTensorFlowTrainer.fitc	              
   K   s6  |  d |	dd}
|	rtd|	 |
r| j}nt|||||d| j| jd}| j|d |  t	|t
jsFt
j||dk|d|j| d	}|   d| _|  i }|   | % |D ]\}}}|| | |}||| | jrx nq^W d    n1 sw   Y  | |}|| |r|S | |S )
Nr   r   FzArguments not recognized: )r-   r.   r0   r   r   r   r#   r\   rm   r   r   r   r   r   r   r   )r   pop
ValueErrorr   r   r#   r\   r   r   r}   r   r   r   r   stop_evaluatingon_test_beginr   r   on_test_batch_beginr   on_test_batch_endr   on_test_end_flatten_metrics_in_order)r   r-   r.   r   r   r0   r   r   r   kwargsuse_cached_eval_datasetr   r   r   r   rn   r    r    r!   r     s^   
	





zTensorFlowTrainer.evaluatec                    s  t |||d j jd}t|tjs tj||dk|d|j d}dd } fdd	}   d _|	  d }	|
 0 |D ]%\}
}}||
 ||} |}|||	}	||d
|i  jrc nq>W d    n1 snw   Y  |  t|t|	}	tt|	S )NF)r-   r   r   r   r#   r\   r   r   r   c                 S   s4   |d u rt dd | }|S t | dd ||  |S )Nc                 S   s   | gS r"   r    )batch_outputr    r    r!   ro     s    zFTensorFlowTrainer.predict.<locals>.append_to_outputs.<locals>.<lambda>c                 S   s
   |  |S r"   )append)outputr   r    r    r!   ro        
 )r   r   map_structure_up_to)batch_outputsrT   r    r    r!   append_to_outputs  s   z4TensorFlowTrainer.predict.<locals>.append_to_outputsc                    s~   g }t  jD ]5}zt| }W n' ttjjfy6 } zt|dr0t|dkr0|W  Y d}~  S |d}~ww |	| q|S )z$Returns data for the next execution.__len__r   N)
rq   r\   nextr   r   errorsOutOfRangeErrorhasattrlenr   )rn   rE   rJ   r   er   r    r!   get_data  s   	z+TensorFlowTrainer.predict.<locals>.get_datarT   )r   r#   r\   r}   r   r   r   r   stop_predictingon_predict_beginr   on_predict_batch_beginr   on_predict_batch_endon_predict_endr   r   potentially_ragged_concatr   convert_to_np_if_not_ragged)r   r-   r   r   r   r   r   r   r   rT   r   r   rn   rE   r   r    r   r!   predict  sT   
	



zTensorFlowTrainer.predictc                    s   |  d |d ur d urtd  d| t| | j fd |    fdd}| | }tdd |}|rD|S | 	|S )	Ntrain_on_batchzkArguments `sample_weight` and `class_weight` cannot be specified at the same time. Received: sample_weight=z, class_weight=
data_batchc                   3        fV  d S r"   r    r    r0   r-   r.   r    r!   rE   \     z.TensorFlowTrainer.train_on_batch.<locals>.datac                 S   
   t | S r"   nparrayr-   r    r    r!   ro   `  r   z2TensorFlowTrainer.train_on_batch.<locals>.<lambda>)
r   r   r   class_weight_to_sample_weightsr   r   r   r   r   r   )r   r-   r.   r0   r   r   rE   r   r    r   r!   r   C  s*   

z TensorFlowTrainer.train_on_batchc                    sb   |  d  fdd}| j fd |   | | }tdd |}|r,|S | |S )Ntest_on_batchc                   3   r   r"   r    r    r   r    r!   rE   n  r  z-TensorFlowTrainer.test_on_batch.<locals>.datar   c                 S   r  r"   r  r  r    r    r!   ro   v  r   z1TensorFlowTrainer.test_on_batch.<locals>.<lambda>)r   r   r   r   r   r   r   )r   r-   r.   r0   r   rE   r   r    r   r!   r  e  s   

zTensorFlowTrainer.test_on_batchc                 C   s&   |    | |fg}tt|}|S r"   )r   r   r   r   r   )r   r-   r   r    r    r!   predict_on_batch{  s   z"TensorFlowTrainer.predict_on_batchc                    s   G  fddd}| S )Nc                       s   e Zd Zd fdd	ZdS )zDTensorFlowTrainer.compiled_metrics.<locals>.DeprecatedCompiledMetricNc                    s    j |||dS )Nr1   )_compiled_metrics_update_state)rJ   r.   r/   r0   r   r    r!   r7     s   zQTensorFlowTrainer.compiled_metrics.<locals>.DeprecatedCompiledMetric.update_stater"   )__name__
__module____qualname__r7   r    r   r    r!   DeprecatedCompiledMetric  s    r  r    )r   r  r    r   r!   compiled_metrics  s   z"TensorFlowTrainer.compiled_metricsc                 C   sJ   t jddd | jD ]}t|tjr|j||d q
|j|||d q
d S )Nz`model.compiled_metrics()` is deprecated. Instead, use e.g.:
```
for metric in self.metrics:
    metric.update_state(y, y_pred)
```
   )
stacklevelr1   )rB   rC   r   r}   metrics_moduleMeanr7   )r   r.   r/   r0   metricr    r    r!   r
    s   
	z0TensorFlowTrainer._compiled_metrics_update_statec                 C      t d | jd |||dS )Nzp`model.compiled_loss()` is deprecated. Instead, use `model.compute_loss(x, y, y_pred, sample_weight, training)`.r-   r.   r/   r0   rB   rC   compute_loss)r   r.   r/   r0   regularization_lossesr    r    r!   compiled_loss  s   zTensorFlowTrainer.compiled_lossc                 C   r  )Nzg`model.loss()` is deprecated. Instead, use `model.compute_loss(x, y, y_pred, sample_weight, training)`.r  r  )r   r.   r/   r0   r    r    r!   r     s   zTensorFlowTrainer.lossc                 C   s   | j d u rd S |d ur2|D ]$\}}}t|}tdd |}tt|r/| jjd|d d}n|} | j  | j	|d W d    d S 1 sIw   Y  d S )Nc                 S   s   t | tjjS r"   )r}   r   r   DistributedValuesr  r    r    r!   ro     s    z9TensorFlowTrainer._maybe_symbolic_build.<locals>.<lambda>MEANaxisr   )
r   r   r   r   allr;   r#   reducescope_symbolic_build)r   rn   r   rJ   itmaybe_distributed_data_batchhas_distributed_valuesr    r    r!   r     s(   
"z'TensorFlowTrainer._maybe_symbolic_buildc                    s   t  |}t|S r"   )r   _aggregate_additional_lossr8   scale_loss_for_distribution)r   r   r   r    r!   r&    s   
z,TensorFlowTrainer._aggregate_additional_loss)F)NNNr   r$   Nr   NTNNr   NNNr   )NNNr$   NNNF)Nr$   NN)NNNF)NNFr"   )NN)r  r  r  r   propertyr#   r'   setterrH   rI   rK   r   r   r   r   r   filter_tracebackr   r   r   r   r  r	  r  r
  r  r   r   r&  __classcell__r    r    r   r!   r      s    


#
{

1 BQ
%
	

	


	r   c                       sH   e Zd Zd fdd	Zdd Zdd Zdd	 Zej fd
dZ	  Z
S )r   Nc                    sF   t  j|i | || _| j }t|tjjs| j	|}|| _
d S r"   )r   r   r   data_adapterget_tf_datasetr}   r   r   DistributedDatasetexperimental_distribute_dataset_distributed_dataset)r   r#   rM   r   datasetr   r    r!   r     s   

zTFEpochIterator.__init__c                 C   s   | j S r"   )r0  r   r    r    r!   _get_iterator  s   zTFEpochIterator._get_iteratorc                 C   s   t   d S r"   )
tf_context
async_waitr   r    r    r!   tf_sync     zTFEpochIterator.tf_syncc                 C   
   t | jS r"   )r   _epoch_iteratorr   r    r    r!   __next__     
zTFEpochIterator.__next__c              	   #   s\    t    z	dV  |   W n tjjy   tw W d   dS 1 s'w   Y  dS )z1Catches errors when an iterator runs out of data.N)r   r   r5  r   r   r   r   r   r   r    r!   r     s   "z$TFEpochIterator.catch_stop_iterationr"   )r  r  r  r   r2  r5  r9  
contextlibcontextmanagerr   r+  r    r    r   r!   r     s    
r   c                    s:    dkrt tjjrd nd  fdd}t|| S )a	  Attempt to reduce the structure `values` to single values.

    Given `values` (a `tf.Tensor` or a `PerReplica` structure),
    which represents the values across all the replicas, `reduce_per_replica`
    attempts to "reduce" those values and returns the corresponding structure
    that represents only single values.

    Currently, `reduce_per_replica` is only used for reducing the metric results
    from `tf.distribute.Strategy.run()`. Depending on the underlying
    `Strategy` implementation, `values` may be a `PerReplica` object,
    which can be thought of as a collection of values across the replicas,
    or a `tf.Tensor`, if the strategy has already conducted the reduction
    for the downstream library.

    There are five possible outcomes of reduction:

    1) if the `values` is a structure of simple `tf.Tensor`s, meaning that
       reduction is not actually needed, `reduce_per_replica` returns the
       structure as-is.
    2) else, if `reduction="auto"`, then the best reduction strategy is
       chosen based on the current environment. This should only be used
       for training cases (`fit()`).
    3) else, if `reduction="first"`, then `reduce_per_replica`
       returns the values of the first replica. This is used in the case of
       training and evaluation, where `values` is expected to hold the same
       value across the replicas as a result of `Strategy`'s synchronization
       across the replicas.
       `reduce_per_replica` does not synchronize the values.
    4) else, if `reduction="sum"`, then `reduce_per_replica` returns the sum
       of values for all replicas. This may be used in the custom training loop
       case, where each replica contain different values which are not
       synchronized.
    5) else, if `reduction="concat"`, then `reduce_per_replica`
       returns the concatenation of the values across the replicas, along the
       axis of dimension 0. This is used in the inference case (`predict()`).

    Args:
        values: Structure of `PerReplica` objects or `tf.Tensor`s.
            `tf.Tensor`s are returned as-is.
        strategy: `tf.distribute.Strategy` object.
        reduction: One of `"auto"`, `"first"`, `"concat"`, `"mean"`, or `"sum"`.
            `"auto"` will select `"first"` when used under a TPUStrategy, or
            `"mean"` otherwise.

    Returns:
        Structure of `Tensor`s, representing the result of reduction.
    r$   firstmeanc                    s   t r# dkrt| S  dkrd| S  dkr#jd| ddS t| s)| S  dkr4| d S  dkrHtrAt| S t| S  dkrTt	| S  dkrbtj
| ddS td	  d
)z$Reduce a single `PerReplica` object.r   sumSUMr>  r  r   r  r=  z\`reduction` must be one of "first", "concat", "mean", "sum", or "auto". Received: reduction=.)#_collective_all_reduce_multi_worker_multi_worker_concatr   _is_per_replica_instanceexperimental_local_results_is_tpu_multi_host_tpu_multi_host_concatr   r   
reduce_sumreduce_meanr   )vrO   strategyr    r!   _reduce*  s4   


z#reduce_per_replica.<locals>._reduce)r}   r   r   TPUStrategyr   r   )valuesrL  rO   rM  r    rK  r!   rR     s   1 rR   c                 C   s   |j | dd}t| r tjdd | jD dd}|j |dd}n|j tjt| d dddd}tj|||jd}g }t	|j
j}t|D ]}|||d| 7 }qFt|S )zDOrder PerReplica objects for CollectiveAllReduceStrategy and concat.r   r  c                 S   s$   g | ]}t jt |d  d dqS )r   r  )r   expand_dimsr:   )r   single_valuer    r    r!   
<listcomp>S  s    z(_multi_worker_concat.<locals>.<listcomp>)num_or_size_splitsnumN)gatherrD  r   r   rO  rP  r:   splitnum_replicas_in_syncr   extendedworker_devicesrq   )rJ  rL  replicasshapes
all_shapesordered_replicasnum_replicas_per_worker
replica_idr    r    r!   rC  M  s,   rC  c                 C   sH   t | d tjrtjj|| dS t| d rtj| |dS tj| |dS )zConcats `tensor`s along `axis`.r   r  	sp_inputsr  )r}   r   SparseTensorsparser   
_is_scalarstack)tensorsr  r    r    r!   r   l  s
   r   c                 C   s>   | | }|jj}g }t|D ]}|||d| 7 }qt|S )z'Correctly order TPU PerReplica objects.N)rE  rX  num_replicas_per_hostrq   r   )rJ  rL  rZ  rg  r]  r_  r    r    r!   rG  v  s   
rG  c                 C   s   t | tjjo| j S r"   )r}   r   r   MultiWorkerMirroredStrategyrX  _in_multi_worker_moderL  r    r    r!   rB    s   rB  c                 C   s   t | tjjot | tjjS r"   )r}   r   r   r  __internal__CompositeTensor)objr    r    r!   rD    s   rD  c                 C   s   t | tjtjfo| jjdkS )Nr   )r}   r   TensorVariabler:   rankr  r    r    r!   rd    s   rd  c                 C   s   t | o	| jjdkS r   )_is_tpu_strategyrX  	num_hostsrj  r    r    r!   rF    s   rF  c                 C   r7  r"   )_is_tpu_strategy_classr   rj  r    r    r!   rq    r:  rq  c                 C   s$   dd }|| r
dS t tt| jS )Nc                 S   s   | j dS )NrN  )r  
startswith)kr    r    r!   is_tpu_strat  r6  z,_is_tpu_strategy_class.<locals>.is_tpu_stratT)anymaprs  	__bases__)clzrv  r    r    r!   rs    s   rs  c                 C   s(   t | tjr| S t | tjr| S |  S r"   )r}   r   RaggedTensorrb  numpyr  r    r    r!   r     s
   r   c                 C   s  t | dkr
| d S t| d tjrtjjd| dS t| d tjr)tj| ddS tdd | D }tjj	||dd kdd}tj	|
  r_t| d rXtj| ddS tj| ddS |
  ddd d	}|dkrtd}n
| d j| d }tjjd
d | D |dddS )a  Concats `Tensor`s along their first dimension.

    Args:
        tensors: List of `Tensor`s.

    Returns:
        Concatenation of the inputs along the first dimension -- of type
        `np.ndarray` if all input shapes are compatible, or `tf.RaggedTensor`
        if not.
    r   r   r`  r  c                 S   s   g | ]}t |d d qS )r   N)r   r:   r   tensorr    r    r!   rR    s    z-potentially_ragged_concat.<locals>.<listcomp>NFc                 S   s   g | ]}|  qS r    )r|  r}  r    r    r!   rR    s    )inner_shape)r   r}   r   rb  rc  r   r{  re  math
reduce_allr|  itemrd  tolistindexr:   raggedrw   
merge_dims)rf  non_batch_shapesconstant_dimsconstant_inner_dimensionsconstant_inner_shaper    r    r!   r     s0   r   )r   )/r;  rB   r|  r  
tensorflowr   tensorflow.python.eagerr   r3  	keras.srcr   r   r   r  r   r   r   keras.src.backendr   keras.src.lossesr   r8   keras.src.trainersr	   base_trainer keras.src.trainers.data_adaptersr
   r   !keras.src.trainers.epoch_iteratorr   keras.src.utilsr   Trainerr   r   rR   rC  r   rG  rB  rD  rd  rF  rq  rs  r   r   r    r    r    r!   <module>   sF         DZ

	