Bird
0
0

What will be the shape of the hidden state tensor returned by the GRU in the following code?

medium📝 Predict Output Q5 of 15
NLP - Sequence Models for NLP
What will be the shape of the hidden state tensor returned by the GRU in the following code? ```python gru = nn.GRU(input_size=15, hidden_size=30, num_layers=2) input_tensor = torch.randn(3, 5, 15) # seq_len=3, batch=5, input_size=15 output, hidden = gru(input_tensor) print(hidden.shape) ```
A(2, 5, 30)
B(5, 3, 30)
C(3, 5, 15)
D(5, 2, 30)
Step-by-Step Solution
Solution:
  1. Step 1: Recall hidden state shape for multi-layer GRU

    Hidden shape is (num_layers, batch_size, hidden_size).
  2. Step 2: Match code parameters to shape

    num_layers=2, batch=5, hidden_size=30, so hidden shape is (2,5,30).
  3. Final Answer:

    (2, 5, 30) -> Option A
  4. Quick Check:

    Hidden shape = (num_layers, batch, hidden_size) [OK]
Quick Trick: Hidden state shape starts with num_layers, then batch [OK]
Common Mistakes:
MISTAKES
  • Mixing sequence length with batch size
  • Confusing output and hidden shapes
  • Ignoring num_layers dimension

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes