Get Started

Building an LSTM model for text

At PyBooks, the team is constantly seeking to enhance the user experience by leveraging the latest advancements in technology. In line with this vision, they have assigned you a critical task. The team wants you to explore the potential of another powerful tool: LSTM, known for capturing more complexities in data patterns. You are working with the same Newsgroup dataset, with the objective remaining unchanged: to classify news articles into three distinct categories:

rec.autos, sci.med, and comp.graphics.

The following packages have been loaded for you: torch, nn, optim.

This is a part of the course

“Deep Learning for Text with PyTorch”

View Course

Exercise instructions

  • Set up an LSTM model by completing the LSTM and linear layers with the necessary parameters.
  • Initialize the model with the necessary parameters.
  • Train the LSTM model resetting the gradients to zero and passing the input data X_train_seq through the model.
  • Calculate the loss based on the predicted outputs and the true labels.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize the LSTM and the output layer with parameters
class LSTMModel(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
        super(LSTMModel, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm = nn.LSTM(____, ____, ____, batch_first=True)
        self.fc = nn.Linear(____, ____)        
    def forward(self, x):
        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size)
        c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size)
        out, _ = self.lstm(x, (h0, c0))
        out = out[:, -1, :] 
        out = self.fc(out)
        return out

# Initialize model with required parameters
lstm_model = LSTMModel(____, ____, ____, ____)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(lstm_model.parameters(), lr=0.01)

# Train the model by passing the correct parameters and zeroing the gradient
for epoch in range(10): 
    optimizer.____
    outputs = lstm_model(____)
    loss = criterion(____, y_train_seq)
    loss.backward()
    optimizer.step()
    print(f'Epoch: {epoch+1}, Loss: {loss.item()}')
Edit and Run Code

This exercise is part of the course

Deep Learning for Text with PyTorch

AdvancedSkill Level
3.8+
8 reviews

Discover the exciting world of Deep Learning for Text with PyTorch and unlock new possibilities in natural language processing and text generation.

Explore text classification and its role in Natural Language Processing (NLP). Apply your skills to implement word embeddings and develop both Convolutional Neural Networks (CNNs) and Recurrent Neural Networks (RNNs) for text classification using PyTorch, and understand how to evaluate your models using suitable metrics.

Exercise 1: Overview of Text ClassificationExercise 2: Embedding in PyTorchExercise 3: Categorizing text classification tasksExercise 4: Convolutional neural networks for text classificationExercise 5: Build a CNN model for textExercise 6: Train a CNN model for textExercise 7: Testing the Sentiment Analysis CNN ModelExercise 8: Recurrent neural networks for text classificationExercise 9: Building an RNN model for textExercise 10: Building an LSTM model for text
Exercise 11: Building a GRU model for textExercise 12: Evaluation metrics for text classificationExercise 13: Evaluating RNN classification modelsExercise 14: Evaluating the model's performanceExercise 15: Comparing models

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free