Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a tokenizer that splits text into words.
TensorFlow
from tensorflow.keras.preprocessing.text import Tokenizer texts = ['I love machine learning'] tokenizer = Tokenizer(num_words=100) tokenizer.[1](texts)
Attempts:
3 left
💡 Hint
Common Mistakes
Using texts_to_sequences before fitting the tokenizer.
Trying to call a non-existent method 'tokenize'.
✗ Incorrect
The method fit_on_texts() trains the tokenizer on the given texts to build the word index.
2fill in blank
mediumComplete the code to convert texts into sequences of integers.
TensorFlow
sequences = tokenizer.[1](texts)Attempts:
3 left
💡 Hint
Common Mistakes
Calling fit_on_texts again instead of converting texts.
Using sequences_to_texts which does the opposite.
✗ Incorrect
texts_to_sequences() converts texts into lists of integer tokens based on the tokenizer's word index.
3fill in blank
hardFix the error in the code to get the word index dictionary.
TensorFlow
word_index = tokenizer.[1]Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after word_index causing a TypeError.
Trying to call a non-existent method get_word_index.
✗ Incorrect
word_index is an attribute, not a method, so no parentheses are needed.
4fill in blank
hardFill both blanks to create a dictionary of word counts from texts.
TensorFlow
word_counts = tokenizer.[1] print(word_counts.[2]('machine'))
Attempts:
3 left
💡 Hint
Common Mistakes
Using word_index instead of word_counts for counts.
Using count() which is not a dictionary method.
✗ Incorrect
word_counts is an attribute holding word frequencies; get() retrieves the count for a given word safely.
5fill in blank
hardFill all three blanks to create sequences and pad them to length 5.
TensorFlow
from tensorflow.keras.preprocessing.sequence import pad_sequences sequences = tokenizer.[1](texts) padded = pad_sequences(sequences, maxlen=[2], padding=[3]) print(padded)
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit_on_texts instead of texts_to_sequences for sequences.
Setting maxlen too large or too small.
Using 'post' padding instead of 'pre'.
✗ Incorrect
texts_to_sequences converts texts to sequences; maxlen=3 sets length; padding='pre' adds zeros at start.
