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 qijq_{ij} the conditional probability P(wj∣wi)P(w_j\mid w_i) of word wjw_j given word wiw_i in the skip-gram model, we have

qij=exp⁡(uj⊤vi)∑k∈Vexp⁡(uk⊤vi),(18.5.1)q_{ij}=\frac{\exp(\mathbf{u}_j^\top \mathbf{v}_i)}{ \sum_{k \in \mathcal{V}} \exp(\mathbf{u}_k^\top \mathbf{v}_i)}, \tag{18.5.1}

where for any index ii vectors vi\mathbf{v}_i and ui\mathbf{u}_i represent word wiw_i as the center word and context word, respectively, and V={0,1,…,∣V∣−1}\mathcal{V} = \{0, 1, \ldots, |\mathcal{V}|-1\} is the index set of the vocabulary.

Consider word wiw_i that may occur multiple times in the corpus. In the entire corpus, all the context words wherever wiw_i is taken as their center word form a multiset Ci\mathcal{C}_i 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 wiw_i occurs twice in the corpus and indices of the context words that take wiw_i as their center word in the two context windows are k,j,m,kk, j, m, k and k,l,k,jk, l, k, j. Thus, multiset Ci={j,j,k,k,k,k,l,m}\mathcal{C}_i = \{j, j, k, k, k, k, l, m\}, where multiplicities of elements j,k,l,mj, k, l, m are 2, 4, 1, 1, respectively.

Now let's denote the multiplicity of element jj in multiset Ci\mathcal{C}_i as xijx_{ij}. This is the global co-occurrence count of word wjw_j (as the context word) and word wiw_i (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

−∑i∈V∑j∈Vxijlog⁡ qij.(18.5.2)-\sum_{i\in\mathcal{V}}\sum_{j\in\mathcal{V}} x_{ij} \log\,q_{ij}. \tag{18.5.2}

We further denote by xix_i the number of all the context words in the context windows where wiw_i occurs as their center word, which is equivalent to ∣Ci∣|\mathcal{C}_i|. Letting pijp_{ij} be the conditional probability xij/xix_{ij}/x_i for generating context word wjw_j given center word wiw_i, (18.5.2) can be rewritten as

−∑i∈Vxi∑j∈Vpijlog⁡ qij.(18.5.3)-\sum_{i\in\mathcal{V}} x_i \sum_{j\in\mathcal{V}} p_{ij} \log\,q_{ij}. \tag{18.5.3}

In (18.5.3), −∑j∈Vpijlog⁡ qij-\sum_{j\in\mathcal{V}} p_{ij} \log\,q_{ij} calculates the cross-entropy of the conditional distribution pijp_{ij} of global corpus statistics and the conditional distribution qijq_{ij} of model predictions. This loss is also weighted by xix_i 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 qijq_{ij} 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):

  1. Use variables pij′=xijp'_{ij}=x_{ij} and qij′=exp⁡(uj⊤vi)q'_{ij}=\exp(\mathbf{u}_j^\top \mathbf{v}_i) that are not probability distributions and take the logarithm of both, so the squared loss term is (log⁡ pij′−log⁡ qij′)2=(uj⊤vi−log⁡ xij)2\left(\log\,p'_{ij} - \log\,q'_{ij}\right)^2 = \left(\mathbf{u}_j^\top \mathbf{v}_i - \log\,x_{ij}\right)^2.
  2. Add two scalar model parameters for each word wiw_i: the center word bias bib_i and the context word bias cic_i.
  3. Replace the weight of each loss term with the weight function h(xij)h(x_{ij}), where h(x)h(x) is increasing in the interval of [0,1][0, 1].

Putting all things together, training GloVe is to minimize the following loss function:

∑i∈V∑j∈Vh(xij)(uj⊤vi+bi+cj−log⁡ xij)2.(18.5.4)\sum_{i\in\mathcal{V}} \sum_{j\in\mathcal{V}} h(x_{ij}) \left(\mathbf{u}_j^\top \mathbf{v}_i + b_i + c_j - \log\,x_{ij}\right)^2. \tag{18.5.4}

