o
    Neg                     @   s\  d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZ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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% 	 dKddZ&dd Z(dLddZ)e#d										dMddZ*dd Z+dNddZ,dOd d!Z-dPd"d#Z.G d$d% d%Z/d&d' Z0e#d(G d)d* d*Z1d+d, Z2i a3da4e5 Z6da7e8 Z9da:e; Z<d-d. Z=d/d0 Z>d1d2 Z?d3d4 Z@d5d6 ZAe#d7G d8d9 d9ZBe#d:G d;d< d<eBZCdKd=d>ZDd?d@ ZEe#dAG dBdC dCeBZFe#dDdE		F	G	G	HdQdIdJZGdS )Rz(Utilities for file download and caching.    N)abstractmethod)closing)urlsplit)io_utils)
tf_inspect)Progbar)keras_export)urlopenTc                 C   s`   ddd}t | |}t|d}|||dD ]}|| qW d   dS 1 s)w   Y  dS )a  Replacement for `urlretrieve` for Python 2.

        Under Python 2, `urlretrieve` relies on `FancyURLopener` from legacy
        `urllib` module, known to have issues with proxy management.

        Args:
            url: url to retrieve.
            filename: where to store the retrieved data locally.
            reporthook: a hook function that will be called once on
              establishment of the network connection and once after each block
              read thereafter. The hook will be passed three arguments; a count
              of blocks transferred so far, a block size in bytes, and the total
              size of the file.
            data: `data` argument passed to `urlopen`.
            Nc                 s   sf    |   d}d}|d urt| }d}	 | |}|d7 }|d ur*|||| |r0|V  nd S q)NzContent-Lengthr   T   )infogetintstripread)response
chunk_size
reporthookcontent_type
total_sizecountchunk r   FD:\Projects\ConvertPro\env\Lib\site-packages\keras/utils/data_utils.py
chunk_readD   s   
zurlretrieve.<locals>.chunk_readwb)r   )r
   N)r	   openwrite)urlfilenamer   datar   r   fdr   r   r   r   urlretrieve3   s   

"r#   )r#   c                 C   sJ   t tttttf}t| tjt	j
f| rdS t| p$t| tp$t| tjS )z'Check if `x` is a Keras generator type.F)strlisttupledictset	frozenset
isinstancetfZTensornpZndarrayr   isgeneratorSequencetypingIterator)xZbuiltin_iteratorsr   r   r   is_generator_or_sequence]   s   

r2   .autoc                 C   s  |du rdS |dkrddg}t |tr|g}t| } t|}|D ]^}|dkr.tj}tj}|dkr8tj}tj	}|| r|| 5}z|
| W n# tjttfyk   tj|rjtj|ret|  t|  w W d    dS 1 sxw   Y   dS q"dS )aN  Extracts an archive if it matches tar, tar.gz, tar.bz, or zip formats.

    Args:
        file_path: path to the archive file
        path: path to extract the archive file
        archive_format: Archive format to try for extracting the file.
            Options are 'auto', 'tar', 'zip', and None.
            'tar' includes tar, tar.gz, and tar.bz files.
            The default 'auto' is ['tar', 'zip'].
            None or an empty list will return no matches found.

    Returns:
        True if a match was found and an archive extraction was completed,
        False otherwise.
    NFr4   tarzipT)r*   r$   r   path_to_stringtarfiler   
is_tarfilezipfileZipFile
is_zipfile
extractallTarErrorRuntimeErrorKeyboardInterruptospathexistsisfileremoveshutilrmtree)	file_pathrB   archive_formatZarchive_typeZopen_fnZis_match_fnarchiver   r   r   _extract_archivei   sD   








rK   zkeras.utils.get_fileFdatasetsc
              
   C   s  |du rt d|	du rtjtjdd}	|dur#|du r#|}d}tj|	}
t|
tjs7tjdd}
tj|
|}t| t	| } | s\tj
t|j} | s\t d| d|r| d	rtt| } | d
d
} t| } tj|| }|d	 }ntj|| }d}tj|r|durt|||dstd| d| d d}nd}|r3td|  G dd d}d}z=z	t|||  W n1 tjjy } zt|||j|jd}~w tjjy } zt|||j|jd}~ww W n ttfy   tj|rt |  w tj|r3|dur3t|||ds3t d| d| d|rFtj|sDt!||dd |S |rOt!||| |S )aM	  Downloads a file from a URL if it not already in the cache.

    By default the file at the url `origin` is downloaded to the
    cache_dir `~/.keras`, placed in the cache_subdir `datasets`,
    and given the filename `fname`. The final location of a file
    `example.txt` would therefore be `~/.keras/datasets/example.txt`.

    Files in tar, tar.gz, tar.bz, and zip formats can also be extracted.
    Passing a hash will verify the file after download. The command line
    programs `shasum` and `sha256sum` can compute the hash.

    Example:

    ```python
    path_to_downloaded_file = tf.keras.utils.get_file(
        "flower_photos",
        "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz",
        untar=True)
    ```

    Args:
        fname: Name of the file. If an absolute path `/path/to/file.txt` is
            specified the file will be saved at that location. If `None`, the
            name of the file at `origin` will be used.
        origin: Original URL of the file.
        untar: Deprecated in favor of `extract` argument.
            boolean, whether the file should be decompressed
        md5_hash: Deprecated in favor of `file_hash` argument.
            md5 hash of the file for verification
        file_hash: The expected hash string of the file after download.
            The sha256 and md5 hash algorithms are both supported.
        cache_subdir: Subdirectory under the Keras cache dir where the file is
            saved. If an absolute path `/path/to/folder` is
            specified the file will be saved at that location.
        hash_algorithm: Select the hash algorithm to verify the file.
            options are `'md5'`, `'sha256'`, and `'auto'`.
            The default 'auto' detects the hash algorithm in use.
        extract: True tries extracting the file as an Archive, like tar or zip.
        archive_format: Archive format to try for extracting the file.
            Options are `'auto'`, `'tar'`, `'zip'`, and `None`.
            `'tar'` includes tar, tar.gz, and tar.bz files.
            The default `'auto'` corresponds to `['tar', 'zip']`.
            None or an empty list will return no matches found.
        cache_dir: Location to store cached files, when None it
            defaults to the default directory `~/.keras/`.

    Returns:
        Path to the downloaded file
    NzCPlease specify the "origin" argument (URL of the file to download).~z.kerasmd5z/tmpz5Can't parse the file name from the origin provided: 'z0'.Please specify the `fname` as the input param.z.tar.gz F)	algorithmzNA local file was found, but it seems to be incomplete or outdated because the z0 file hash does not match the original value of z! so we will re-download the data.TzDownloading data from c                   @   s    e Zd ZdZdd Zdd ZdS )zget_file.<locals>.DLProgbarz1Manage progress bar state for use in urlretrieve.c                 S   s   d | _ d| _d S NF)progbarfinishedselfr   r   r   __init__  s   
z$get_file.<locals>.DLProgbar.__init__c                 S   sx   | j s|dkr	d }t|| _ || }|d u r| j | d S ||k r*| j | d S | js:| j | j j d| _d S d S )Nr   T)rR   r   updaterS   target)rU   Z	block_num
block_sizer   currentr   r   r   __call__  s   

z$get_file.<locals>.DLProgbar.__call__N)__name__
__module____qualname____doc__rV   r[   r   r   r   r   	DLProgbar  s    r`   z!URL fetch failure on {}: {} -- {}z+Incomplete or corrupted file detected. The z0 file hash does not match the provided value of r3   r5   )rI   )"
ValueErrorrA   rB   join
expanduseraccessW_OK_makedirs_exist_okr   r7   basenamer   endswithpathlibPathwith_suffixr$   rC   validate_fileZ	print_msgr#   urlliberror	HTTPError	ExceptionformatcodemsgURLErrorerrnoreasonr@   rE   rK   )fnameoriginZuntarZmd5_hash	file_hashZcache_subdirZhash_algorithmextractrI   	cache_dirZdatadir_basedatadirZuntar_fpathfpathdownloadr`   	error_msger   r   r   get_file   s   >




