Build a CNN model for text
PyBooks has successfully built a book recommendation engine. Their next task is to implement a sentiment analysis model to understand user reviews and gain insight into book preferences.
You'll use a Convolutional Neural Network (CNN) model to classify text data (book reviews) based on their sentiment.
torch
, torch.nn
as nn
, and torch.nn.functional
as F
have been loaded for you.
This exercise is part of the course
Deep Learning for Text with PyTorch
Exercise instructions
- Initialize the embedding layer in the
__init__()
method. - Apply the convolutional layer
self.conv
to theembedded
text within theforward()
method. - Apply the ReLU activation to this layer within the
forward()
method.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class TextClassificationCNN(nn.Module):
def __init__(self, vocab_size, embed_dim):
super(TextClassificationCNN, self).__init__()
# Initialize the embedding layer
self.embedding = ____.____(vocab_size, embed_dim)
self.conv = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, stride=1, padding=1)
self.fc = nn.Linear(embed_dim, 2)
def forward(self, text):
embedded = self.embedding(text).permute(0, 2, 1)
# Pass the embedded text through the convolutional layer and apply a ReLU
conved = ____.____(self.conv(____))
conved = conved.mean(dim=2)
return self.fc(conved)