For the weight function, a suggested choice is: h(x)=(x/c)αh(x) = (x/c) ^\alpha (e.g α=0.75\alpha = 0.75) if x<cx \lt c (e.g., c=100c = 100); otherwise h(x)=1h(x) = 1. In this case, because h(0)=0h(0)=0, the squared loss term for any xij=0x_{ij}=0 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 xijx_{ij} to calculate gradients and update the model parameters. Note that these non-zero xijx_{ij} are precomputed global corpus statistics; thus, the model is called GloVe for Global Vectors.

It should be emphasized that if word wiw_i appears in the context window of word wjw_j, then vice versa. Therefore, xij=xjix_{ij}=x_{ji}. Unlike word2vec that fits the asymmetric conditional probability pijp_{ij}, GloVe fits the symmetric log⁡ xij\log \, x_{ij}. 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 pij=defP(wj∣wi)p_{ij} \stackrel{\textrm{def}}{=} P(w_j \mid w_i) be the conditional probability of generating the context word wjw_j given wiw_i as the center word in the corpus. Throughout this section, the first index of pp denotes the conditioning center word and the second index the context word being generated; thus pjip_{ji} below means "probability of context wiw_i given center wjw_j". Table 18.5.1 lists several co-occurrence probabilities given words "ice" and "steam" and their ratios based on statistics from a large corpus.

Table 18.5.1. Word-word co-occurrence probabilities and their ratios from a large corpus (adapted from Table 1 in Pennington et al. (2014))
wkw_k=solidgaswaterfashion
p1=P(wk∣ice)p_1=P(w_k\mid \textrm{ice})0.000190.0000660.0030.000017
p2=P(wk∣steam)p_2=P(w_k\mid\textrm{steam})0.0000220.000780.00220.000018
p1/p2p_1/p_28.90.0851.360.96

We can observe the following from Table 18.5.1:

  • For a word wkw_k that is related to "ice" but unrelated to "steam", such as wk=solidw_k=\textrm{solid}, we expect a larger ratio of co-occurrence probabilities, such as 8.9.
  • For a word wkw_k that is related to "steam" but unrelated to "ice", such as wk=gasw_k=\textrm{gas}, we expect a smaller ratio of co-occurrence probabilities, such as 0.085.
  • For a word wkw_k that is related to both "ice" and "steam", such as wk=waterw_k=\textrm{water}, we expect a ratio of co-occurrence probabilities that is close to 1, such as 1.36.
  • For a word wkw_k that is unrelated to both "ice" and "steam", such as wk=fashionw_k=\textrm{fashion}, 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 pji/pki{p_{ji}}/{p_{ki}} with wjw_j and wkw_k being the center words (played by "ice" and "steam" in Table 18.5.1) and wiw_i being the shared context word, we want to fit this ratio using some function ff:

f(vj,vk,ui)≈pjipki.(18.5.5)f(\mathbf{v}_j, \mathbf{v}_k, \mathbf{u}_i) \approx \frac{p_{ji}}{p_{ki}}. \tag{18.5.5}

Among many possible designs for ff, we only pick a reasonable choice in the following. Since the ratio of co-occurrence probabilities is a scalar, we require that ff be a scalar function, such as f(vj,vk,ui)=f((vj−vk)⊤ui)f(\mathbf{v}_j, \mathbf{v}_k, \mathbf{u}_i) = f\left((\mathbf{v}_j - \mathbf{v}_k)^\top \mathbf{u}_i\right). Switching word indices jj and kk in (18.5.5), it must hold that f(x)f(−x)=1f(x)f(-x)=1, so one possibility is f(x)=exp⁡(x)f(x)=\exp(x), i.e.,

f(vj,vk,ui)=exp⁡(vj⊤ui)exp⁡(vk⊤ui)≈pjipki.(18.5.6)f(\mathbf{v}_j, \mathbf{v}_k, \mathbf{u}_i) = \frac{\exp\left(\mathbf{v}_j^\top \mathbf{u}_i\right)}{\exp\left(\mathbf{v}_k^\top \mathbf{u}_i\right)} \approx \frac{p_{ji}}{p_{ki}}. \tag{18.5.6}