r   c                 C   s   t j| dd d S )NT)exist_ok)rA   makedirs)r|   r   r   r   rf   M  s   rf   c                 C   s<   | dkrt  S | dkr|durt|dkrt  S t  S )z+Returns hash algorithm as hashlib function.sha256r4   N@   )hashlibr   lenrN   )rP   ry   r   r   r   _resolve_hasherQ  s
   r   r     c                    st   t |tr
t|}n|}t| dt fdddD ]}|| qW d   | S 1 s1w   Y  | S )a  Calculates a file sha256 or md5 hash.

    Example:

    ```python
    _hash_file('/path/to/file.zip')
    'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
    ```

    Args:
        fpath: path to the file being validated
        algorithm: hash algorithm, one of `'auto'`, `'sha256'`, or `'md5'`.
            The default `'auto'` detects the hash algorithm in use.
        chunk_size: Bytes to read at a time, important for large files.

    Returns:
        The file hash
    rbc                      s
     S N)r   r   r   Z
fpath_filer   r   <lambda>v  s   
 z_hash_file.<locals>.<lambda>    N)r*   r$   r   r   iterrW   	hexdigest)r}   rP   r   hasherr   r   r   r   
_hash_file]  s   


r   c                 C   s*   t ||}tt| ||t|krdS dS )a  Validates a file against a sha256 or md5 hash.

    Args:
        fpath: path to the file being validated
        file_hash:  The expected hash string of the file.
            The sha256 and md5 hash algorithms are both supported.
        algorithm: Hash algorithm, one of 'auto', 'sha256', or 'md5'.
            The default 'auto' detects the hash algorithm in use.
        chunk_size: Bytes to read at a time, important for large files.

    Returns:
        Whether the file is valid
    TF)r   r$   r   )r}   ry   rP   r   r   r   r   r   rl   |  s   
