Bird
0
0

Consider this code snippet using TensorFlow Keras:

medium📝 Predict Output Q13 of 15
NLP - Sequence Models for NLP
Consider this code snippet using TensorFlow Keras:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Bidirectional, Dense

model = Sequential()
model.add(Bidirectional(LSTM(10, return_sequences=False), input_shape=(5, 8)))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')

import numpy as np
x = np.random.random((2, 5, 8))
pred = model.predict(x)
print(pred.shape)

What will be the shape of pred?
A(2, 10)
B(2, 1)
C(5, 1)
D(2, 20)
Step-by-Step Solution
Solution:
  1. Step 1: Understand model output shape

    The Bidirectional LSTM with 10 units outputs 20 features (10 forward + 10 backward) per timestep. Since return_sequences=False, it outputs only the last timestep's features, shape (batch_size, 20).
  2. Step 2: Dense layer output shape

    The Dense layer with 1 unit outputs shape (batch_size, 1). Input batch size is 2, so output shape is (2, 1).
  3. Final Answer:

    (2, 1) -> Option B
  4. Quick Check:

    Batch size 2, Dense 1 unit = (2, 1) [OK]
Quick Trick: Dense(1) outputs shape (batch_size, 1) [OK]
Common Mistakes:
MISTAKES
  • Confusing return_sequences=True vs False
  • Forgetting bidirectional doubles units
  • Mixing batch and timestep dimensions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes