Bird
0
0

Find the bug in this code snippet for generating text with an RNN model: ```python seed_text = "hello" for _ in range(5): token_list = tokenizer.texts_to_sequences([seed_text])[0] token_list = np.array(token_list).reshape(1, -1) predicted = model.predict(token_list) next_word = np.argmax(predicted) seed_text += " " + tokenizer.index_word[next_word] print(seed_text) ```

medium📝 Debug Q7 of 15
NLP - Text Generation
Find the bug in this code snippet for generating text with an RNN model: ```python seed_text = "hello" for _ in range(5): token_list = tokenizer.texts_to_sequences([seed_text])[0] token_list = np.array(token_list).reshape(1, -1) predicted = model.predict(token_list) next_word = np.argmax(predicted) seed_text += " " + tokenizer.index_word[next_word] print(seed_text) ```
Anp.argmax should be applied on axis=1
Btokenizer.index_word does not exist, should use tokenizer.word_index
Ctoken_list shape should be (1, sequence_length), but reshape(-1) may cause wrong shape
Dmodel.predict should be called with batch size 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify tokenizer attribute error

    Keras Tokenizer does not create 'index_word' attribute automatically. It must be added manually: tokenizer.index_word = {i: w for w, i in tokenizer.word_index.items()}.
  2. Step 2: Verify other parts

    reshape(1, -1) correctly forms (1, seq_len); np.argmax works on flattened predictions for batch_size=1; batch size is correct.
  3. Final Answer:

    tokenizer.index_word does not exist, should use tokenizer.word_index -> Option B
  4. Quick Check:

    Create index_word from word_index [OK]
Quick Trick: Keras Tokenizer lacks index_word; build from word_index [OK]
Common Mistakes:
MISTAKES
  • Using wrong tokenizer attribute for index_word
  • Misusing np.argmax axis parameter
  • Passing invalid batch size to predict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes