Bird
0
0

Identify the error in this RNN text generation model code: ```python model = tf.keras.Sequential([ tf.keras.layers.Embedding(input_dim=5000, output_dim=64), tf.keras.layers.SimpleRNN(128), tf.keras.layers.Dense(5000, activation='softmax') ]) model.compile(loss='categorical_crossentropy', optimizer='adam') ```

medium📝 Debug Q6 of 15
NLP - Text Generation
Identify the error in this RNN text generation model code: ```python model = tf.keras.Sequential([ tf.keras.layers.Embedding(input_dim=5000, output_dim=64), tf.keras.layers.SimpleRNN(128), tf.keras.layers.Dense(5000, activation='softmax') ]) model.compile(loss='categorical_crossentropy', optimizer='adam') ```
ADense layer units should be 128, not 5000
BMissing input_length in Embedding layer
CLoss function should be sparse_categorical_crossentropy for integer labels
DSimpleRNN layer should have return_sequences=True
Step-by-Step Solution
Solution:
  1. Step 1: Check loss function compatibility

    Using 'categorical_crossentropy' requires one-hot encoded labels, but text generation usually uses integer labels, so 'sparse_categorical_crossentropy' is better.
  2. Step 2: Verify other parts

    Input_length is optional; Dense units match vocab size; return_sequences=False is valid for final output.
  3. Final Answer:

    Loss function should be sparse_categorical_crossentropy for integer labels -> Option C
  4. Quick Check:

    Use sparse_categorical_crossentropy for integer labels [OK]
Quick Trick: Match loss to label format: sparse for integers [OK]
Common Mistakes:
MISTAKES
  • Assuming categorical_crossentropy works with integer labels
  • Thinking input_length is always required
  • Confusing output units with RNN units

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes