Bird
Raised Fist0
PyTorchml~20 mins

Bidirectional RNNs in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Bidirectional RNN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Bidirectional RNN Outputs

In a bidirectional RNN, what does the output at each time step represent?

AThe output is the sum of all hidden states across all time steps.
BThe output only contains information from past time steps up to the current step.
CThe output only contains information from future time steps after the current step.
DThe output combines information from past and future time steps relative to the current step.
Attempts:
2 left
💡 Hint

Think about how information flows in both directions in a bidirectional RNN.

Predict Output
intermediate
2:00remaining
Output Shape of a Bidirectional RNN in PyTorch

Given the following PyTorch code, what is the shape of output?

PyTorch
import torch
import torch.nn as nn

rnn = nn.RNN(input_size=10, hidden_size=20, num_layers=1, bidirectional=True)
input_seq = torch.randn(5, 3, 10)  # seq_len=5, batch=3, input_size=10
output, hidden = rnn(input_seq)
print(output.shape)
Atorch.Size([3, 5, 40])
Btorch.Size([5, 3, 20])
Ctorch.Size([5, 3, 40])
Dtorch.Size([3, 5, 20])
Attempts:
2 left
💡 Hint

Remember the output shape is (seq_len, batch, num_directions * hidden_size).

Model Choice
advanced
1:30remaining
Choosing Bidirectional RNN for Sequence Tasks

For which of the following tasks is using a bidirectional RNN most beneficial?

AClassifying the sentiment of a complete sentence after reading it entirely.
BPredicting the next word in a sentence given only previous words.
CGenerating text one character at a time from left to right.
DReal-time speech recognition where future audio frames are not available.
Attempts:
2 left
💡 Hint

Consider if future context is available when making predictions.

Hyperparameter
advanced
2:00remaining
Effect of Bidirectionality on Hidden State Size

If you set hidden_size=50 in a bidirectional RNN with num_layers=1, what is the size of the hidden state tensor h_n returned by PyTorch?

Atorch.Size([2, batch_size, 50])
Btorch.Size([1, batch_size, 100])
Ctorch.Size([1, batch_size, 50])
Dtorch.Size([2, batch_size, 100])
Attempts:
2 left
💡 Hint

Remember the first dimension of h_n is num_layers * num_directions.

🔧 Debug
expert
2:30remaining
Identifying Error in Bidirectional RNN Output Usage

Consider this PyTorch code snippet:

import torch
import torch.nn as nn

rnn = nn.RNN(input_size=8, hidden_size=16, bidirectional=True)
input_seq = torch.randn(7, 4, 8)
output, hidden = rnn(input_seq)
last_output = output[-1]
print(last_output.shape)

What is the shape of last_output and why might using it directly be problematic for sequence classification?

Atorch.Size([4, 16]); It only contains forward direction output, missing backward context.
Btorch.Size([4, 32]); It combines forward and backward outputs at the last time step, but backward output corresponds to the first input step, so it may not represent the full sequence context.
Ctorch.Size([7, 4, 32]); It is the full output sequence, so no problem using it directly.
Dtorch.Size([4, 16]); It only contains backward direction output, missing forward context.
Attempts:
2 left
💡 Hint

Think about what the last time step output means in a bidirectional RNN.

Practice

(1/5)
1. What is the main advantage of using a bidirectional RNN compared to a standard RNN?
easy
A. It processes the input sequence in both forward and backward directions to capture full context.
B. It uses fewer parameters to reduce model size.
C. It only processes sequences backward for faster training.
D. It replaces recurrent layers with convolutional layers.

Solution

  1. Step 1: Understand standard RNN processing

    Standard RNNs process sequences only in the forward direction, so they only see past context.
  2. Step 2: Analyze bidirectional RNN behavior

    Bidirectional RNNs process sequences both forward and backward, capturing past and future context.
  3. Final Answer:

    It processes the input sequence in both forward and backward directions to capture full context. -> Option A
  4. Quick Check:

    Bidirectional = forward + backward context [OK]
Hint: Bidirectional means reading sequence both ways [OK]
Common Mistakes:
  • Thinking bidirectional reduces parameters
  • Assuming it only reads backward
  • Confusing with convolutional layers
2. Which of the following is the correct way to create a bidirectional GRU layer in PyTorch?
easy
A. torch.nn.GRU(input_size=10, hidden_size=20, direction='both')
B. torch.nn.GRU(input_size=10, hidden_size=20, bidirectional=True)
C. torch.nn.GRU(input_size=10, hidden_size=20, bidirectional=False)
D. torch.nn.GRU(input_size=10, hidden_size=20, two_directions=True)

