
    2Vh                     D    d Z ddlZddlZddlmZ ddlmZ  G d d      Zy)a-  
Separation of concerns:

DataAdapter:
    - x, y
    - sample_weight
    - class_weight
    - shuffle
    - batch_size
        - steps, as it relates to batch_size for array data

EpochIterator:
    - whether to yield numpy or tf data
    - steps
    - most argument validation

Trainer:
    - steps_per_execution
    - validation_split
    - validation_data
    - callbacks
    - validation_freq
    - epochs
    - initial_epoch
    - any backend-specific concern such as distribution

PyDataset:
    - num_workers
    - use_multiprocessing
    - max_queue_size

EpochIterator steps:

1. Look at data type and select correct DataHandler
2. Instantiate DataHandler with correct arguments
3. Raise or warn on unused arguments
4. in __iter__, iterate, either for a fixed number of steps
or until there is no data

    N)config)data_adaptersc                       e Zd Z	 	 	 	 	 	 	 ddZd Zd Zd Zd Zd Zd Z	d	 Z
ej                  d
        Zed        Zy)EpochIteratorNc	           	      0   t        j                         }	|	r!|r|	|k  rt        j                  d|	z         |	}|| _        || _        d | _        d | _        d| _        t        j                  |||||||      | _        | j                  j                  | _        y )NzLimiting steps_per_epoch to %dr   )xysample_weight
batch_sizesteps_per_epochshuffleclass_weight)r   max_steps_per_epochwarningswarnr   steps_per_execution_current_iterator_epoch_iterator_steps_seenr   get_data_adapterdata_adapternum_batches_num_batches)
selfr   r	   r
   r   r   r   r   r   r   s
             Q/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/trainers/epoch_iterator.py__init__zEpochIterator.__init__2   s     %88:"&9O&K47JJ #6.#6 !%#)::'!+%
 !--99    c                 6    | j                   j                         S N)r   get_numpy_iteratorr   s    r   _get_iteratorzEpochIterator._get_iteratorU   s      3355r   c                 2    t        j                  dd       y )NzYour input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.   )
stacklevel)r   r   r!   s    r   _interrupted_warningz"EpochIterator._interrupted_warningX   s    3
 	
r   c                     d | _         | j                  j                  | _        d| _        d | _        | j                  j                          y )Nr   )r   r   r   r   r   r   on_epoch_endr!   s    r   resetzEpochIterator.resetb   s@    !% --99#&&(r   c              #     K   | j                   j                          | j                  xs | j                  xs d}|dkD  r| j                  | j                  %t        | j                               | _        d| _        t        d|| j                        D ]t  }| j                  r7| j                  | j                  k\  r| j                  r| j                           n1| xj                  | j                  z  c_        || j                  f v | j                  r| j                  | j                  k\  rwt        | j                               | _        d| _        nQt        | j                               }| j                   }	 || j                  z  }|| j                  z   | _        ||f *| j                   j                          y w)Nr   )r   on_epoch_beginr   r   r   iterr"   r   ranger   r&   r(   )r   r   stepiterators       r   _enumerate_iteratorz!EpochIterator._enumerate_iteratori   s    ((*..I$2C2CIrQ%%-1E1E1M)-d.@.@.B)C&#$ a$2J2JK 3$$)9)9T=N=N)N++113  D$<$<< D22223   T%5%59J9J%J)-d.@.@.B)C&#$ D..01H,,,D000#'$*B*B#B Hn$  	&&(s   GGc                 0    | j                         | _        | S r   )r1   r   r!   s    r   __iter__zEpochIterator.__iter__   s    #779r   c                    g }t        | j                        \  }}| j                         5  t        | j                        D ]  }t        |      }|j                  |         ||fcd d d        S # 1 sw Y   nxY w|r||fS t        r   )nextr   catch_stop_iterationr.   r   appendStopIteration)r   bufferr/   r0   _datas         r   __next__zEpochIterator.__next__   s    d223h&&( 	 4334 $H~d#$ <		  	  	 
 <s   :A//A8c              #   ,   K   | D ]  \  }}||f  y wr    )r   r/   r;   s      r   enumerate_epochzEpochIterator.enumerate_epoch   s#      	JD$*	s   c              #      K   	 d y# t         $ rQ | j                  | j                  | _        | j                          d| _        | j
                  j                          Y yw xY ww)z1Catches errors when an iterator runs out of data.N)r8   r   r   r&   r   r   r(   r!   s    r   r6   z"EpochIterator.catch_stop_iteration   s\     	- 	-  ($($4$4!%%'%)D"**,	-s&   A&	 A&AA# A&"A##A&c                 J    | j                   r| j                   S | j                  S r   )r   r   r!   s    r   r   zEpochIterator.num_batches   s%    '''    r   )NNNNFN   )__name__
__module____qualname__r   r"   r&   r)   r1   r3   r<   r?   
contextlibcontextmanagerr6   propertyr   r>   r   r   r   r   1   sp     !:F6
))6
 	- 	- ! !r   r   )__doc__rF   r   keras.src.backendr   keras.src.trainersr   r   r>   r   r   <module>rL      s%   'R   $ ,y! y!r   