0
0
PyTorchml~20 mins

Bidirectional RNNs in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.