C3W3 seq_pad_and_trunk

I’m getting an error in the function seq_pad_and_trunc that I can’t understand. My output is appended below. Please advise.

Thanks!

TypeError Traceback (most recent call last)
in
1 # Test your function
----> 2 train_pad_trunc_seq = seq_pad_and_trunc(train_sentences, tokenizer, PADDING, TRUNCATING, MAXLEN)
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")

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’

Course 2 week 4 deals with an mnist dataset. Please see this community user guide and move your topic to the correct sub-category.

1 Like

Thanks, this the moving now done. I didn’t find what I needed in the community guidelines, but trial and error moving the post worked :slight_smile:
Unfortunately the error remains…

Please click my name and message your notebook as an attachment.

The mistake is not in seq_pad_and_trunc but in fit_tokenizer function. The 1st parameter of Tokenizer#init is num_words. This should not be set to sentences. Please read the docs and fix the constructor call.