o
    ={c:6                     @   s   d 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 Z	edG dd	 d	ej
Zed
dddZed						dddZdS )ai  Utilities for preprocessing sequence data.

Deprecated: `tf.keras.preprocessing.sequence` APIs are not recommended for new
code. Prefer `tf.keras.utils.timeseries_dataset_from_array` and
the `tf.data` APIs which provide a much more flexible mechanisms for dealing
with sequences. See the [tf.data guide](https://www.tensorflow.org/guide/data)
for more details.
    N)
data_utils)keras_exportc                 C   sF   g g }}t ||D ]\}}t|| k r|| || q
||fS )aC  Removes sequences that exceed the maximum length.

    Args:
        maxlen: Int, maximum length of the output sequences.
        seq: List of lists, where each sublist is a sequence.
        label: List where each element is an integer.

    Returns:
        new_seq, new_label: shortened lists for `seq` and `label`.
    )ziplenappend)maxlenseqZlabelZnew_seqZ	new_labelxy r   <lib/python3.10/site-packages/keras/preprocessing/sequence.py_remove_long_seq$   s   


r   z0keras.preprocessing.sequence.TimeseriesGeneratorc                   @   sH   e Zd ZdZ							dddZd	d
 Zdd Zdd Zdd ZdS )TimeseriesGeneratora  Utility class for generating batches of temporal data.

    Deprecated: `tf.keras.preprocessing.sequence.TimeseriesGenerator` does not
    operate on tensors and is not recommended for new code. Prefer using a
    `tf.data.Dataset` which provides a more efficient and flexible mechanism for
    batching, shuffling, and windowing input. See the
    [tf.data guide](https://www.tensorflow.org/guide/data) for more details.

    This class takes in a sequence of data-points gathered at
    equal intervals, along with time series parameters such as
    stride, length of history, etc., to produce batches for
    training/validation.

    Arguments:
        data: Indexable generator (such as list or Numpy array)
            containing consecutive data points (timesteps).
            The data should be at 2D, and axis 0 is expected
            to be the time dimension.
        targets: Targets corresponding to timesteps in `data`.
            It should have same length as `data`.
        length: Length of the output sequences (in number of timesteps).
        sampling_rate: Period between successive individual timesteps
            within sequences. For rate `r`, timesteps
            `data[i]`, `data[i-r]`, ... `data[i - length]`
            are used for create a sample sequence.
        stride: Period between successive output sequences.
            For stride `s`, consecutive output samples would
            be centered around `data[i]`, `data[i+s]`, `data[i+2*s]`, etc.
        start_index: Data points earlier than `start_index` will not be used
            in the output sequences. This is useful to reserve part of the
            data for test or validation.
        end_index: Data points later than `end_index` will not be used
            in the output sequences. This is useful to reserve part of the
            data for test or validation.
        shuffle: Whether to shuffle output samples,
            or instead draw them in chronological order.
        reverse: Boolean: if `true`, timesteps in each output sample will be
            in reverse chronological order.
        batch_size: Number of timeseries samples in each batch
            (except maybe the last one).

    Returns:
        A [Sequence](
        https://www.tensorflow.org/api_docs/python/tf/keras/utils/Sequence)
        instance.

    Examples:
        ```python
        from keras.preprocessing.sequence import TimeseriesGenerator
        import numpy as np
        data = np.array([[i] for i in range(50)])
        targets = np.array([[i] for i in range(50)])
        data_gen = TimeseriesGenerator(data, targets,
                                       length=10, sampling_rate=2,
                                       batch_size=2)
        assert len(data_gen) == 20
        batch_0 = data_gen[0]
        x, y = batch_0
        assert np.array_equal(x,
                              np.array([[[0], [2], [4], [6], [8]],
                                        [[1], [3], [5], [7], [9]]]))
        assert np.array_equal(y,
                              np.array([[10], [11]]))
        ```
       r   NF   c                 C   s   t |t |krtddt |  dt |  || _|| _|| _|| _|| _|| | _|d u r8t |d }|| _|| _	|	| _
|
| _| j| jkrTtd| j| jf d S )NzData and targets have to bez  of same length. Data length is z while target length is r   zz`start_index+length=%i > end_index=%i` is disallowed, as no part of the sequence would be left to be used as current step.)r   
ValueErrordatatargetslengthsampling_ratestridestart_index	end_indexshufflereverse
batch_size)selfr   r   r   r   r   r   r   r   r   r   r   r   r   __init__{   s6   

zTimeseriesGenerator.__init__c                 C   s$   | j | j | j| j  | j| j  S )N)r   r   r   r   r   r   r   r   __len__   s   
zTimeseriesGenerator.__len__c                    s    j rtjj j jd  jd}n j j j |  }t|t	| j j   jd  j}t
 fdd|D }t
 fdd|D } jrZ|d d d d ddf |fS ||fS )Nr   )sizec                    s$   g | ]} j | j | j qS r   )r   r   r   .0rowr   r   r   
<listcomp>   s    z3TimeseriesGenerator.__getitem__.<locals>.<listcomp>c                    s   g | ]} j | qS r   )r   r!   r   r   r   r$      s    .)r   nprandomrandintr   r   r   r   arangeminZarrayr   )r   indexZrowsiZsamplesr   r   r   r   __getitem__   s&   
zTimeseriesGenerator.__getitem__c                 C   s   | j }t| j jtjkr| j  }zt|}W n ty* } ztd||d}~ww | j	}t| j	jtjkr<| j	 }zt|}W n tyU } ztd||d}~ww ||| j
| j| j| j| j| j| j| jd
S )zReturns the TimeseriesGenerator configuration as Python dictionary.

        Returns:
            A Python dictionary with the TimeseriesGenerator configuration.
        zData not JSON Serializable:NzTargets not JSON Serializable:)
