Bird
0
0

Find the bug in this GRU implementation snippet: ```python gru = nn.GRU(input_size=30, hidden_size=50, batch_first=True) input_tensor = torch.randn(4, 10, 30) hidden = torch.randn(1, 4, 50) output, hidden = gru(input_tensor, hidden) print(output.shape) ```

medium📝 Debug Q7 of 15
NLP - Sequence Models for NLP
Find the bug in this GRU implementation snippet: ```python gru = nn.GRU(input_size=30, hidden_size=50, batch_first=True) input_tensor = torch.randn(4, 10, 30) hidden = torch.randn(1, 4, 50) output, hidden = gru(input_tensor, hidden) print(output.shape) ```
AHidden state shape should be (1, 4, 30) instead of (1, 4, 50)
BHidden state shape is correct; no bug present
CInput tensor shape should be (4, 30, 10) for batch_first=True
DHidden state should be initialized with zeros, not random values
Step-by-Step Solution
Solution:
  1. Step 1: Check input tensor shape with batch_first=True

    Input shape (4,10,30) matches (batch=4, seq_len=10, input_size=30).
  2. Step 2: Verify hidden state shape

    Hidden shape (1,4,50) matches (num_layers=1, batch=4, hidden_size=50).
  3. Step 3: Confirm hidden initialization method

    Random initialization is allowed; no strict requirement for zeros.
  4. Final Answer:

    Hidden state shape is correct; no bug present -> Option B
  5. Quick Check:

    Shapes match GRU expected dimensions [OK]
Quick Trick: Hidden shape = (num_layers, batch, hidden_size) always [OK]
Common Mistakes:
MISTAKES
  • Confusing input tensor dimension order
  • Assuming hidden must be zeros
  • Mixing hidden size with input size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes