Starting from:

$34.99

CSC413-2516 Assignment 1- Learning Distributed Word Representations Solution

Version: 1.2
Changes by Version:
(v1.2)
• (1.3, 1.6) Change notation for gradient from
• (1.3, 1.5) Added additional hints
(v1.1)
• (1.2) Clarified operations that can be used
• (1.5) Reference the newly added GloVe gradient checker function
• (1.6) Clarified additional assumptions
• (“What you have to submit”) Fixed colour reference to zero point questions
Based on an assignment by George Dahl
Submission: You must submit two files through MarkUs : (1) a PDF file containing your writeup, titled a1-writeup.pdf, which will be an export of the iPython notebook, and (2) your code file a1-code.ipynb. There will be sections in the notebook for you to write your responses. Your writeup must be typed. Make sure that the relevant outputs (e.g. print gradients() outputs, plots, etc.) are included and clearly visible.
The programming assignments are individual work. See the Course Information handout for detailed policies.
Introduction
In this assignment we will learn about word embeddings and make neural networks learn about words. We could try to match statistics about the words, or we could train a network that takes a sequence of words as input and learns to predict the word that comes next.
This assignment will ask you to implement a linear embedding and then the backpropagation computations for a neural language model and then run some experiments to analyze the learned representation. The amount of code you have to write is very short but each line will require you to think very carefully. You will need to derive the updates mathematically, and then implement them using matrix and vector operations in NumPy.
Starter code and data
The starter helper function will download the specific the dataset from http://www.cs.toronto. edu/~jba/a1_data.tar.gz. Look at the file raw_sentences.txt. It contains the sentences that we will be using for this assignment. These sentences are fairly simple ones and cover a vocabulary of only 250 words (+ 1 special [MASK] token word).
We have already extracted the 4-grams from this dataset and divided them into training, validation, and test sets. To inspect this data, run the following within IPython:
import pickle
data = pickle.load(open(’data.pk’, ’rb’))
Now data is a Python dict which contains the vocabulary, as well as the inputs and targets for all three splits of the data. data[’vocab’] is a list of the 251 words in the dictionary; data[’vocab’][0] is the word with index 0, and so on. data[’train_inputs’] is a 372,500 × 4 matrix where each row gives the indices of the 4 consecutive context words for one of the 372,500 training cases. The validation and test sets are handled analogously.
1 Linear Embedding – GloVe (3pts)
In this section we will be implementing a simplified version of GloVe [Jeffrey Pennington and Manning]. Given a corpus with V distinct words, we define the co-occurrence matrix X ∈ NV ×V with entries Xij representing the frequency of the i-th word and j-th word in the corpus appearing in the same context - in our case the adjacent words. The co-occurrence matrix can be symmetric (i.e., Xij = Xji) if the order of the words do not matter, or asymmetric (i.e., Xij ̸= Xji) if we wish to distinguish the counts for when i-th word appears before j-th word. GloVe aims to find a d-dimensional embedding of the words that preserves properties of the co-occurrence matrix by representing the i-th word with two d-dimensional vectors wi,w˜ i ∈ Rd, as well as two scalar biases bi,˜bi ∈ R. Typically we have the dimension of the embedding d much smaller than the number of words V . This objective can be written as :
V
L({wi,w˜ i,bi,˜bi}Vi=1) = X(wi⊤w˜ j + bi +˜bj − logXij)2 (1)
i,j=1
When the bias terms are omitted and we tie the two embedding vectors wi = w˜ i, then GloVe corresponds to finding a rank-d symmetric factorization of the co-occurrence matrix.
1.1 GloVe Parameter Count [0pt]
Given the vocabulary size V and embedding dimensionality d, how many trainable parameters does the GloVe model have? Note that each word in the vocabulary is associated with 2 embedding vectors and 2 biases.
1.2 Expression for the vectorized loss function [0.5pt]
In practice, we concatenate the V embedding vectors into matrices W,W˜ ∈ RV ×d and bias (column) vectors b,b˜ ∈ RV , where V denotes the number of distinct words as described in the introduction. Rewrite the loss function L (Eq. 1) in a vectorized format in terms of W,W˜ ,b,b˜,X. You are allowed to use elementwise operations such as addition and subtraction as well as matrix operations such as the Frobenius norm and/or trace operator in your answer.
Hint: Use the all-ones column vector 1 = [1...1]T ∈ RV . You can assume the bias vectors are column vectors, i.e. implicitly a matrix with V rows and 1 column: b,b˜ ∈ RV ×1
1.3 Expression for the vectorized gradient ∇WL [0.5pt]
Write the vectorized expression for ∇WL, the gradient of the loss function L with respect to the embedding matrix W. The gradient should be a function of W,W˜ ,b,b˜,X.
Hint: Make sure that the shape of the gradient is equivalent to the shape of the matrix. You can use the all-ones vector as in the previous question.
1.4 Implement Vectorized Loss Function [1pt]
Implement the loss_GloVe() function of GloVe in a1-code.ipynb. Look for the ## YOUR CODE HERE ## comment for where to complete the code. Note that you need to implement both the loss for an asymmetric model (from your answer in question 1.2) and the loss for a symmetric model which uses the same embedding matrix W and bias vector b for the first and second word in the co-occurrence, i.e. W˜ = W and b˜ = b in the original loss.
1.5 Implement the gradient update of GloVe [1pt]
Implement the grad_GloVe() function which computes the gradient of GloVe in a1-code.ipynb. Look for the ## YOUR CODE HERE ## comment for where to complete the code. Again, note that you need to implement the gradient for both the symmetric and asymmetric models.
Update (v1.1): We added a gradient checker function using finite difference called check_GloVe_gradients(). You can run the specified cell in the notebook to check your gradient implementation for both the symmetric and asymmetric models before moving forward.
Once you have implemented the gradient, run the following cell marked by the comment
### TODO: Run this cell ### in order to train an asymmetric and symmetric GloVe model. The code will plot a figure containing the training and validation loss for the two models over the course of training. Include this plot in your write up.
Update (v1.2): Hint: In the symmetric model case, you can use what you have derived for the asymmetric model case. For example, consider a function f(a,b) = a⊤b, where a,b ∈ Rd. If we define a = x and b = x, where b ∈ Rd, then
∇xf = ∇af + ∇bf (2)
= b + a (3)
= x + x (4)
= 2x (5)
1.6 Effects of a buggy implementation [0pt]
Suppose that during the implementation, you initialized the weight embedding matrix W and W˜ with the same initial values (i.e., W = W˜ = W0). The bias vectors were also initialized the same, i.e., b = b˜ = b0. Assume also that in this case, the co-occurrence matrix is also symmetric:
Xij = Xji
What will happen to the values of W and W˜ over the course of training? Will they stay equal to each other, or diverge from each other? Explain your answer briefly. Hint: Consider the gradient ∇WL versus ∇W˜ L
1.7 Effects of embedding dimension [0pt]
Train the both the symmetric and asymmetric GloVe model with varying dimensionality d. Comment on the results:
1. Which d leads to optimal validation performance for the asymmetric and symmetric models?
2. Why does / doesn’t larger d always lead to better validation error?
3. Which model is performing better (asymmetric or symmetric), and why?
2 Neural Language Model Network architecture (1pt)

