tensorflow库中缺少函数keras.sequence.pad_sequences

ryoqjall  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(152)

有没有可能库中的某个方法丢失了,而它应该在那里?

from keras.preprocessing import sequence
  features_train = sequence.pad_sequences(data_train, maxlen=400)
AttributeError                            Traceback (most recent call last)
 Input In [5], in <cell line: 1>()
 ----> 1 features_train = sequence.pad_sequences(data_train, maxlen=400)
       2 features_test = sequence.pad_sequences(data_test, maxlen=400)

 AttributeError: module 'keras.preprocessing.sequence' has no attribute 'pad_sequences'

我检查了帮助和功能不存在

FUNCTIONS

make_sampling_table(size,sampling_factor= 1 e-05)生成基于单词排序的概率抽样表。

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.

skipgram(sequence,vocabulary_size,window_size=4,negative_samples=1.0,shuffle=True,categorical=False,sampling_table=None,seed=None)生成skipgram单词对。

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.

虽然很明显,对于tensorflow 2.9.0(我现在使用的版本),从他们的网站上应该可以使用的功能是:类类TimeseriesGenerator:用于生成批量时态数据的实用工具类。
函数make_sampling_table(...):生成基于单词排名的概率抽样表。

pad_sequences(...):将序列填充为相同长度。
skipgrams(...):生成skipgram单词对。

这是因为安装失败还是其他原因造成的?因为我从来没有这样的问题在我的生活,甚至无法找到任何参考在互联网上

mcdcgff0

mcdcgff01#

我也遇到了同样的问题,我使用

keras.utils.data_utils.pad_sequences

而是
我猜他们是从keras转移过来的序列
希望能帮到你:)

ogsagwnx

ogsagwnx2#

而不是keras.preprocessing.sequence.pad_sequences Pip install keras-preprocessing
从keras_preprocessing导入pad_sequences
希望它能有所帮助

相关问题