Chapter 175
Word Embedding with Global Vectors (GloVe)
Word Embedding with Global Vectors (GloVe)
Word-word co-occurrences within context windows may carry rich semantic information. For example, in a large corpus word "solid" is more likely to co-occur with "ice" than "steam", but word "gas" probably co-occurs with "steam" more frequently than "ice". Besides, global corpus statistics of such co-occurrences can be precomputed: this can lead to more efficient training. To leverage statistical information in the entire corpus for word embedding, let us first revisit the skip-gram model in subsec_skip-gram, but interpreting it using global corpus statistics such as co-occurrence counts.
Skip-Gram with Global Corpus Statistics
Denoting by the conditional probability of word given word in the skip-gram model, we have
where for any index vectors and represent word as the center word and context word, respectively, and is the index set of the vocabulary.
Consider word that may occur multiple times in the corpus. In the entire corpus, all the context words wherever is taken as their center word form a multiset of word indices that allows for multiple instances of the same element. For any element, its number of instances is called its multiplicity. To illustrate with an example, suppose that word occurs twice in the corpus and indices of the context words that take as their center word in the two context windows are and . Thus, multiset , where multiplicities of elements are 2, 4, 1, 1, respectively.
Now let us denote the multiplicity of element in multiset as . This is the global co-occurrence count of word (as the context word) and word (as the center word) in the same context window in the entire corpus. Using such global corpus statistics, the loss function of the skip-gram model is equivalent to
We further denote by the number of all the context words in the context windows where occurs as their center word, which is equivalent to . Letting be the conditional probability for generating context word given center word , eq_skipgram-x_ij can be rewritten as
In eq_skipgram-p_ij, calculates the cross-entropy of the conditional distribution of global corpus statistics and the conditional distribution of model predictions. This loss is also weighted by as explained above. Minimizing the loss function in eq_skipgram-p_ij will allow the predicted conditional distribution to get close to the conditional distribution from the global corpus statistics.
Though being commonly used for measuring the distance between probability distributions, the cross-entropy loss function may not be a good choice here. On one hand, as we mentioned in sec_approx_train, the cost of properly normalizing results in the sum over the entire vocabulary, which can be computationally expensive. On the other hand, a large number of rare events from a large corpus are often modeled by the cross-entropy loss to be assigned with too much weight.
The GloVe Model
In view of this, the GloVe model makes three changes to the skip-gram model based on squared loss Pennington.Socher.Manning.2014:
- Use variables and that are not probability distributions and take the logarithm of both, so the squared loss term is .
- Add two scalar model parameters for each word : the center word bias and the context word bias .
- Replace the weight of each loss term with the weight function , where is increasing in the interval of .
Putting all things together, training GloVe is to minimize the following loss function:
For the weight function, a suggested choice is: (e.g ) if (e.g., ); otherwise . In this case, because , the squared loss term for any can be omitted for computational efficiency. For example, when using minibatch stochastic gradient descent for training, at each iteration we randomly sample a minibatch of non-zero to calculate gradients and update the model parameters. Note that these non-zero are precomputed global corpus statistics; thus, the model is called GloVe for Global Vectors.
It should be emphasized that if word appears in the context window of word , then vice versa. Therefore, . Unlike word2vec that fits the asymmetric conditional probability , GloVe fits the symmetric . Therefore, the center word vector and the context word vector of any word are mathematically equivalent in the GloVe model. However in practice, owing to different initialization values, the same word may still get different values in these two vectors after training: GloVe sums them up as the output vector.
Interpreting GloVe from the Ratio of Co-occurrence Probabilities
We can also interpret the GloVe model from another perspective. Using the same notation in subsec_skipgram-global, let be the conditional probability of generating the context word given as the center word in the corpus. tab_glove lists several co-occurrence probabilities given words "ice" and "steam" and their ratios based on statistics from a large corpus.
:Word-word co-occurrence probabilities and their ratios from a large corpus (adapted from Table 1 in Pennington.Socher.Manning.2014:)
| = | solid | gas | water | fashion |
|---|---|---|---|---|
| 0.00019 | 0.000066 | 0.003 | 0.000017 | |
| 0.000022 | 0.00078 | 0.0022 | 0.000018 | |
| 8.9 | 0.085 | 1.36 | 0.96 |
We can observe the following from tab_glove:
- For a word that is related to "ice" but unrelated to "steam", such as , we expect a larger ratio of co-occurence probabilities, such as 8.9.
- For a word that is related to "steam" but unrelated to "ice", such as , we expect a smaller ratio of co-occurence probabilities, such as 0.085.
- For a word that is related to both "ice" and "steam", such as , we expect a ratio of co-occurence probabilities that is close to 1, such as 1.36.
- For a word that is unrelated to both "ice" and "steam", such as , we expect a ratio of co-occurence probabilities that is close to 1, such as 0.96.
It can be seen that the ratio of co-occurrence probabilities can intuitively express the relationship between words. Thus, we can design a function of three word vectors to fit this ratio. For the ratio of co-occurrence probabilities with being the center word and and being the context words, we want to fit this ratio using some function :
Among many possible designs for , we only pick a reasonable choice in the following. Since the ratio of co-occurrence probabilities is a scalar, we require that be a scalar function, such as . Switching word indices and in eq_glove-f, it must hold that , so one possibility is , i.e.,
Now let us pick , where is a constant. Since , after taking the logarithm on both sides we get . We may use additional bias terms to fit , such as the center word bias and the context word bias :
Measuring the squared error of eq_glove-square with weights, the GloVe loss function in eq_glove-loss is obtained.
Summary
- The skip-gram model can be interpreted using global corpus statistics such as word-word co-occurrence counts.
- The cross-entropy loss may not be a good choice for measuring the difference of two probability distributions, especially for a large corpus. GloVe uses squared loss to fit precomputed global corpus statistics.
- The center word vector and the context word vector are mathematically equivalent for any word in GloVe.
- GloVe can be interpreted from the ratio of word-word co-occurrence probabilities.
Exercises
- If words and co-occur in the same context window, how can we use their distance in the text sequence to redesign the method for calculating the conditional probability ? Hint: see Section 4.2 of the GloVe paper Pennington.Socher.Manning.2014.
- For any word, are its center word bias and context word bias mathematically equivalent in GloVe? Why?