Figure 1: A simplified architecture with N words input and N words output. During training, we mask out one of the input words by replacing it with a [MASK] token, and try to predict the masked out word in the corresponding position in the output. Only that output position is used in the cross entropy loss.
2.1 Number of parameters in neural network model [0.5pt]
The trainable parameters of the model consist of 3 weight matrices and 2 sets of biases. What is the total number of trainable parameters in the model, as a function of V,N,D,H? In the diagram given above, which part of the model (i.e., word_embbeding_weights, embed_to_hid_weights, hid_to_output_weights, hid_bias, or output_bias) has the largest number of trainable parameters if we have the constraint that V ≫ H > D > N? Explain your reasoning.
2.2 Number of parameters in n-gram model [0.5pt]
2.3 Comparing neural network and n-gram model scaling [0pt]
How do the parameters in the neural network model scale with the number of context words N versus how the number of entries in the n-gram model scale with N? Which model has a more compact representation for the words?
3 Training the Neural Network (2pts)
B N V
C = −XXXm(ni)(t(vi+)nV logyv(i+)nV ) (6)
i n v
Where:
• denotes the output probability prediction from the neural network for the i-th training example for the word v in the n-th output word. Denoting z as the output logits, we define the output probability y as a softmax on z over contiguous chunks of V units (see also Figure
1):
(7)
• is 1 if for the i-th training example, the word v is the n-th word in context
• is a mask that is set to 1 if we are predicting the n-th word position for the i-th example (because we had masked that word in the input), and 0 otherwise
• compute_activations computes the activations of all units on a given input batch
• compute_loss_derivative computes the gradient with respect to the output logits
• evaluate computes the average cross-entropy loss for a given set of inputs and targets
You will need to complete the implementation of two additional methods to complete the training, and print the outputs of the gradients.
3.1 Implement Vectorized Loss [0.5pt]
Implement a vectorized compute_loss function, which computes the total cross-entropy loss on a mini-batch according to Eq. 6. Look for the ## YOUR CODE HERE ## comment for where to complete the code. The docstring provides a description of the inputs to the function.
3.2 Implement gradient with respect to parameters [1pt]
back_propagate is the function which computes the gradient of the loss with respect to model parameters using backpropagation. It uses the derivatives computed by compute_loss_derivative. Some parts are already filled in for you, but you need to compute the matrices of derivatives for embed_to_hid_weights and output_bias. These matrices have the same sizes as the parameter matrices. Look for the ## YOUR CODE HERE ## comment for where to complete the code.
In order to implement backpropagation efficiently, you need to express the computations in terms of matrix operations, rather than for loops. You should first work through the derivatives on pencil and paper. First, apply the chain rule to compute the derivatives with respect to individual units, weights, and biases. Next, take the formulas you’ve derived, and express them in matrix form. You should be able to express all of the required computations using only matrix multiplication, matrix transpose, and element-wise operations — no for loops! If you want inspiration, read through the code for Model.compute_activations and try to understand how the matrix operations correspond to the computations performed by all the units in the network.
Hints: Your implementations should also be similar to hid_to_output_weights_grad, hid_bias_grad in the same function call.
3.3 Print the gradients [0.5pt]
To make your life easier, we have provided the routine check_gradients, which checks your gradients using finite differences. You should make sure this check passes (prints OK for the various parameters) before continuing with the assignment. Once check_gradients passes, call print_gradients and include its output in your write-up.
3.4 Run model training [0 pt]
Once you’ve implemented the gradient computation, you’ll need to train the model. The function train in a1-code.ipynb implements the main training procedure. It takes two arguments:
• embedding_dim: The number of dimensions in the distributed representation.
• num_hid: The number of hidden units
For example, execute the following:
model = train(16, 128)
As the model trains, the script prints out some numbers that tell you how well the training is going. It shows:
• The cross entropy on the last 100 mini-batches of the training set. This is shown after every 100 mini-batches.
• The cross entropy on the entire validation set every 1000 mini-batches of training.
At the end of training, this function shows the cross entropies on the training, validation and test sets. It will return a Model instance.
4 Bias in Word Embeddings (2pts)
Unfortunately, stereotypes and prejudices are often reflected in the outputs of natural language processing algorithms. For example, Google Translate is more likely to translate a non-English sentence to “He is a doctor” than “She is a doctor” when the sentence is ambiguous. In this section, you will explore how bias enters natural language processing algorithms by implementing and analyzing a popular method for measuring bias in word embeddings.
4.1 WEAT method for detecting bias [1pt]
Embedding Association Test (WEAT). The WEAT test measures whether two target word sets (e.g. {programmer, engineer, scientist, ...} and {nurse, teacher, librarian, ...}) have the same relative association to two attribute word sets (e.g. {man, male, ...} and {woman, female ...}).
Formally, let A, B be two sets of attribute words. Then
s(w,A,B) = meana∈A cos(w⃗,⃗a) − meanb∈B cos(w⃗,⃗b) (8)
measures the association of a target word w with the attribute sets - for convenience, we will call this the WEAT association score. A positive score means that the word w is more associated with A, while a negative score means the opposite. For example, a WEAT association score of 1 in the following test s(“programmer”,{man},{woman}) = 1, implies the “programmer” has a stronger association to {man}. For reference, the cosine similarity between two word vectors ⃗a and ⃗b is given by:
(9)
In the notebook, we have provided example target words (in sets X and Y ) and attribute words (in sets A and B). You must implement the function weat_association_score() and compute the WEAT association score for each target word.
4.2 Reasons for bias in word embeddings [0pt]
Based on the results of the WEAT test, do the pretrained word embeddings associate certain occuptations with one gender more than another? What might cause word embedding models to learn certain stereotypes and prejudices? How might this be a problem in downstream applications?
4.3 Analyzing WEAT
4.3.1 1-word subsets [0.5pts]
In the notebook, you are asked to find 1-word subsets of the original A and B that reverse the association between some of the occupations and the gendered attributes (change the sign of the WEAT score).
4.3.2 How word frequency affects embedding similarity [0.5pts]
s(wi,{wj},{wk}) = cos(wi,wj) − cos(wi,wk)
= wi⊤wj − wi⊤wk
∥wi∥∥wj∥ ∥wi∥∥wk∥
Remember the GloVe embedding training objective from Part 1 in this assignment. Assume tied weights and ignore the bias units, we can write the training loss as following:
V
Simplified GloVe L({wi}Vi=1) = X(wi⊤wj − logXij)2,
i,j=1
where Xij denotes the number of times word i and word j co-occured together in the training corpus. In the special case, Xii denotes the number of times word i appeared in the corpus. When this model reaches zero training loss, the inner product of the GloVe embedding vectors will simply equal to the entries in the log co-occurrence matrix logX. We can then express the WEAT association score in terms of the original co-occurrence matrix:
!
Briefly explain how this fact might contribute to the results from the previous section when using different attribute words. Provide your answers in no more than three sentences. Hint: The paper cited above is a great resource if you are stuck.
4.3.3 Relative association between two sets of target words [0 pts]
In the original WEAT paper, the authors do not examine the association of individual words with attributes, but rather compare the relative association of two sets of target words. For example, are insect words more associated with positive attributes or negative attributes than flower words.
Formally, let X and Y be two sets of target words of equal size. The WEAT test statistic is given by:
s(X,Y,A,B) = X s(x,A,B) − X s(y,A,B) (10)
x∈X y∈Y
Will the same technique from the previous section work to manipulate this test statistic as well? Provide your answer in no more than 3 sentences.
What you have to submit
For reference, here is everything you need to hand in. The zero point questions (in black below) will not be graded, but you are more than welcome to include your answers for these as well in the submission. See the top of this handout for submission directions.
• A PDF file titled a1-writeup.pdf containing the following:
– Part 1: Questions 1.1,1.2, 1.3, 1.6, 1.7. Completed code for loss_GloVe() (1.4) and grad_GloVe() function and output plot in 1.5.
– Part 2: Questions 2.1, 2.2, 2.3.
– Part 3: Completed code for compute_loss() (3.1), back_propagate() (3.2) functions, and the output of print_gradients() (3.3)
– Part 4: Questions 4.2, 4.3.2, 4.3.3. Completed code for weat_association_score(), and outputs (4.1), 1-word subsets with outputs (4.3.1).
• Your code file a1-code.ipynb
References
Richard Socher Jeffrey Pennington and Christopher D Manning. Glove: Global vectors for word representation. Citeseer.
Yoshua Bengio, R´ejean Ducharme, Pascal Vincent, and Christian Jauvin. A neural probabilistic language model. Journal of machine learning research, 3(Feb):1137–1155, 2003.

More products