Bird
0
0

You wrote this code to build an RNN model for text classification but get an error:

medium📝 Debug Q14 of 15
NLP - Sequence Models for NLP
You wrote this code to build an RNN model for text classification but get an error:
model = Sequential()
model.add(SimpleRNN(16))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

What is the most likely cause of the error?
ADense layer cannot have sigmoid activation
BSimpleRNN units must be 32 or more
CMissing input shape for the first SimpleRNN layer
DLoss function 'binary_crossentropy' is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check first layer requirements

    The first RNN layer must know input shape to accept data; missing input_shape causes error.
  2. Step 2: Validate other options

    Sigmoid activation in Dense is valid for binary classification; units can be any positive integer; binary_crossentropy is valid loss.
  3. Final Answer:

    Missing input shape for the first SimpleRNN layer -> Option C
  4. Quick Check:

    First RNN layer needs input_shape = B [OK]
Quick Trick: Always set input_shape in first RNN layer to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Assuming activation or loss function causes error
  • Thinking units must be 32 or more
  • Ignoring input shape requirement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes