RNNs can remember past information to understand data that comes in order, like sentences or time steps.
Why RNNs handle sequences in PyTorch
Start learning this pattern below
Jump into concepts and practice - no test required
rnn = torch.nn.RNN(input_size, hidden_size, num_layers) output, hidden = rnn(input_sequence, hidden_state)
input_sequence shape is (sequence_length, batch_size, input_size).
hidden_state holds memory from previous steps and is optional for the first input.
rnn = torch.nn.RNN(10, 20, 1) input_seq = torch.randn(5, 1, 10) output, hidden = rnn(input_seq)
hidden = torch.zeros(1, 1, 20) output, hidden = rnn(input_seq, hidden)
This code creates a simple RNN to process a sequence of 4 steps, each with 3 features. It shows how the RNN outputs a result for each step and keeps a hidden state that remembers past information.
import torch import torch.nn as nn # Define RNN parameters input_size = 3 hidden_size = 5 sequence_length = 4 batch_size = 1 # Create RNN layer rnn = nn.RNN(input_size, hidden_size, num_layers=1) # Create a random input sequence (sequence_length, batch_size, input_size) input_seq = torch.randn(sequence_length, batch_size, input_size) # Initialize hidden state with zeros hidden = torch.zeros(1, batch_size, hidden_size) # Forward pass through RNN output, hidden = rnn(input_seq, hidden) print("Input sequence shape:", input_seq.shape) print("Output shape:", output.shape) print("Hidden state shape:", hidden.shape) print("Output at last time step:", output[-1])
RNNs process data step by step, passing information forward through hidden states.
They are good for sequences but can struggle with very long sequences due to forgetting.
RNNs remember past inputs to understand sequences.
They take input one step at a time and keep a hidden state as memory.
This makes them useful for language, time series, and other ordered data.
Practice
Solution
Step 1: Understand RNN memory mechanism
RNNs keep a hidden state that stores information from previous inputs, acting like memory.Step 2: Relate memory to sequence handling
This memory lets RNNs understand order and context in sequences like sentences or time series.Final Answer:
Because they keep a memory of previous inputs using a hidden state -> Option BQuick Check:
RNN memory = sequence understanding [OK]
- Thinking RNNs process all inputs at once
- Confusing RNNs with convolutional networks
- Assuming RNNs ignore past data
Solution
Step 1: Recall PyTorch RNN syntax
PyTorch uses torch.nn.RNN with parameters input_size and hidden_size.Step 2: Check options for correct parameter order and names
rnn = torch.nn.RNN(input_size=10, hidden_size=20, num_layers=1) correctly uses input_size=10 and hidden_size=20 with num_layers=1.Final Answer:
rnn = torch.nn.RNN(input_size=10, hidden_size=20, num_layers=1) -> Option AQuick Check:
Correct PyTorch RNN init = rnn = torch.nn.RNN(input_size=10, hidden_size=20, num_layers=1) [OK]
- Using non-existent classes like RNNLayer or SimpleRNN
- Swapping input_size and hidden_size
- Missing required parameters
import torch
rnn = torch.nn.RNN(input_size=5, hidden_size=3, num_layers=1)
input_seq = torch.randn(4, 2, 5) # seq_len=4, batch=2, input_size=5
output, hidden = rnn(input_seq)
Solution
Step 1: Understand RNN input and output shapes
Input shape is (seq_len=4, batch=2, input_size=5). Output shape is (seq_len, batch, hidden_size).Step 2: Apply hidden_size to output shape
Hidden size is 3, so output shape is (4, 2, 3).Final Answer:
(4, 2, 3) -> Option CQuick Check:
Output shape = (seq_len, batch, hidden_size) = (4, 2, 3) [OK]
- Mixing batch and sequence dimensions
- Confusing hidden_size with input_size
- Assuming output shape swaps batch and seq_len
rnn = torch.nn.RNN(input_size=8, hidden_size=4)
input_seq = torch.randn(5, 3, 10) # seq_len=5, batch=3, input_size=10
output, hidden = rnn(input_seq)
Solution
Step 1: Check input_size consistency
RNN expects input_size=8 but input_seq has last dimension 10, which is incorrect.Step 2: Verify other parameters
num_layers is optional and defaults to 1, output unpacking is correct, hidden_size can be smaller than input_size.Final Answer:
input_seq has wrong input_size dimension -> Option AQuick Check:
Input size mismatch causes error [OK]
- Assuming num_layers is mandatory
- Thinking hidden_size must be bigger than input_size
- Misunderstanding output unpacking
Solution
Step 1: Understand RNN sequence processing
RNNs process inputs step-by-step, keeping hidden state to remember past words.Step 2: Apply this to next word prediction
Feeding words one by one and using the final output leverages RNN memory to predict the next word.Final Answer:
Feed the sentence word by word to the RNN, updating hidden state each step, then predict the next word from the final output -> Option DQuick Check:
Stepwise input + hidden state = best sequence use [OK]
- Feeding entire sentence as one vector loses order
- Ignoring hidden state loses sequence memory
- Using convolution to remove sequence order
