Assignment 5

Deadline: March 7, 9pm

Late Penalty: See Syllabus

TA: Kingsley Chang

In this assignment, we will build a recurrent neural network to classify a SMS text message as "spam" or "not spam". In the process, you will

  1. Clean and process text data for machine learning.
  2. Understand and implement a character-level recurrent neural network.
  3. Use torchtext to build recurrent neural network models.
  4. Understand batching for a recurrent neural network, and use torchtext to implement RNN batching.

What to submit

Submit a PDF file containing all your code and outputs. Do not submit any other files produced by your code.

Completing this assignment using Jupyter Notebook is recommended (though not necessarily for all subsequent assignments). If you are using Jupyter Notebook, you can export a PDF file using the menu option File -> Download As -> PDF via LaTeX (pdf)

In [ ]:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np

Part 1. Data Cleaning [12 pt]

We will be using the "SMS Spam Collection Data Set" available at

Download and unzip the Data Folder, and move the file SMSSpamCollection to your working directory. (Same folder as this notebook)

Part (a) [1 pt]

Open up the file in Python. Print out one example of a spam SMS, and one example of a non-spam SMS.

In [ ]:
for line in open('SMSSpamCollection'):

Part (b) [1 pt]

How many spam messages and non-spam messages are there in the data set?

Part (c) [2 pt]

We will be using the package torchtext to load, process, and batch the data. A tutorial to torchtext is available below. This tutorial uses the same Sentiment140 data set that we explored during lecture.

One major difference is that we will be building a character level RNN. That is, we will treat each character as a token in our sequence, rather than each word.

Identify one advantage and one disadvantage of modelling SMS text messages as a sequence of characters rather than a sequence of words.

Part (d) [1 pt]

We will be loading our data set using The constructor will read directly from the SMSSpamCollection file.

For the data file to be read successfuly, we need to specify the fields (columns) in the file. In our case, the dataset has two fields:

  • a text field containing the sms messages,
  • a label field which will be converted into a binary label.

Split the dataset into train, valid, and test. Use a 60-20-20 split. You may find this torchtext API page helpful:

In [ ]:
import torchtext

text_field =,      # text sequence
                                  tokenize=lambda x: x, # because are building a character-RNN
                                  include_lengths=True, # to track the length of sequences, for batching
                                  use_vocab=True)       # to turn each character into an integer index
label_field =,    # not a sequence
                                   use_vocab=False,     # don't need to track vocabulary
                                   preprocessing=lambda x: int(x == 'spam')) # convert text to 0 and 1

fields = [('label', label_field), ('sms', text_field)]
dataset ="SMSSpamCollection", # name of the file
                                        "tsv",               # fields are separated by a tab
# dataset[0].sms
# dataset[0].label

# train, valid, test =  ...

Part (e) [2 pt]

You saw in part (b) that there are much more non-spam messages than spam messages. This imbalance in our training data will be problematic for training.

We can fix this disparity by duplicating non-spam messages in the training set, so that the training set is roughly balanced.

Explain why having a balanced training set is helpful for training our neural network.

Note: if you are not sure, try removing the below code and train your mode.

In [ ]:
# save the original training examples
old_train_examples = train.examples
# get all the spam messages in `train`
train_spam = []
for item in train.examples:
    if item.label == 1:
# duplicate each spam message 6 more times
train.examples = old_train_examples + train_spam * 6

Part (f) [1 pt]

We need to build the vocabulary on the training data by running the below code. This finds all the possible character tokens in the training set.

Explain what the variables text_field.vocab.stoi and text_field.vocab.itos represent.

In [ ]:

Part (g) [2 pt]

The tokens <unk> and <pad> were not in our SMS text messages. What do these two values represent?

Part (h) [2 pt]

Since text sequences are of variable length, torchtext provides a BucketIterator data loader, which batches similar length sequences together. The iterator also provides functionalities to pad sequences automatically.

Take a look at ~10 batches in train_iter. What is the maximum length of the input sequence in each batch? How many <pad> tokens are used in each of the ~10 batches?

In [ ]:
train_iter =,
                                           sort_key=lambda x: len(x.sms), # to minimize padding
                                           sort_within_batch=True,        # sort within each batch
                                           repeat=True)                   # repeat the iterator for multiple epochs
In [ ]:
for i, batch in enumerate(train_iter):
    if i >= 10:

Part 2. Model Building [10 pt]

Build a recurrent neural network model, using an architecture of your choosing. Use the one-hot embedding of each character as input to your recurrent network. Use one or more fully-connected layers to make the prediction based on your recurrent network output.

Instead of using the RNN output value for the final token, another often used strategy is to max-pool over the entire output array. That is, instead of calling something like:

out, _ = self.rnn(x)
self.fc(out[:, -1, :])

where self.rnn is an nn.RNN or nn.LSTM module, and self.fc is a linear layer, we use:

out, _ = self.rnn(x)
self.fc(torch.max(out, dim=1)[0])

This works reasonably in practice.

In [ ]:
# You mind find this code helpful for obtaining
# pytorch one-hot vectors.

ident = torch.eye(10)
print(ident[0]) # one-hot vector
print(ident[1]) # one-hot vector
x = torch.tensor([[1, 2], [3, 4]])
print(ident[x]) # one-hot vectors
In [ ]:
# This code is here to help you test your model.
# You may beed to change this depending on how your forward
# function is set up.

model = YourModelName()
sample_batch = next(iter(train_iter))
sms = sample_batch.sms[0]
length = sample_batch.sms[1]
y = model(sms)

Part 3. Training [15 pt]

Part (a) [8 pt]

Train your model. Plot the training curve of your final model. Your training curve should have the training/validation loss and accuracy plotted periodically. You can use the following code to compute your accuracy.

In [ ]:
def get_accuracy(model, data):
    data_iter =, 
                                              sort_key=lambda x: len(x.sms), 
    correct, total = 0, 0
    for i, batch in enumerate(data_iter):
        output = model(batch.sms[0]) # You may need to modify this, depending on your model setup
        pred = output.max(1, keepdim=True)[1]
        correct += pred.eq(batch.label.view_as(pred)).sum().item()
        total += batch.sms[1].shape[0]
    return correct / total

Part (b) [5 pt]

Choose at least 4 hyper parameters to tune. Explain how you tuned the hyper parameters. You don't need to include your traing curve for every model you trained. Instead, explain what hyper paremters you tuned, what the validation accuracy was, and the reasoning behind the hyper parameter decisions you made.

For this assignment, you should tune more than just your learning rate and epoch. Choose at least 2 hyper parameters that are unrelated to the optimizer.

Part (c) [2 pt]

Report the final test accuracy of your model. You should be able to obtain fairly good accuracy for this model.

Part 4. Baseline Model [3 pt]

Do you think detecting spam is an easy or difficult task? One way to answer this question is to think of a baseline model: a simple model that is easy to build and inexpensive to run, that we can compare our recurrent neural network model against.

Explain how you might build a simple baseline model. This baseline model can be a simple neural network (with very few weights), a hand-written algorithm, or any other strategy that is easy to build and test.

Since machine learning models are expensive to train and deploy, it is very important to compare our models against baseline models.