rl   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ThreadsafeIterzEWrap an iterator with a lock and propagate exceptions to all threads.c                 C   s   || _ t | _d | _d S r   )it	threadingLocklock
_exception)rU   r   r   r   r   rV     s   


zThreadsafeIter.__init__c                 C   s   | S r   r   rT   r   r   r   __iter__  s   zThreadsafeIter.__iter__c                 C   s   |   S r   )__next__rT   r   r   r   next  s   zThreadsafeIter.nextc                 C   sd   | j % | jr
| jzt| jW W  d    S  ty' } z|| _ d }~ww 1 s+w   Y  d S r   )r   r   r   r   rp   )rU   r   r   r   r   r     s   
zThreadsafeIter.__next__N)r\   r]   r^   r_   rV   r   r   r   r   r   r   r   r     s    r   c                       t   fdd}|S )Nc                     s   t  | i |S r   )r   )akwfr   r   g  s   zthreadsafe_generator.<locals>.g	functoolswraps)r   r   r   r   r   threadsafe_generator  s   r   zkeras.utils.Sequencec                   @   s8   e Zd ZdZedd Zedd Zdd Zdd	 Zd
S )r.   a  Base object for fitting to a sequence of data, such as a dataset.

    Every `Sequence` must implement the `__getitem__` and the `__len__` methods.
    If you want to modify your dataset between epochs you may implement
    `on_epoch_end`.
    The method `__getitem__` should return a complete batch.

    Notes:

    `Sequence` are a safer way to do multiprocessing. This structure guarantees
    that the network will only train once
     on each sample per epoch which is not the case with generators.

    Examples:

    ```python
    from skimage.io import imread
    from skimage.transform import resize
    import numpy as np
    import math

    # Here, `x_set` is list of path to the images
    # and `y_set` are the associated classes.

    class CIFAR10Sequence(tf.keras.utils.Sequence):

        def __init__(self, x_set, y_set, batch_size):
            self.x, self.y = x_set, y_set
            self.batch_size = batch_size

        def __len__(self):
            return math.ceil(len(self.x) / self.batch_size)

        def __getitem__(self, idx):
            batch_x = self.x[idx * self.batch_size:(idx + 1) *
            self.batch_size]
            batch_y = self.y[idx * self.batch_size:(idx + 1) *
            self.batch_size]

            return np.array([
                resize(imread(file_name), (200, 200))
                   for file_name in batch_x]), np.array(batch_y)
    ```
    c                 C      t )zGets batch at position `index`.

        Args:
            index: position of the batch in the Sequence.

        Returns:
            A batch
        NotImplementedError)rU   indexr   r   r   __getitem__     
zSequence.__getitem__c                 C   r   )znNumber of batch in the Sequence.

        Returns:
            The number of batches in the Sequence.
        r   rT   r   r   r   __len__  s   zSequence.__len__c                 C   s   dS )z(Method called at the end of every epoch.Nr   rT   r   r   r   on_epoch_end  s   zSequence.on_epoch_endc                 #   s,     fddt t D D ]}|V  qdS )z2Create a generator that iterate over the Sequence.c                 3   s    | ]} | V  qd S r   r   ).0irT   r   r   	<genexpr>  s    z$Sequence.__iter__.<locals>.<genexpr>N)ranger   )rU   itemr   rT   r   r     s   zSequence.__iter__N)	r\   r]   r^   r_   r   r   r   r   r   r   r   r   r   r.     s    -

r.   c                 c   s    	 | D ]}|V  qq)zIterates indefinitely over a Sequence.

    Args:
      seq: `Sequence` instance.

    Yields:
      Batches of data from the `Sequence`.
    r   )seqr   r   r   r   iter_sequence_infinite  s
   	r   c                    r   )Nc                     sH   t  td}a | i |}|a|W  d    S 1 sw   Y  d S )NT)_FORCE_THREADPOOL_LOCK_FORCE_THREADPOOL)argskwargsZold_force_threadpooloutr   r   r   wrapped*  s   
$z.dont_use_multiprocessing_pool.<locals>.wrappedr   )r   r   r   r   r   dont_use_multiprocessing_pool)  s   r   c                 C   s   | rt rtjjS tjS r   )r   multiprocessingdummyZPool)use_multiprocessingr   r   r   get_pool_class6  s   r   c                   C   s   t du rt a t S )z,Lazily create the queue to track worker ids.N)_WORKER_ID_QUEUEr   Queuer   r   r   r   get_worker_id_queue=  s   r   c                 C   s   | a d S r   _SHARED_SEQUENCES)seqsr   r   r   	init_poolE  s   r   c                 C   s   t |  | S )ac  Get the value from the Sequence `uid` at index `i`.

    To allow multiple Sequences to be used at the same time, we use `uid` to
    get a specific one. A single Sequence would cause the validation to
    overwrite the training Sequence.

    Args:
        uid: int, Sequence identifier
        i: index

    Returns:
        The value at index `i`.
    r   )uidr   r   r   r   	get_indexJ  s   r   zkeras.utils.SequenceEnqueuerc                   @   sj   e Zd ZdZdddZdd Zdd	d
Zdd ZdddZdd Z	e
dd Ze
dd Ze
dd ZdS )SequenceEnqueuera  Base class to enqueue inputs.

    The task of an Enqueuer is to use parallelism to speed up preprocessing.
    This is done with processes or threads.

    Example:

    ```python
        enqueuer = SequenceEnqueuer(...)
        enqueuer.start()
        datas = enqueuer.get()
        for data in datas:
            # Use the inputs; training, evaluating, predicting.
            # ... stop sometime.
        enqueuer.stop()
    ```

    The `enqueuer.get()` should be an infinite stream of data.
    Fc                 C   s   || _ || _td u rztddaW n ty   daY nw tttr+t| _td7 ant	  tj
| _t j
d7  _
W d    n1 sEw   Y  d| _d | _d | _d | _d | _d S )Nr   r   r   )sequencer   _SEQUENCE_COUNTERr   ValueOSErrorr*   r   r   Zget_lockvalueworkersexecutor_fnqueue
run_threadstop_signal)rU   r   r   r   r   r   rV   q  s(   



zSequenceEnqueuer.__init__c                 C   s   | j d uo
| j   S r   )r   is_setrT   r   r   r   
is_running  s   zSequenceEnqueuer.is_runningr   
   c                    sd   | j r
|  | _n fdd| _ | _t|| _t | _tj	| j
d| _d| j_| j  dS )zStarts the handler's workers.

        Args:
            workers: Number of workers.
            max_queue_size: queue size
                (when full, workers could block on `put()`)
        c                    s   t d S rQ   )r   )_r   r   r   r     s    z(SequenceEnqueuer.start.<locals>.<lambda>)rX   TN)r   _get_executor_initr   r   r   r   r   Eventr   Thread_runr   daemonstart)rU   r   Zmax_queue_sizer   r   r   r     s   
zSequenceEnqueuer.startc                 C   s   | j t| j< dS )z&Sends current Iterable to all workers.N)r   r   r   rT   r   r   r   _send_sequence  s   zSequenceEnqueuer._send_sequenceNc                 C   sl   | j   | jj | jj  d| j_| jj  W d   n1 s$w   Y  | j	| dt
| j< dS )zStops running threads and wait for them to exit, if necessary.

        Should be called by the same thread which called `start()`.

        Args:
            timeout: maximum time to wait on `thread.join()`
        r   N)r   r(   r   mutexclearunfinished_tasksnot_fullnotifyr   rb   r   r   )rU   timeoutr   r   r   stop  s   

zSequenceEnqueuer.stopc                 C   s   |   r
|   d S d S r   )r   r   rT   r   r   r   __del__  s   zSequenceEnqueuer.__del__c                 C   r   )?Submits request to the executor and queue the `Future` objects.r   rT   r   r   r   r     s   zSequenceEnqueuer._runc                 C   r   )Gets the Pool initializer for multiprocessing.

        Args:
            workers: Number of workers.

        Returns:
            Function, a Function to initialize the pool
        r   rU   r   r   r   r   r     r   z#SequenceEnqueuer._get_executor_initc                 C   r   )zCreates a generator to extract data from the queue.

        Skip the data if it is `None`.
        # Returns
            Generator yielding tuples `(inputs, targets)`
                or `(inputs, targets, sample_weights)`.
        r   rT   r   r   r   r     s   	zSequenceEnqueuer.get)F)r   r   r   )r\   r]   r^   r_   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   [  s    




r   zkeras.utils.OrderedEnqueuerc                       sB   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Z  Z	S )OrderedEnqueuera  Builds a Enqueuer from a Sequence.

    Args:
        sequence: A `tf.keras.utils.data_utils.Sequence` object.
        use_multiprocessing: use multiprocessing if True, otherwise threading
        shuffle: whether to shuffle the data at the beginning of each epoch
    Fc                       t  || || _d S r   )superrV   shuffle)rU   r   r   r   	__class__r   r   rV        
zOrderedEnqueuer.__init__c                    s    fdd}|S )r   c                    s(   t d t| d t fd}t| |S NT)ZinitializerZinitargs)r   init_pool_generatorr   _DATA_POOLSaddr   poolr   r   r   pool_fn  s   

z3OrderedEnqueuer._get_executor_init.<locals>.pool_fnr   rU   r   r   r   r   r   r     s   
	z"OrderedEnqueuer._get_executor_initc                 C   s(   	 t d | jjdks| j rdS q)zWait for the queue to be empty.T皙?r   N)timesleepr   r   r   r   rT   r   r   r   _wait_queue  s
   
zOrderedEnqueuer._wait_queuec                 C   s   t tt| j}|   	 | jrt| t| t	<}|D ]}| j
 r0 W d   dS | jj|t| j|fdd q |   | j
 rR	 W d   dS W d   n1 s\w   Y  | j  |   qr   TNblock)r%   r   r   r   r   r   randomr   r   r   r   r   r   putapply_asyncr   r   r   r   )rU   r   executorr   r   r   r   r     s.   



zOrderedEnqueuer._runc              
   c   s    |   rCz| jjddd }|   r| j  |dur |V  W n tjy+   Y n ty< } z|   |d}~ww |   sdS dS )  Creates a generator to extract data from the queue.

        Skip the data if it is `None`.

        Yields:
            The next element in the queue, i.e. a tuple
            `(inputs, targets)` or
            `(inputs, targets, sample_weights)`.
        T   r   r   N)r   r   r   	task_doneEmptyrp   r   )rU   inputsr   r   r   r   r   "  s"   

