Bird
0
0

Given this code snippet, what will be the shape of the output from the Bidirectional LSTM layer?

medium📝 Predict Output Q4 of 15
NLP - Sequence Models for NLP
Given this code snippet, what will be the shape of the output from the Bidirectional LSTM layer?
from tensorflow.keras.layers import Bidirectional, LSTM, Input
from tensorflow.keras.models import Model

inputs = Input(shape=(10, 8))
lstm = Bidirectional(LSTM(16, return_sequences=False))(inputs)
model = Model(inputs, lstm)
print(model.output_shape)
A(None, 32)
B(None, 16)
C(None, 10, 32)
D(None, 10, 16)
Step-by-Step Solution
Solution:
  1. Step 1: Understand input and LSTM parameters

    Input shape is (10, 8) meaning 10 time steps, 8 features. LSTM units=16, return_sequences=False means output is last output only.
  2. Step 2: Calculate output shape of Bidirectional LSTM

    Bidirectional doubles units: 16 * 2 = 32. Since return_sequences=False, output shape is (batch_size, 32).
  3. Final Answer:

    (None, 32) -> Option A
  4. Quick Check:

    Output shape = (batch, units * 2) when bidirectional and return_sequences=False [OK]
Quick Trick: Bidirectional doubles units; return_sequences=False outputs last step only [OK]
Common Mistakes:
MISTAKES
  • Forgetting bidirectional doubles units
  • Confusing return_sequences output shape
  • Assuming output keeps time steps

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes