1. Learn
  2. /
  3. Courses
  4. /
  5. Deep Learning for Text with PyTorch

Connected

Exercise

Building an RNN model for text

As a data analyst at PyBooks, you often encounter datasets that contain sequential information, such as customer interactions, time series data, or text documents. RNNs can effectively analyze and extract insights from such data. In this exercise, you will dive into the Newsgroup dataset that has already been processed and encoded for you. This dataset comprises articles from different categories. Your task is to apply an RNN to classify these articles into three categories:

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

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

Additionally, the parameters input_size, hidden_size (32), num_layers (2), and num_classes have been preloaded for you.

This and the following exercises use the fetch_20newsgroups dataset from sklearn.

Instructions

100 XP
  • Complete the RNN class with an RNN layer and a fully connected linear layer.
  • Initialize the model.
  • Train the RNN model for ten epochs by zeroing the gradients.