o
    2h                     @   s>   d Z ddlZddlZddlmZ ddlmZ G dd dZdS )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                   @   sv   e Z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
ejdd Zedd ZdS )EpochIteratorNF   c	           
   	   C   sp   t  }	|	r|r|	|k rtd|	  |	}|| _|| _d | _d | _d| _t	j
|||||||d| _| jj| _d S )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    r   \/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/trainers/epoch_iterator.py__init__2   s,   	zEpochIterator.__init__c                 C   s
   | j  S N)r   get_numpy_iteratorr   r   r   r   _get_iteratorU   s   
zEpochIterator._get_iteratorc                 C   s   t jddd d S )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   r   r   r   _interrupted_warningX   s   
z"EpochIterator._interrupted_warningc                 C   s*   d | _ | jj| _d| _d | _| j  d S )Nr   )r   r   r   r   r   r   on_epoch_endr   r   r   r   resetb   s
   
zEpochIterator.resetc                 c   s&   | j   | jp| jpd}|dkrj| jd u s| jd u r&t|  | _d| _td|| j	D ](}| jrA| j| jkrA| jr?| 
   n|  j| j	7  _||| j	 d | jfV  q-| jri| j| jkrit|  | _d| _n"t|  }| j	 }	 || j	7 }|| j	 | _||| j	 d |fV  qu| j   d S )Nr   r   )r   on_epoch_beginr
   r   r   iterr   r   ranger   r"   r#   )r   r
   stepiteratorr   r   r   _enumerate_iteratori   s:   

z!EpochIterator._enumerate_iteratorc                 C   s   |   | _| S r   )r+   r   r   r   r   r   __iter__   s   
zEpochIterator.__iter__c                 C   sz   g }t | j\}}}|   t| jD ]}t |}|| q|||fW  d    S 1 s/w   Y  |r;|||fS tr   )nextr   catch_stop_iterationr(   r   appendStopIteration)r   buffer
begin_stepend_stepr*   _datar   r   r   __next__   s   
 
zEpochIterator.__next__c                 c   s"    | D ]\}}}|||fV  qd S r   r   )r   r2   r3   r5   r   r   r   enumerate_epoch   s   zEpochIterator.enumerate_epochc                 c   sN    zdV  W dS  t y&   | jdu r| j| _|   d| _| j  Y dS w )z1Catches errors when an iterator runs out of data.N)r0   r   r   r"   r   r   r#   r   r   r   r   r.      s   
z"EpochIterator.catch_stop_iterationc                 C   s   | j r| j S | jS r   )r
   r   r   r   r   r   r      s   zEpochIterator.num_batches)NNNNFNr   )__name__
__module____qualname__r   r   r"   r$   r+   r,   r6   r7   
contextlibcontextmanagerr.   propertyr   r   r   r   r   r   1   s(    
#

r   )__doc__r;   r   keras.src.backendr   keras.src.trainersr   r   r   r   r   r   <module>   s    )