Homework 10
Recall that in Notebook 0409, we trained a Categorical Deep Set model on the binary classification task of the subset of the dair-ai/emotion dataset on tweets labeled as either sadness or joy. We even trained a model that used 1-dimensional word vectors succesfully, which we then were able to interpret: we could see that the values of the 1-dimensional word vectors correlate with how joyful (positive) or sad (negative) they are.
In this homework, we shall train a Categorical Deep Set on the full dataset with 6 labels sadness, joy, love, anger, fear, and surprise. We will be able to adapt our interpretation methods to this setting.
- Load and split the dataset. You can do this similar to Notebook 0409, just don't filter it based on the labels.
- You can find the list of labels, in the same order as the IDs used in the
"label"keys of the dataset entries, as follows:- Take the
featuresattribute of the dataset. This is a dictionary. - Take the value of the above dictionary at the
"label"key. - Take the
namesattribute of the above object.
- Take the
- Using
enumerate, print the label ID-label pairs used in the full dataset.
- You can find the list of labels, in the same order as the IDs used in the
- Preprocess the dataset. You can do this similar to Notebook 0409, just use the 64-bit integer datatype to store labels instead of the 32-bit floats.
- To save time when you are experimenting, I suggest saving the preprocessed dataset splits.
- You should also get the word ID to word assignments either in the form of a list or a dictionary. Note that this is also done in Notebook 0409.
- Similar to Notebook 0409, train a categorical deep set model on the dataset. It should use 10-dimensional word vectors and no hidden dimensions. Note that in this case, we have 6 classes. Therefore, you need to use non-binary accuracy and cross-entropy for metric and loss functions, respectively. These are implemented in Notebook 0321.
- Note that we used population-based training, thus we have multiple models at our disposal. Let's interpret the best one.
- At the
"validation metric"key of the log dictionary output by the training function, you can find evaluation metrics of the population members along training. It should have shape(evaluations, population_num). In particular, its last row contains validation metrics for the model in its present state. - Based on the validation metrics, get the ID of the current best population member.
- Calling the
state_dictmethod of the model, you can get a dictionary with values the following parameter tensors:- Word vectors, of shape
(population_num, vocabulary_size, embedding_dim), - Linear classifier bias, of shape
(population_num, label_num), and - Linear classifier weight, of shape
(population_num, embedding_dim, label_num).
- Word vectors, of shape
- Taking the subtensors of the above at the best model ID along the leftmost dimension, we get the following:
- A word vector matrix \(E\),
- A classifier bias vector \(\mathbf b\), and
- A classifier weight matrix \(W\).
- At the
-
Via the tensors you got, given a document \(D=(i_1,\dotsc,i_k)\) where the \(i_j\) are word indices, the class logits are gotten as
\[ \mathbf b + \frac{1}{k}\left(\sum_{j=1}^kE_{i_j}\right)W =\mathbf b+\sum_{j=1}^k\frac{1}{k}E_{i_j}W. \]-
Note that on the right, we see that the class logits are a sum of the following:
- The bias vector \(\mathbf b\), and
- The logit contributions of individual words \(\frac{1}{k}E_{i_j}W\).
-
This opens up an interpretation opportunity!
- The product \(EW\) of shape
(vocabulary_size, label_num)gives logit contributions for each word - label pair. -
Loop over the label indices \(j=0,\dotsc,\mathtt{label\_num}-1\), and for each \(j\):
- Print the corresponding label name.
- Get the indices of the ten largest values of the \(j\)-th column of \(EW\).
- Print the words corresponding to these 10 indices.
- The product \(EW\) of shape
-