Challenge - 5 Problems
Tokenizer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tokenizer on Simple Text
What is the output of the following code snippet using TensorFlow's Tokenizer?
TensorFlow
from tensorflow.keras.preprocessing.text import Tokenizer texts = ['I love machine learning', 'Machine learning is fun'] tokenizer = Tokenizer(num_words=5) tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) print(sequences)
Attempts:
2 left
💡 Hint
Remember that num_words limits the vocabulary size and words outside are ignored.
✗ Incorrect
The tokenizer assigns indexes based on word frequency. Since num_words=5, only top 4 words are kept (indexes 1 to 4). Words outside this range are ignored in sequences.
❓ Model Choice
intermediateChoosing Tokenizer Parameters for Vocabulary Size
You want to limit your tokenizer vocabulary to the 10 most frequent words in your dataset. Which parameter setting achieves this?
Attempts:
2 left
💡 Hint
num_words includes the top words starting from index 1.
✗ Incorrect
Setting num_words=10 keeps the top 9 words plus index 0 is reserved, so effectively 9 words are kept.
❓ Metrics
advancedCalculating Out-of-Vocabulary Token Count
Given the tokenizer below, how many tokens in the sequences are replaced by the out-of-vocabulary token?
TensorFlow
from tensorflow.keras.preprocessing.text import Tokenizer texts = ['apple banana orange', 'banana kiwi mango'] tokenizer = Tokenizer(num_words=4, oov_token='<OOV>') tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) flat_tokens = [token for seq in sequences for token in seq] oov_count = flat_tokens.count(tokenizer.word_index['<OOV>']) print(oov_count)
Attempts:
2 left
💡 Hint
Check which words are outside the top 3 words (num_words=4 means top 3 plus OOV).
✗ Incorrect
The tokenizer keeps top 3 words plus OOV token. Words 'kiwi' and 'mango' are out-of-vocabulary and replaced by OOV token, so count is 2.
🔧 Debug
advancedDebugging Tokenizer Sequence Length Mismatch
You run the code below but get sequences of different lengths. What is the best way to fix this so all sequences have the same length?
TensorFlow
from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences texts = ['I love AI', 'AI is amazing and fun'] tokenizer = Tokenizer() tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) print(sequences)
Attempts:
2 left
💡 Hint
Sequences of different lengths are common; padding fixes this for model input.
✗ Incorrect
pad_sequences adds zeros to shorter sequences so all have equal length, which is required for batch processing in models.
🧠 Conceptual
expertUnderstanding Tokenizer's word_index Behavior
After fitting a Tokenizer on texts, what does the word_index dictionary represent?
Attempts:
2 left
💡 Hint
Think about how Tokenizer converts words to numbers for models.
✗ Incorrect
word_index maps each word to a unique integer index, assigned by descending frequency order.
