【问题标题】:Tensorflow Keras "TypeError: '>=' not supported between instances of 'int' and 'tuple'"Tensorflow Keras \"TypeError: \'>=\' 在 \'int\' 和 \'tuple\'\ 的实例之间不支持"
【发布时间】:2022-12-15 21:49:30
【问题描述】:

我正在尝试解决 deeplearning.ai 的作业在尝试将句子转换为序列时出现以下错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-50-934f9fde7150> in <module>
      1 # Test your function
----> 2 train_pad_trunc_seq = seq_pad_and_trunc(train_sentences, tokenizer, PADDING, TRUNCATING, maxlen=16)
      3 val_pad_trunc_seq = seq_pad_and_trunc(val_sentences, tokenizer, PADDING, TRUNCATING, MAXLEN)
      4 
      5 print(f"Padded and truncated training sequences have shape: {train_pad_trunc_seq.shape}\n")

<ipython-input-47-1ad2379829b0> in seq_pad_and_trunc(sentences, tokenizer, padding, truncating, maxlen)
     16 
     17     # Convert sentences to sequences
---> 18     sequences = tokenizer.texts_to_sequences(sentences)
     19 
     20     # Pad the sequences using the correct padding, truncating and maxlen

/opt/conda/lib/python3.8/site-packages/keras_preprocessing/text.py in texts_to_sequences(self, texts)
    279             A list of sequences.
    280         """
--> 281         return list(self.texts_to_sequences_generator(texts))
    282 
    283     def texts_to_sequences_generator(self, texts):

/opt/conda/lib/python3.8/site-packages/keras_preprocessing/text.py in texts_to_sequences_generator(self, texts)
    315                 i = self.word_index.get(w)
    316                 if i is not None:
--> 317                     if num_words and i >= num_words:
    318                         if oov_token_index is not None:
    319                             vect.append(oov_token_index)

TypeError: '>=' not supported between instances of 'int' and 'tuple'

这是我的 github repo 与相关代码的链接。

https://github.com/dkonuk/datascience/blob/main/C3W3_Assignment.ipynb

【问题讨论】:

    标签: tensorflow nlp


    【解决方案1】:

    tf.keras.preprocessing.text.Tokenizer API 不将 train_sentences 作为参数。您正在将 train_sentences 传递给它,因此会引发错误。

    替换以下内容

    tokenizer = Tokenizer(train_sentences, oov_token=OOV_TOKEN)
    

    在 fit tokenizer() 方法中使用下面的行。

    tokenizer = Tokenizer(oov_token=OOV_TOKEN,)
    

    有关 Tokenizer 的更多信息,请参阅document。谢谢!

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2018-09-03
      • 2020-11-13
      • 2021-01-15
      • 2020-06-02
      相关资源
      最近更新 更多