r   r   r   r   r   r   r   r   r   r   )r   type
__module__r&   __name__tolistjsondumps	TypeErrorr   r   r   r   r   r   r   r   r   )r   r   Z	json_dataer   Zjson_targetsr   r   r   
get_config   s:   

zTimeseriesGenerator.get_configc                 K   s(   |   }| jj|d}tj|fi |S )a  Returns a JSON string containing the timeseries generator configuration.

        Args:
            **kwargs: Additional keyword arguments
                to be passed to `json.dumps()`.
        Returns:
            A JSON string containing the tokenizer configuration.
        )
class_nameconfig)r6   	__class__r0   r2   r3   )r   kwargsr8   Ztimeseries_generator_configr   r   r   to_json   s
   	zTimeseriesGenerator.to_json)r   r   r   NFFr   )	r0   r/   __qualname____doc__r   r   r-   r6   r;   r   r   r   r   r   7   s    G
*#r   z0keras.preprocessing.sequence.make_sampling_tableh㈵>c                 C   sV   d}t | }d|d< |t ||  d dd|   }|| }t d|t | S )a2  Generates a word rank-based probabilistic sampling table.

    Used for generating the `sampling_table` argument for `skipgrams`.
    `sampling_table[i]` is the probability of sampling
    the word i-th most common word in a dataset
    (more common words should be sampled less frequently, for balance).

    The sampling probabilities are generated according
    to the sampling distribution used in word2vec:

    ```
    p(word) = (min(1, sqrt(word_frequency / sampling_factor) /
        (word_frequency / sampling_factor)))
    ```

    We assume that the word frequencies follow Zipf's law (s=1) to derive
    a numerical approximation of frequency(rank):

    `frequency(rank) ~ 1/(rank * (log(rank) + gamma) + 1/2 - 1/(12*rank))`
    where `gamma` is the Euler-Mascheroni constant.

    Args:
        size: Int, number of possible words to sample.
        sampling_factor: The sampling factor in the word2vec formula.

    Returns:
        A 1D Numpy array of length `size` where the ith entry
        is the probability that a word of rank i should be sampled.
    gX9v?r   r   g      ?      ?g      (@)r&   r)   logZminimumZsqrt)r    Zsampling_factorZgammaZrankZinv_fqfr   r   r   make_sampling_table   s   
"rB   z&keras.preprocessing.sequence.skipgrams   r?   TFc                    sj  g }g }	t | D ]O\}
}|sq|dur|| t k rqtd|
| }tt| |
| d }t||D ]#}||
krV| | }|s@q3|||g |rQ|	ddg q3|	d q3q|dkrtt|	| }dd |D t | fddt|D 7 }|r|	ddgg| 7 }	n|	dg| 7 }	|r|du rt	dd}t
| t| t
| t|	 ||	fS )as  Generates skipgram word pairs.

    This function transforms a sequence of word indexes (list of integers)
    into tuples of words of the form:

    - (word, word in the same window), with label 1 (positive samples).
    - (word, random word from the vocabulary), with label 0 (negative samples).

    Read more about Skipgram in this gnomic paper by Mikolov et al.:
    [Efficient Estimation of Word Representations in
    Vector Space](http://arxiv.org/pdf/1301.3781v3.pdf)

    Args:
        sequence: A word sequence (sentence), encoded as a list
            of word indices (integers). If using a `sampling_table`,
            word indices are expected to match the rank
            of the words in a reference dataset (e.g. 10 would encode
            the 10-th most frequently occurring token).
            Note that index 0 is expected to be a non-word and will be skipped.
        vocabulary_size: Int, maximum possible word index + 1
        window_size: Int, size of sampling windows (technically half-window).
            The window of a word `w_i` will be
            `[i - window_size, i + window_size+1]`.
        negative_samples: Float >= 0. 0 for no negative (i.e. random) samples.
            1 for same number as positive samples.
        shuffle: Whether to shuffle the word couples before returning them.
        categorical: bool. if False, labels will be
            integers (eg. `[0, 1, 1 .. ]`),
            if `True`, labels will be categorical, e.g.
            `[[1,0],[0,1],[0,1] .. ]`.
        sampling_table: 1D array of size `vocabulary_size` where the entry i
            encodes the probability to sample a word of rank i.
        seed: Random seed.

    Returns:
        couples, labels: where `couples` are int pairs and
            `labels` are either 0 or 1.

    Note:
        By convention, index 0 in the vocabulary is
        a non-word and will be skipped.
    Nr   r   c                 S   s   g | ]}|d  qS )r   r   )r"   cr   r   r   r$   l  s    zskipgrams.<locals>.<listcomp>c                    s,   g | ]}|t   td  d  gqS )r   )r   r'   r(   )r"   r,   vocabulary_sizewordsr   r   r$   o  s    g    cA)	enumerater'   maxr*   r   ranger   intr   r(   seed)ZsequencerF   Zwindow_sizeZnegative_samplesr   ZcategoricalZsampling_tablerL   Zcoupleslabelsr,   ZwiZwindow_startZ
window_endjZwjZnum_negative_samplesr   rE   r   	skipgrams  sN   5





rO   )r>   )rC   r?   TFNN)r=   r2   r'   Znumpyr&   Zkeras.utilsr   Z tensorflow.python.util.tf_exportr   r   Sequencer   rB   rO   r   r   r   r   <module>   s(   
 @'