Now let's pick exp⁡(vj⊤ui)≈αpji\exp\left(\mathbf{v}_j^\top \mathbf{u}_i\right) \approx \alpha p_{ji}, where α\alpha is a constant. Since pji=xji/xjp_{ji}=x_{ji}/x_j, after taking the logarithm on both sides we get vj⊤ui≈log⁡ α+log⁡ xji−log⁡ xj\mathbf{v}_j^\top \mathbf{u}_i \approx \log\,\alpha + \log\,x_{ji} - \log\,x_j. Renaming j→ij \to i and i→ji \to j so that the final relation is stated in the conventional form (center word wiw_i, context word wjw_j) and absorbing −log⁡α+log⁡xi-\log\alpha + \log x_i into bias terms (bib_i for the center word and cjc_j for the context word), we obtain

uj⊤vi+bi+cj≈log⁡ xij.(18.5.7)\mathbf{u}_j^\top \mathbf{v}_i + b_i + c_j \approx \log\, x_{ij}. \tag{18.5.7}

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#

  1. If words wiw_i and wjw_j 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 pijp_{ij}? Hint: see Section 4.2 of the GloVe paper (Pennington et al., 2014).
  2. For any word, are its center word bias and context word bias mathematically equivalent in GloVe? Why?
  3. Let X=∑i,jxijX = \sum_{i, j} x_{ij}, and define the pointwise mutual information PMIij=log⁡(xijX/(xixj))\textrm{PMI}_{ij} = \log \left(x_{ij} X / (x_i x_j)\right), where xj=∑ixjix_j = \sum_i x_{ji} as in the text.
    1. Show that with the biases fixed to bi=log⁡xi−12log⁡Xb_i = \log x_i - \frac{1}{2} \log X and cj=log⁡xj−12log⁡Xc_j = \log x_j - \frac{1}{2} \log X, the relation (18.5.7) becomes uj⊤vi≈PMIij\mathbf{u}_j^\top \mathbf{v}_i \approx \textrm{PMI}_{ij}.
    2. 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 log⁡xij\log x_{ij} and PMIij\textrm{PMI}_{ij} stays approximately unchanged? What do the biases therefore absorb?
    3. What is PMIij\textrm{PMI}_{ij} for two words that never co-occur, and how does h(0)=0h(0) = 0 avoid the problem? Compare with skip-gram with negative sampling, whose dot products approximate PMIij−log⁡K\textrm{PMI}_{ij} - \log K for KK noise words (Levy and Goldberg, 2014).
  4. The derivation of (18.5.7) requires f(x)f(−x)=1f(x) f(-x) = 1 and then picks f(x)=exp⁡(x)f(x) = \exp(x).
    1. Show that f(x)=exp⁡(g(x))f(x) = \exp(g(x)) satisfies this condition for every odd function gg, so that the condition alone does not single out the exponential function.
    2. The next step uses a stronger property: it writes f((vj−vk)⊤ui)f\left((\mathbf{v}_j - \mathbf{v}_k)^\top \mathbf{u}_i\right) as f(vj⊤ui)/f(vk⊤ui)f(\mathbf{v}_j^\top \mathbf{u}_i) / f(\mathbf{v}_k^\top \mathbf{u}_i) for all vectors, that is, f(a−b)=f(a)/f(b)f(a - b) = f(a) / f(b) for all real aa and bb. Show that every continuous f>0f > 0 with this property is f(x)=exp⁡(λx)f(x) = \exp(\lambda x) for some constant λ\lambda.
    3. Explain why the value of λ\lambda does not matter for the model.
  5. Let A\mathbf{A} be an invertible d×dd \times d matrix.
    1. Show that the loss (18.5.4) does not change when every vi\mathbf{v}_i is replaced by Avi\mathbf{A} \mathbf{v}_i and every uj\mathbf{u}_j by A−⊤uj\mathbf{A}^{-\top} \mathbf{u}_j.
    2. Which of the quantities uj⊤vi\mathbf{u}_j^\top \mathbf{v}_i, vi⊤vk\mathbf{v}_i^\top \mathbf{v}_k, and the cosine similarity of vi\mathbf{v}_i and vk\mathbf{v}_k are unchanged for every invertible A\mathbf{A}, and which only for orthogonal A\mathbf{A}?
    3. The loss cannot prefer one A\mathbf{A} 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?

Discussions