Sequence classification helps us teach a computer to understand and label a series of items, like words in a sentence or steps in a process.
Sequence classification in PyTorch
Start learning this pattern below
Jump into concepts and practice - no test required
import torch import torch.nn as nn class SequenceClassifier(nn.Module): def __init__(self, input_size, hidden_size, num_classes): super(SequenceClassifier, self).__init__() self.rnn = nn.RNN(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, num_classes) def forward(self, x): out, _ = self.rnn(x) out = out[:, -1, :] out = self.fc(out) return out
The input to the model is a sequence of vectors (like word embeddings).
The RNN processes the sequence step by step, and we use the last output to classify the whole sequence.
model = SequenceClassifier(input_size=10, hidden_size=20, num_classes=3)
output = model(torch.randn(5, 7, 10))
This program creates a simple RNN model to classify sequences into two classes. It trains on random data for 5 steps and prints loss and accuracy.
import torch import torch.nn as nn import torch.optim as optim # Define the model class SequenceClassifier(nn.Module): def __init__(self, input_size, hidden_size, num_classes): super(SequenceClassifier, self).__init__() self.rnn = nn.RNN(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, num_classes) def forward(self, x): out, _ = self.rnn(x) out = out[:, -1, :] out = self.fc(out) return out # Parameters input_size = 5 hidden_size = 10 num_classes = 2 batch_size = 4 seq_length = 6 # Create model, loss, optimizer model = SequenceClassifier(input_size, hidden_size, num_classes) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.01) # Dummy data: batch of 4 sequences, each 6 steps, each step 5 features inputs = torch.randn(batch_size, seq_length, input_size) # Labels: 4 labels for the batch labels = torch.tensor([0, 1, 0, 1]) # Training loop for 5 epochs for epoch in range(5): optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() _, predicted = torch.max(outputs, 1) accuracy = (predicted == labels).float().mean() print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}, Accuracy: {accuracy.item():.4f}")
Sequence length and batch size can vary depending on your data.
RNNs can be replaced with LSTM or GRU for better performance on longer sequences.
Always check that your input data shape matches the model's expected input.
Sequence classification labels whole sequences, not just single items.
RNNs process sequences step by step and help capture order information.
The last output of the RNN is used to predict the class of the entire sequence.
Practice
Solution
Step 1: Understand sequence classification
Sequence classification means giving one label to the whole sequence, not to individual items.Step 2: Compare options
Only To assign a label to the entire input sequence describes labeling the entire sequence, which matches the goal of sequence classification.Final Answer:
To assign a label to the entire input sequence -> Option AQuick Check:
Sequence classification = label whole sequence [OK]
- Confusing sequence classification with sequence labeling
- Thinking it predicts next sequence item
- Assuming it generates new sequences
Solution
Step 1: Identify sequence processing modules
RNN (Recurrent Neural Network) modules process sequences step-by-step, capturing order.Step 2: Match options to sequence processing
Only torch.nn.RNN is designed for sequential data; others serve different purposes.Final Answer:
torch.nn.RNN -> Option CQuick Check:
RNN processes sequences stepwise [OK]
- Choosing Linear which is for fixed-size input
- Selecting Conv2d meant for images
- Picking BatchNorm which normalizes features
rnn = torch.nn.RNN(input_size=10, hidden_size=20, batch_first=True) inputs = torch.randn(5, 7, 10) # batch=5, seq_len=7, features=10 output, hn = rnn(inputs) final_output = hn.squeeze(0)
Solution
Step 1: Understand RNN output shapes
Output shape is (batch, seq_len, hidden_size) = (5,7,20). hn shape is (num_layers, batch, hidden_size) = (1,5,20).Step 2: Analyze final_output shape
hn.squeeze(0) removes the first dimension (num_layers), resulting in (5,20).Final Answer:
[5, 20] -> Option AQuick Check:
hn.squeeze(0) shape = [batch, hidden_size] = [5, 20] [OK]
- Confusing output and hn shapes
- Not squeezing the layer dimension
- Mixing sequence length with batch size
class SeqClassifier(torch.nn.Module):
def __init__(self):
super().__init__()
self.rnn = torch.nn.RNN(10, 20, batch_first=True)
self.fc = torch.nn.Linear(10, 2)
def forward(self, x):
out, hn = self.rnn(x)
out = self.fc(hn.squeeze(0))
return outSolution
Step 1: Check Linear layer input size
The RNN hidden size is 20, so hn has shape (batch, 20). The Linear layer expects input size 10, which is incorrect.Step 2: Correct Linear input size
Linear layer input size must match hidden size 20 to process hn correctly.Final Answer:
The Linear layer input size should be 20, not 10 -> Option DQuick Check:
Linear input size = hidden size = 20 [OK]
- Mismatching Linear input size with hidden size
- Applying squeeze to wrong tensor
- Returning wrong tensor from forward
Solution
Step 1: Understand variable-length sequence handling
Sequences must be padded to the same length for batch processing, then packed to ignore padding during RNN.Step 2: Evaluate options
Pad sequences to the same length and use pack_padded_sequence before RNN uses padding plus pack_padded_sequence, the correct PyTorch method to handle varying lengths efficiently.Final Answer:
Pad sequences to the same length and use pack_padded_sequence before RNN -> Option BQuick Check:
Use padding + pack_padded_sequence for variable lengths [OK]
- Ignoring padding and feeding raw sequences
- Truncating sequences losing data
- Replacing RNN with Linear layer incorrectly
