Chapter 9: RNN and CNN

80. Turning words into numeric IDs

We want to turn the words from the data we created in problem 51 into numeric IDs. Assign to each word that occurs twice or more in data a numeric ID, so that the most frequent word is assigned the ID 1, the second-most frequent word the ID 2, and so on. Then, implement a function that returns the ID for a given word, or, if the word occurs less than two times in the data, returns 0.

81. Prediction with an RNN

We have a sequence of numeric word IDs , corresponding to the words in a given sentence. Here, is the number of words in the sequence (i.e., the sentence) and is a one-hot vector ( is the number of words in the vocabulary). Use an RNN (Recurrent Neural Network) to implement a model that predicts a category from a given sequence of word IDs .

where is a word embedding (a function that maps a one-hot vector to a dense word vector). Here, is the hidden state at time step . In addition, is an RNN cell that (1) takes as input the word at timestep and the hidden state of the previous time step, and (2) produces the hidden state for the next time step. is a linear transformation for predicting the category from the hidden state, and is a bias term (, and are the dimensionalities of the word embeddings, hidden state, and the number of categories respectively). While there are several different RNN cells , a simple example is as follows:

where are the parameters of the RNN cell, and is an activation function (e.g. and ReLU).

For this problem, you don’t have to train the model. You only need to randomly initialize the network and compute . For the dimentionalities, use the settings like (same applies for the later questions).

82. Training with Stochastic Gradient Descent

Use Stochastic Gradient Descent (SGD) to train the model we built in Problem 81. During training, print the training loss and training accuracy, and loss and accuracy on validation data. Stop training according to the appropriate criterion (e.g., after 10 epochs).

83. Mini-batch Training, GPU Training

Modify the code from Problem 82 so that it computes loss and gradient for a batch of instances. You may decide mini-batch size (e.g., ). After this, train the model on a GPU.

84. Add Pretrained Word Embeddings

Use pretrained word embeddings (e.g., the Google News embeddings, which have been trained on approx. 100 billion tokens) to initialize and train the model.

85. Bi-directional RNN and Multi-layer RNN

Encode the input text using both forward and backward RNNs and train the model.

However, is the hidden state vector at time obtained by the forward and backward RNNs, and is the RNN unit that calculates the previous state from the input and the hidden state at the next time, is a matrix for predicting categories from the hidden state vector, and is the bias term. Moreover, represents a concatenation of two vectors and .

In addition, experiment with multi-layered bidirectional RNNs.

86. Convolutional Neural Networks (CNN)

Let us consider the sequence of words . Here, is the length of the word. In addition, is one-hot vector that represents the ID that corresponds to the word ( is the total number of words).

Implement a model that predicts category from the given sequence using a Convolutional Neural Network (CNN).

The configuration of the CNN is as follows:

  • Word embedding dimension:
  • Convolution filter size: 3 tokens
  • Convolution stride: 1 token
  • Convolution padding: Yes
  • The size of the vector of each time step after convolution operation:
  • Apply max pooling after convolution operation and express input sentences as -dimensional hidden vector.

That is, the feature vector at time is expressed via the following equation:

Here, , is a CNN parameter, is the activation function (e.g., , ReLU, etc.), is the concatenation of vectors . The reason why the number of columns in matrix is is to perform a linear transformation on the concatenation of word embeddings for the 3 tokens.

In max pooling, the maximum value at all times is taken for each dimension of the feature vector, and the feature vector of the input document is obtained. Assuming that represents the -th dimension of vector , max pooling is given by the following equation:

Lastly, predict category by using both linear transformation and softmax function with matrix and bias applied to the input document’s feature vector:

Moreover, it is fine to calculate with a randomly initialized weight matrix without learning the model.

87. CNN Learning via Stochastic Gradient Descent

Using Stochastic Gradient Descent (SGD), learn the model you constructed in Problem 86.Learn the model while displaying the loss and correct rate on the training data and the loss and correct rate on the evaluation data. Finish with a suitable standard (e.g., 10 epochs, etc.).

88. Hyper-parameter Tuning

Altering Problem 85 or 87’s code, build a high-quality category classifier by adjusting the neural network’s shape and hyper-parameters.

89. Transfer Learning from a Pretrained Language Model

Starting from a Pretrained Language Model (e.g. BERT, etc.) model, build a model that categorizes news article headlines into categories.