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
Recall & Review
beginner
What is the purpose of the nn.RNN layer in PyTorch?
The nn.RNN layer processes sequences of data by passing information from one time step to the next, allowing the model to learn patterns over time.
Click to reveal answer
beginner
What are the main inputs to an nn.RNN layer?
The main inputs are the sequence data (shape: seq_len, batch_size, input_size) and an optional initial hidden state (shape: num_layers * num_directions, batch_size, hidden_size).
Click to reveal answer
beginner
What does the 'hidden_size' parameter control in nn.RNN?
It controls the size of the hidden state vector, which stores information from previous time steps and affects the model's capacity to learn patterns.
Click to reveal answer
intermediate
How does nn.RNN handle multiple layers and directions?
You can set 'num_layers' to stack multiple RNN layers, and 'bidirectional=True' to process sequences forwards and backwards, doubling the output size.
Click to reveal answer
beginner
What are the outputs of nn.RNN layer?
It outputs 'output' (all hidden states for each time step) and 'hidden' (the last hidden state for each layer and direction).
Click to reveal answer
What shape should the input sequence to nn.RNN have?
Abatch_size, seq_len, input_size
Bseq_len, batch_size, input_size
Cinput_size, seq_len, batch_size
Dbatch_size, input_size, seq_len
✗ Incorrect
The nn.RNN expects input with shape (seq_len, batch_size, input_size).
What does setting 'bidirectional=True' do in nn.RNN?
AProcesses the sequence forwards and backwards
BStacks multiple RNN layers
CChanges the activation function
DDisables the hidden state
✗ Incorrect
Bidirectional RNN processes the input sequence in both forward and backward directions.
Which of these is NOT an output of nn.RNN?
AOutput for all time steps
BLast hidden state
CPredicted class labels
DNone of the above
✗ Incorrect
nn.RNN outputs hidden states, not direct class predictions.
What does the 'hidden_size' parameter affect?
ASize of the hidden state vector
BLength of the input sequence
CNumber of layers
DBatch size
✗ Incorrect
'hidden_size' controls the size of the hidden state vector in the RNN.
How can you provide an initial hidden state to nn.RNN?
ABy setting a parameter during initialization
BYou cannot provide an initial hidden state
CBy modifying the input sequence
DBy passing it as the second argument to the forward method
✗ Incorrect
You can pass the initial hidden state as the second argument when calling the RNN layer.
Explain how the nn.RNN layer processes a sequence of data step-by-step.
Think about how information flows through time steps in the RNN.
You got /4 concepts.
Describe the difference between a unidirectional and bidirectional nn.RNN layer.
Consider how reading the sequence backwards adds information.
You got /3 concepts.
Practice
(1/5)
1. What does the nn.RNN layer in PyTorch primarily do?
easy
A. Processes sequences step by step, keeping track of past information
B. Sorts input data in ascending order
C. Generates random numbers for initialization
D. Performs matrix multiplication without memory
Solution
Step 1: Understand the purpose of RNN
The RNN layer is designed to handle sequential data by processing one step at a time and remembering previous steps.
Step 2: Compare options with RNN behavior
Only Processes sequences step by step, keeping track of past information describes this behavior correctly; others describe unrelated functions.
Final Answer:
Processes sequences step by step, keeping track of past information -> Option A
Quick Check:
RNN remembers past inputs = A [OK]
Hint: RNNs remember past steps in sequences [OK]
Common Mistakes:
Thinking RNN sorts data
Confusing RNN with random number generators
Assuming RNN does simple matrix multiplication only
2. Which of the following is the correct way to create an RNN layer with input size 10 and hidden size 20 in PyTorch?
easy
A. nn.RNN(20, 10)
B. nn.RNN(10)
C. nn.RNN(input_size=10, hidden_size=20)
D. nn.RNN(hidden_size=10, input_size=20)
Solution
Step 1: Recall nn.RNN constructor parameters
The constructor requires input_size first, then hidden_size, e.g., nn.RNN(input_size=10, hidden_size=20).
Step 2: Check each option
Only nn.RNN(input_size=10, hidden_size=20) matches the correct parameter order and names; the others reverse sizes, omit hidden_size, or swap parameters.
Final Answer:
nn.RNN(input_size=10, hidden_size=20) -> Option C
Quick Check:
Input size first, hidden size second = D [OK]
Hint: Remember: input_size before hidden_size in nn.RNN [OK]
Common Mistakes:
Swapping input_size and hidden_size
Omitting hidden_size parameter
Using positional args in wrong order
3. Given the code below, what is the shape of output after running the RNN?
PyTorch recommends padding sequences to equal length and using pack_padded_sequence to inform RNN about actual lengths.
Step 2: Evaluate options for best practice
Pad sequences to the same length and use pack_padded_sequence before the RNN correctly describes this approach. Options B and C ignore padding/packing, causing errors or inefficiency. Set hidden_size equal to the longest sequence length is unrelated to sequence length handling.
Final Answer:
Pad sequences to the same length and use pack_padded_sequence before the RNN -> Option A
Quick Check:
Use padding + pack_padded_sequence for variable lengths = A [OK]
Hint: Pad and pack sequences before RNN for variable lengths [OK]