zOrderedEnqueuer.get)FF)
r\   r]   r^   r_   rV   r   r   r   r   __classcell__r   r   r   r   r     s    r   c                 C   sV   | a t }d|j |_|durtj||j  |dur)|j|jddd dS dS )a  Initializer function for pool workers.

    Args:
      gens: State which should be made available to worker processes.
      random_seed: An optional value with which to seed child processes.
      id_queue: A multiprocessing Queue of worker ids. This is used to indicate
        that a worker process was created by Keras and can be terminated using
        the cleanup_all_keras_forkpools utility.
    ZKeras_worker_NTr   r  )	r   r   current_processnamer,   r  seedidentr  )Zgensrandom_seedZid_queueZworker_procr   r   r   r   :  s   r   c                 C   s   t t|  S )a[  Gets the next value from the generator `uid`.

    To allow multiple generators to be used at the same time, we use `uid` to
    get a specific one. A single generator would cause the validation to
    overwrite the training generator.

    Args:
        uid: int, generator identifier

    Returns:
        The next value of generator `uid`.
    )r   r   )r   r   r   r   next_sampleU  s   r  zkeras.utils.GeneratorEnqueuerc                       s:   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Z  ZS )GeneratorEnqueuera  Builds a queue out of a data generator.

    The provided generator can be finite in which case the class will throw
    a `StopIteration` exception.

    Args:
        generator: a generator function which yields data
        use_multiprocessing: use multiprocessing if True, otherwise threading
        random_seed: Initial seed for workers,
            will be incremented by one for each worker.
    FNc                    r   r   )r   rV   r  )rU   	generatorr   r  r   r   r   rV   s  r   zGeneratorEnqueuer.__init__c                    s    fdd}|S )zGets the Pool initializer for multiprocessing.

        Args:
          workers: Number of works.

        Returns:
            A Function to initialize the pool
        c                    s*   t dt|  jt fd}t| |S r   )r   r   r  r   r   r   r   r   r   r   r     s   
z5GeneratorEnqueuer._get_executor_init.<locals>.pool_fnr   r   r   r   r   r   w  s   
	z$GeneratorEnqueuer._get_executor_initc                 C   sh   |    t| t}	 | j r	 W d   dS | jj|t	| j
fdd q1 s-w   Y  dS r   )r   r   r   r   r   r   r   r  r  r  r   )rU   r  r   r   r   r     s   
zGeneratorEnqueuer._runc              
   c   s    z#|   r"| jjdd }| j  |dur|V  |   sW dS W dS  tyd   g }| j dkrE|| jjdd | j dks4|D ]}|  qGdd |D }|D ]	}|dur`|V  qWY dS  ty } z| 	  dt
|v rytd|d}~ww )	r  Tr   Nr   c                 S   s   g | ]
}|  r| qS r   )Z
successfulr   )r   futurer   r   r   
<listcomp>  s
    z)GeneratorEnqueuer.get.<locals>.<listcomp>zgenerator already executingzyYour generator is NOT thread-safe. Keras requires a thread-safe generator when `use_multiprocessing=False, workers > 1`. )r   r   r   r  StopIterationqsizeappendwaitrp   r   r$   r?   )rU   r
  Z	last_onesr   r   r   r   r   r     s@   


zGeneratorEnqueuer.get)FN)	r\   r]   r^   r_   rV   r   r   r   r  r   r   r   r   r  e  s    r  zkeras.utils.pad_sequencesz*keras.preprocessing.sequence.pad_sequencesint32pre        c                 C   s  t | ds	tdt| }g }d}d}	| D ]3}
z|t|
 |	r1t|
r1t|
jdd }d}	W q tyH } z
tdt|
 |d}~ww |du rRt	|}t
