0
0
PyTorchml~20 mins

Sequence classification in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequence Classification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple sequence classification model
What is the output shape of the logits tensor after running this PyTorch sequence classification model on a batch of 8 sequences, each of length 10, with an embedding size of 16 and 3 output classes?
PyTorch
import torch
import torch.nn as nn

class SimpleSeqClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.embedding = nn.Embedding(20, 16)
        self.rnn = nn.GRU(16, 32, batch_first=True)
        self.fc = nn.Linear(32, 3)

    def forward(self, x):
        x = self.embedding(x)
        _, h_n = self.rnn(x)
        logits = self.fc(h_n.squeeze(0))
        return logits

model = SimpleSeqClassifier()
inputs = torch.randint(0, 20, (8, 10))
logits = model(inputs)
output_shape = logits.shape
A(32, 3)
B(8, 3)
C(8, 10, 3)
D(10, 3)
Attempts:
2 left
💡 Hint
The model outputs one prediction per sequence in the batch, not per time step.
Model Choice
intermediate
1:30remaining
Choosing the right model for sequence classification
Which PyTorch model architecture is best suited for classifying sequences where the order of elements matters and the sequences can have variable lengths?
AA recurrent neural network (RNN) such as LSTM or GRU
BA convolutional neural network (CNN) with 1D convolutions over the sequence
CA feedforward neural network with fixed-size input vectors
DA simple linear regression model
Attempts:
2 left
💡 Hint
Think about models that can handle sequences of different lengths and remember order.
Hyperparameter
advanced
1:30remaining
Effect of hidden size on sequence classification model
In a GRU-based sequence classification model, increasing the hidden size from 32 to 128 will most likely have which effect?
ADecrease model capacity and reduce training time
BHave no effect on model capacity or training time
CCause the model to ignore sequence order
DIncrease model capacity and may improve accuracy but increase training time
Attempts:
2 left
💡 Hint
Hidden size controls how much information the model can store at each step.
Metrics
advanced
1:30remaining
Choosing the right metric for imbalanced sequence classification
For a sequence classification task with highly imbalanced classes, which metric is most appropriate to evaluate model performance?
AF1-score
BAccuracy
CPrecision
DMean Squared Error
Attempts:
2 left
💡 Hint
Consider a metric that balances false positives and false negatives.
🔧 Debug
expert
2:00remaining
Debugging a sequence classification model with exploding gradients
You train a GRU-based sequence classification model, but the training loss suddenly becomes NaN after a few epochs. Which of the following is the most likely cause?
AThe batch size is too small causing underfitting
BThe model has too few layers causing underfitting
CThe learning rate is too high causing exploding gradients
DThe input sequences are too short causing overfitting
Attempts:
2 left
💡 Hint
NaN loss often happens when gradients become very large.