Tensorflow Keras的类型错误:“int”和“tuple”的示例之间不支持“>=”“

lstz6jyr  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(127)

我试图解决一个作业从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

cunj1qz1

cunj1qz11#

tf.keras.preprocessing.text.Tokenizer API不接受train_sentations作为参数。您向其传递train_sentations,因此引发错误。
替换以下内容

tokenizer = Tokenizer(train_sentences, oov_token=OOV_TOKEN)

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

tokenizer = Tokenizer(oov_token=OOV_TOKEN,)

有关令牌器的更多信息,请参阅document。谢谢!

相关问题