Solution

  1. Step 1: Recall PyTorch GRU parameters

    The bidirectional parameter is a boolean that enables bidirectional processing.
  2. Step 2: Identify correct syntax

    Only torch.nn.GRU(input_size=10, hidden_size=20, bidirectional=True) uses bidirectional=True, which is the correct PyTorch syntax.
  3. Final Answer:

    torch.nn.GRU(input_size=10, hidden_size=20, bidirectional=True) -> Option B
  4. Quick Check:

    bidirectional=True enables two directions [OK]
Hint: Use bidirectional=True to enable both directions [OK]
Common Mistakes:
  • Using invalid parameter names like 'direction' or 'two_directions'
  • Setting bidirectional=False by mistake
  • Confusing input_size and hidden_size
3. Given the following PyTorch code, what is the shape of the output tensor?
rnn = torch.nn.RNN(input_size=5, hidden_size=3, bidirectional=True, batch_first=True)
input = torch.randn(4, 7, 5)  # batch=4, seq_len=7, input_size=5
output, _ = rnn(input)
medium
A. [4, 7, 3]
B. [7, 4, 6]
C. [4, 7, 6]
D. [4, 3, 7]

Solution

  1. Step 1: Understand output shape of bidirectional RNN

    Output shape is (batch_size, seq_len, hidden_size * num_directions). Here, num_directions=2.
  2. Step 2: Calculate output shape

    hidden_size=3, so output last dimension = 3 * 2 = 6. Batch=4, seq_len=7, so output shape = [4, 7, 6].
  3. Final Answer:

    [4, 7, 6] -> Option C
  4. Quick Check:

    Output last dim = hidden_size * 2 [OK]
Hint: Output last dim doubles with bidirectional=True [OK]
Common Mistakes:
  • Forgetting to multiply hidden_size by 2
  • Mixing batch and sequence dimensions
  • Assuming output shape matches input exactly
4. You wrote this code but get a runtime error:
rnn = torch.nn.RNN(input_size=8, hidden_size=4, bidirectional=True)
input = torch.randn(5, 10, 8)
output, hidden = rnn(input)

What is the likely cause of the error?
medium
A. Input tensor shape should have batch_first=True or be transposed to (seq_len, batch, input_size).
B. hidden_size must be equal to input_size for bidirectional RNNs.
C. bidirectional=True is not supported for RNN layers.
D. The input tensor must be 2D, not 3D.

Solution

  1. Step 1: Check default input shape for PyTorch RNN

    By default, PyTorch RNN expects input shape (seq_len, batch, input_size) unless batch_first=True is set.
  2. Step 2: Analyze given input shape

    Input shape is (5, 10, 8) which is (batch, seq_len, input_size), but batch_first=True is not set, causing mismatch.
  3. Final Answer:

    Input tensor shape should have batch_first=True or be transposed to (seq_len, batch, input_size). -> Option A
  4. Quick Check:

    Default RNN input shape = (seq_len, batch, input_size) [OK]
Hint: Set batch_first=True if input shape is (batch, seq_len, input_size) [OK]
Common Mistakes:
  • Assuming bidirectional disables shape rules
  • Thinking hidden_size must match input_size
  • Passing 2D input instead of 3D
5. You want to build a sentiment analysis model using a bidirectional LSTM in PyTorch. The input sequences have variable lengths. Which approach correctly handles variable-length sequences with a bidirectional LSTM?
hard
A. Manually reverse sequences and concatenate outputs without using bidirectional=True.
B. Pad sequences to max length and feed directly without packing, with bidirectional=False.
C. Use only forward LSTM and ignore sequence lengths.
D. Use pack_padded_sequence before the LSTM and pad_packed_sequence after, with batch_first=True and bidirectional=True set.

Solution

  1. Step 1: Understand variable-length sequence handling

    PyTorch requires packing padded sequences to efficiently process variable-length inputs in RNNs.
  2. Step 2: Apply packing with bidirectional LSTM

    Use pack_padded_sequence before feeding to LSTM with bidirectional=True, then unpack with pad_packed_sequence.
  3. Final Answer:

    Use pack_padded_sequence before the LSTM and pad_packed_sequence after, with batch_first=True and bidirectional=True set. -> Option D
  4. Quick Check:

    Pack sequences for variable length + bidirectional LSTM [OK]
Hint: Pack sequences to handle variable lengths with bidirectional LSTM [OK]
Common Mistakes:
  • Ignoring packing and feeding padded sequences directly
  • Disabling bidirectional for variable lengths
  • Manually reversing sequences instead of using bidirectional flag