0
0
NLPml~20 mins

GRU for text in NLP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GRU Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of GRU layer with simple input
What is the shape of the output tensor after passing a batch of 2 sequences, each of length 3 with 4 features, through a GRU layer with 5 units and return_sequences=True?
NLP
import torch
import torch.nn as nn

gru = nn.GRU(input_size=4, hidden_size=5, batch_first=True, bidirectional=False)
input_tensor = torch.randn(2, 3, 4)  # batch=2, seq_len=3, features=4
output, hidden = gru(input_tensor)
print(output.shape)
Atorch.Size([3, 2, 5])
Btorch.Size([2, 3, 5])
Ctorch.Size([2, 5])
Dtorch.Size([2, 3, 4])
Attempts:
2 left
💡 Hint
Remember that batch_first=True means batch size is the first dimension in input and output.
🧠 Conceptual
intermediate
1:30remaining
Purpose of the reset gate in GRU
What is the main role of the reset gate in a GRU cell when processing text sequences?
AIt normalizes the input features before processing.
BIt controls how much of the previous hidden state to keep for the final output.
CIt initializes the hidden state at the start of the sequence.
DIt decides how much past information to forget before calculating the new candidate activation.
Attempts:
2 left
💡 Hint
Think about which gate helps the model reset memory for new inputs.
Hyperparameter
advanced
1:30remaining
Choosing GRU hidden size for text classification
You want to build a GRU-based text classifier. Which hidden size choice is most likely to balance model capacity and training speed for a medium-sized dataset?
AHidden size = 128
BHidden size = 1000
CHidden size = 1
DHidden size = 10
Attempts:
2 left
💡 Hint
Too small hidden size limits learning; too large slows training and risks overfitting.
Metrics
advanced
2:00remaining
Evaluating GRU model performance on text data
After training a GRU model for sentiment analysis, you get these results on the test set: accuracy=0.85, precision=0.60, recall=0.95. What does this tell you about the model's predictions?
AThe model rarely finds positive cases but is very precise when it does.
BThe model has balanced precision and recall.
CThe model finds most positive cases but also has many false positives.
DThe model is overfitting the training data.
Attempts:
2 left
💡 Hint
High recall but low precision means many false alarms.
🔧 Debug
expert
2:30remaining
Identifying error in GRU input shape
You run this PyTorch code and get a runtime error: import torch import torch.nn as nn gru = nn.GRU(input_size=10, hidden_size=20, batch_first=True) input_tensor = torch.randn(5, 20, 10) output, hidden = gru(input_tensor) What is the cause of the error?
NLP
import torch
import torch.nn as nn

gru = nn.GRU(input_size=10, hidden_size=20, batch_first=True)
input_tensor = torch.randn(5, 20, 10)
output, hidden = gru(input_tensor)
AThe input tensor shape is incorrect; batch size and sequence length are swapped.
BThe GRU's hidden size must be equal to input size.
CThe input tensor shape is correct; the error is due to missing initial hidden state.
DThe input tensor's sequence length dimension is 20, which is too large for the GRU.
Attempts:
2 left
💡 Hint
Check the meaning of each dimension when batch_first=True.