18.5 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's first revisit the skip-gram model in Section 18.1.3, but interpreting it using global corpus statistics such as co-occurrence counts.
18.5.1 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's 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 , (18.5.2) can be rewritten as
In (18.5.3), 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 (18.5.3) 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 the one hand, as we mentioned in Section 18.2, 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.
18.5.2 The GloVe Model#
In view of this, the GloVe model makes three changes to the skip-gram model based on squared loss (Pennington et al., 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.
18.5.3 Interpreting GloVe from the Ratio of Co-occurrence Probabilities#
We can also interpret the GloVe model from another perspective. Using the same notation in Section 18.5.1, let be the conditional probability of generating the context word given as the center word in the corpus. Throughout this section, the first index of denotes the conditioning center word and the second index the context word being generated; thus below means "probability of context given center ". Table 18.5.1 lists several co-occurrence probabilities given words "ice" and "steam" and their ratios based on statistics from a large corpus.
| = | 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 Table 18.5.1:
- For a word that is related to "ice" but unrelated to "steam", such as , we expect a larger ratio of co-occurrence 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-occurrence probabilities, such as 0.085.
- For a word that is related to both "ice" and "steam", such as , we expect a ratio of co-occurrence 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-occurrence 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 and being the center words (played by "ice" and "steam" in Table 18.5.1) and being the shared context word, 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 (18.5.5), it must hold that , so one possibility is , i.e.,
Now let's pick , where is a constant. Since , after taking the logarithm on both sides we get . Renaming and so that the final relation is stated in the conventional form (center word , context word ) and absorbing into bias terms ( for the center word and for the context word), we obtain
Measuring the squared error of (18.5.7) with weights, the GloVe loss function in (18.5.4) is obtained.
18.5.4 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.
18.5.5 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 et al., 2014).
- For any word, are its center word bias and context word bias mathematically equivalent in GloVe? Why?
- Let , and define the pointwise mutual information
, where
as in the text.
- Show that with the biases fixed to and , the relation (18.5.7) becomes .
- Suppose that a word becomes twice as frequent while the words it co-occurs with stay the same, so that every count in its row and column doubles. Which of and stays approximately unchanged? What do the biases therefore absorb?
- What is for two words that never co-occur, and how does avoid the problem? Compare with skip-gram with negative sampling, whose dot products approximate for noise words (Levy and Goldberg, 2014).
- The derivation of (18.5.7) requires
and then picks .
- Show that satisfies this condition for every odd function , so that the condition alone does not single out the exponential function.
- The next step uses a stronger property: it writes as for all vectors, that is, for all real and . Show that every continuous with this property is for some constant .
- Explain why the value of does not matter for the model.
- Let be an invertible matrix.
- Show that the loss (18.5.4) does not change when every is replaced by and every by .
- Which of the quantities , , and the cosine similarity of and are unchanged for every invertible , and which only for orthogonal ?
- The loss cannot prefer one over another. What then determines the cosine similarities of the trained vectors, and what does this imply for interpreting individual coordinates of a word vector?