|tjp_t
|tj}t|trx|tkrx|sxtd	| d
t| dtj||f| ||d}
t| D ]p\}}t|sq|dkr|| d }n|dkr|d| }ntd| dtj||d}|jdd |krtd|jdd  d| d| |dkr||
|dt|f< q|dkr||
|t| df< qtd| d|
S )a	  Pads sequences to the same length.

    This function transforms a list (of length `num_samples`)
    of sequences (lists of integers)
    into a 2D Numpy array of shape `(num_samples, num_timesteps)`.
    `num_timesteps` is either the `maxlen` argument if provided,
    or the length of the longest sequence in the list.

    Sequences that are shorter than `num_timesteps`
    are padded with `value` until they are `num_timesteps` long.

    Sequences longer than `num_timesteps` are truncated
    so that they fit the desired length.

    The position where padding or truncation happens is determined by
    the arguments `padding` and `truncating`, respectively.
    Pre-padding or removing values from the beginning of the sequence is the
    default.

    >>> sequence = [[1], [2, 3], [4, 5, 6]]
    >>> tf.keras.preprocessing.sequence.pad_sequences(sequence)
    array([[0, 0, 1],
           [0, 2, 3],
           [4, 5, 6]], dtype=int32)

    >>> tf.keras.preprocessing.sequence.pad_sequences(sequence, value=-1)
    array([[-1, -1,  1],
           [-1,  2,  3],
           [ 4,  5,  6]], dtype=int32)

    >>> tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post')
    array([[1, 0, 0],
           [2, 3, 0],
           [4, 5, 6]], dtype=int32)

    >>> tf.keras.preprocessing.sequence.pad_sequences(sequence, maxlen=2)
    array([[0, 1],
           [2, 3],
           [5, 6]], dtype=int32)

    Args:
        sequences: List of sequences (each sequence is a list of integers).
        maxlen: Optional Int, maximum length of all sequences. If not provided,
            sequences will be padded to the length of the longest individual
            sequence.
        dtype: (Optional, defaults to `"int32"`). Type of the output sequences.
            To pad sequences with variable length strings, you can use `object`.
        padding: String, "pre" or "post" (optional, defaults to `"pre"`):
            pad either before or after each sequence.
        truncating: String, "pre" or "post" (optional, defaults to `"pre"`):
            remove values from sequences larger than
            `maxlen`, either at the beginning or at the end of the sequences.
        value: Float or String, padding value. (Optional, defaults to 0.)

    Returns:
        Numpy array with shape `(len(sequences), maxlen)`

    Raises:
        ValueError: In case of invalid values for `truncating` or `padding`,
            or in case of invalid shape for a `sequences` entry.
    r   z`sequences` must be iterable.r   Tr   NFz=`sequences` must be a list of iterables. Found non-iterable: z`dtype` z( is not compatible with `value`'s type: z;
You should set `dtype=object` for variable length strings.)dtyper  postzTruncating type "z" not understoodzShape of sample z of sequence at position z" is different from expected shape zPadding type ")hasattrra   r   r  r,   Zasarrayshape	TypeErrorr$   maxZ
issubdtypeZstr_Zunicode_r*   objecttypefull	enumerate)	sequencesmaxlenr  paddingZ
truncatingr   Znum_sampleslengthsZsample_shapeflagr1   r   Zis_dtype_stridxstruncr   r   r   pad_sequences  st   
H
r/  )NN)r3   r4   )
NNFNNrL   r4   Fr4   Nr   )r   r   )r4   r   )Nr  r  r  r  )Hr_   r   r   Zmultiprocessing.dummyr   rA   ri   r   r  rF   r8   r   r   r/   rm   weakrefr:   abcr   
contextlibr   numpyr,   Ztensorflow.compat.v2compatv2r+   Zsix.moves.urllib.parser   Zkeras.utilsr   r   Zkeras.utils.generic_utilsr   Z tensorflow.python.util.tf_exportr   Zsix.moves.urllib.requestr	   r#   urllib.requestr2   rK   r   rf   r   r   rl   r   r   r.   r   r   r   WeakSetr   r   r(   Z_WORKER_IDSr   RLockr   r   r   r   r   r   r   r   r   r  r  r/  r   r   r   r   <module>   s   
*
1 3


#M
^\