0
0
PyTorchml~20 mins

nn.RNN layer in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RNN Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape of nn.RNN with batch_first=True
Consider the following PyTorch code using nn.RNN with batch_first=True. What is the shape of the output tensor `out`?
PyTorch
import torch
import torch.nn as nn

rnn = nn.RNN(input_size=5, hidden_size=3, num_layers=1, batch_first=True)
input_tensor = torch.randn(4, 7, 5)  # batch=4, seq_len=7, input_size=5
out, hn = rnn(input_tensor)
print(out.shape)
Atorch.Size([4, 7, 3])
Btorch.Size([7, 4, 3])
Ctorch.Size([4, 3, 7])
Dtorch.Size([7, 3, 4])
Attempts:
2 left
💡 Hint
Remember that batch_first=True means the batch dimension is the first dimension in the input and output.
Model Choice
intermediate
2:00remaining
Choosing RNN parameters for sequence classification
You want to build a simple RNN model to classify sequences of length 10 with 8 features each into 4 classes. Which nn.RNN configuration is correct for this task?
Ann.RNN(input_size=8, hidden_size=4, num_layers=1, batch_first=False)
Bnn.RNN(input_size=10, hidden_size=4, num_layers=1, batch_first=False)
Cnn.RNN(input_size=4, hidden_size=10, num_layers=2, batch_first=True)
Dnn.RNN(input_size=8, hidden_size=16, num_layers=1, batch_first=True)
Attempts:
2 left
💡 Hint
Input size should match the number of features per time step.
Hyperparameter
advanced
2:00remaining
Effect of increasing num_layers in nn.RNN
What is the main effect of increasing the num_layers parameter in nn.RNN from 1 to 3?
AThe input size of the RNN will automatically triple.
BThe RNN will have 3 stacked recurrent layers, allowing it to learn more complex patterns.
CThe output size of the RNN will be three times larger.
DThe RNN will process sequences three times faster.
Attempts:
2 left
💡 Hint
Think about what stacking layers means in neural networks.
🔧 Debug
advanced
2:00remaining
Identifying error in nn.RNN input shape
What error will this code raise when running the RNN forward pass?
PyTorch
import torch
import torch.nn as nn

rnn = nn.RNN(input_size=6, hidden_size=4, num_layers=1)
input_tensor = torch.randn(5, 6, 6)  # batch=5, seq_len=6, input_size=6
out, hn = rnn(input_tensor)
ARuntimeError: Expected input of shape (seq_len, batch, input_size), but got (batch, seq_len, input_size)
BNo error, runs successfully
CTypeError: input_tensor must be a list, not a tensor
DValueError: hidden_size must be equal to input_size
Attempts:
2 left
💡 Hint
Check the default expected input shape for nn.RNN when batch_first is False.
Metrics
expert
2:00remaining
Interpreting training loss behavior of nn.RNN model
You train an nn.RNN model for sequence prediction. The training loss decreases steadily, but the validation loss starts increasing after some epochs. What does this indicate?
AThe learning rate is too low, causing slow convergence.
BThe model is underfitting and needs more training.
CThe model is overfitting the training data and not generalizing well.
DThe batch size is too large, causing unstable training.
Attempts:
2 left
💡 Hint
Think about what it means when validation loss increases while training loss decreases.