CS 1678: Homework 5

Due: 12/3/2021, 11:59pm

This assignment is worth 30 points.

As for HW2, you will write your code and any requested responses or descriptions of your results, in a Colab Jupyter notebook.

We strongly recommend you to spend some time (1-2 hours) to read the recommended implementation and our starter code before you start working. Excluding the time for training the models (please leave a few days for training), we expect this assignment to take 4-6 hours.

The data you need can be found on Canvas (see Homework 5 under Assignments).


Part A: Sentiment analysis on IMDB reviews (15 points)

Large Moview Review Dataset (IMDB) is a dataset for binary sentiment classification containing substantially more data than previous benchmark datasets. It provides a set of 50,000 highly polar movie reviews. We have split the dataset into training (45,000) and test (5,000). The positive:negative ratio is 1:1 in both splits.
In this task, you need to develop a RNN model to "read" each review then predict whether it's positive or negative.
In the provided starter code, we implemented a RNN pipeline (sentiment_analysis.py) and a GRUCell (rnn_modules.py) as an example.
You need to build one other variant of a RNN module, specifically as explained in this blog.

Please see this link for the starter code which you can use in your own Colab notebook. You are not allowed to use the native torch.nn.LSTMCell or other built-in RNN modules in this assignment.

Instructions:
  1. Read the code in rnn_datasets.py and sentiment_analysis.py, then run the training with provided GRU cell. You should be able to achieve at least 85% accuracy in 50 epochs with default hyperparameters.
    Attach the figures (either TensorBoard screenshot or plot on your own) of (1) training loss, (2) training accuracy per epoch and (3) validation accuracy per epoch in your report.
  2. Implement the following variant in rnn_modules.py, details in the blog, the section named Variants on Long Short Term Memory. Please note that the class is already provided for you, and you only need to complete the __init__ and forward functions. Please do NOT change the signatures.
  3. Print number of model parameters for different module type (GRUCell and LSTMCell) using count_parameters, and include the comparison in your notebook.
    Use the following hyperparameters for comparison: input_size=128, hidden_size=100, bias=True.
  4. Run experiments with your custom implementation on sentiment analysis with IMDB dataset, and compare the results with the GRU, including both speed and performance.
    Attach the training loss and training/validation accuracy plot (or include it in your notebook).

Part B: Building a Shakespeare writer (15 points)

RNN has demonstrated great potential in modeling language, and one interesting property is that it can "learn" to generate new sentences.
In this task, you need to develop a character-based RNN model (meaning instead of words, RNN processed one character at a time) to learn how to write like Shakespeare.

Instructions:
  1. Read the code in rnn_datasets.py and sentence_generation.py, and complete the SentenceGeneration class which is a character-level RNN.
    You can reuse (by copy/paste) most of the codes from SentimentClassification in Part A, just note that instead of predicting positive or negative, now your task is to predict the next character given a sequence of chars (history).
  2. Train the model with the GRU module on Shakespeare books. You should be able to achieve loss value of 1.2 in 10 epochs with default hyperparameters. You probably need to use an embedding_dim of 256 and hidden_size of 512 to get the above loss value. If you are interested you could try with your own LSTM variant, but experiments with GRU is required.
  3. Complete the function in sentence_generation.py to load your trained model and generate new sentence from it.
    Basically, once a language model is trained, it is able to predict the next character after a sequence, and this process can be continued (predicted character serve as history for predicting the next).
    More specifically, your model should be able to predict the probability distribution over the vocabulary for the next character, and we have implemented a sampler sample_next_char_id which samples according to the probability. By repeating this process, your model is able to write arbitrarily long paragraphs.
    For example the following passage is written by a GRU trained on Shakespeare:
    ROMEO:Will't Marcius Coriolanus and envy of smelling!
    
    DUKE VINCENTIO:
    He seems muster in the shepherd's bloody winds;
    Which any hand and my folder sea fast,
    Last vantage doth be willing forth to have.
    Sirraher comest that opposite too?
    
    JULIET:
    Are there incensed to my feet relation!
    Down with mad appelate bargage! troubled
    My brains loved and swifter than edwards:
    Or, hency, thy fair bridging courseconce,
    Or else had slept a traitors in mine own.
    Look, Which canst thou have no thought appear.
    
    ROMEO:
    Give me them: for that I put us empty.
    
    RIVERS:
    The shadow doth not live: and he would not
    From thee for his that office past confusion
    Is their great expecteth on the wheek;
    But not the noble fathom were an poison
    Here come to make a dukedom: therefore--
    But O, God grant! for Signior HERY
    
    VI:
    Soft love, that Lord Angelo: then blaze me all;
    And slept not without a Calivan Us.
    
    Note that the model learns how to spell each word and write the sentence-like paragraphs all by itself, even including the punctuations and line breaks.
    Please use ROMEO and JULIET as history to begin the generation for 1000 characters each, and attach the generated text (or include it in your notebook).

Submission: Please submit your Jupyter notebook and results files, as noted above (or include them in your notebook).

Acknowledgement: This assignment was designed by Mingda Zhang in Spring 2020 for CS1699: Intro to Deep Learning.