Bird
0
0

You wrote this code to train an RNN text generator but get a shape mismatch error:

medium📝 Debug Q14 of 15
NLP - Text Generation
You wrote this code to train an RNN text generator but get a shape mismatch error:
model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=64, input_length=15))
model.add(SimpleRNN(128))
model.add(Dense(10000, activation='softmax'))

X = np.random.randint(0, 10000, (64, 15))
y = np.random.randint(0, 10000, (64, 15))  # target labels

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
model.fit(X, y, epochs=5)

What is the main issue causing the error?
ATarget labels y should be shape (64,) with integer word indices, not (64, 15)
BEmbedding input_dim is too large
CSimpleRNN units should match output_dim of embedding
DLoss function sparse_categorical_crossentropy is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check target label shape for next word prediction

    For next word prediction, y should be a 1D array of word indices (batch_size,), not sequences.
  2. Step 2: Identify mismatch in y shape

    y has shape (64, 15), which causes shape mismatch with model output (64, 10000).
  3. Final Answer:

    Target labels y should be shape (64,) with integer word indices, not (64, 15) -> Option A
  4. Quick Check:

    y shape must match output shape = B [OK]
Quick Trick: Targets for next word are 1D, not sequences [OK]
Common Mistakes:
MISTAKES
  • Using sequences as targets instead of next word
  • Confusing embedding size with RNN units
  • Changing loss function unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes