Practical Exercises Chapter 6
Exercise 1: Implement a Simple RNN for Sequence Classification
Task: Implement a simple RNN to classify sequences of numbers. Use synthetic data where each sequence is classified as positive if the sum of the elements is above a threshold, and negative otherwise.
Solution:
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
# Generate synthetic data (binary classification based on sequence sum)
def generate_data(num_samples=1000, sequence_length=10, threshold=5):
X = torch.randint(0, 3, (num_samples, sequence_length)).float()
y = (X.sum(dim=1) > threshold).float()
class SimpleRNN(nn.Module):
def __init__(self, input_size ...