GloVe › Deriving the GloVe Objective

Michael BrenndoerferApril 10, 20255 min read
Language AI HandbookAll chapters
Part 4 of 67Word Embeddings
Chapter 25GloVe › Deriving the GloVe Objective
Reading section 3 of 13 · Deriving the GloVe Objective · 5 minSection 3 of 13 · 5 min

Book contents

Section 3 of 13 · 5 min read

GloVe › Deriving the GloVe ObjectiveOne section · Sections in this chapter3 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The derivation starts from the ratios and works backward to a tractable optimization objective. Each step follows necessarily from a design choice, so the final formula is not arbitrary: it is the natural consequence of the ratio insight combined with a small set of reasonable constraints.

Starting from Co-Occurrence Ratios

We want word vectors w⃗i\vec{w}_i, w⃗j\vec{w}_j, and w⃗k\vec{w}_k such that some function FF of these vectors recovers the ratio:

F(w⃗i,w⃗j,w⃗k)=PikPjkF(\vec{w}_i, \vec{w}_j, \vec{w}_k) = \frac{P_{ik}}{P_{jk}}

where:

  • w⃗i\vec{w}_i: the word vector for word ii (the first target word)
  • w⃗j\vec{w}_j: the word vector for word jj (the second target word we are comparing against)
  • w⃗k\vec{w}_k: the vector for the probe word kk
  • Pik=P(k∣i)=Xik/XiP_{ik} = P(k \mid i) = X_{ik}/X_i: the conditional probability of seeing kk in the context of ii

We want this function to depend on the difference w⃗i−w⃗j\vec{w}_i - \vec{w}_j, because we are comparing two target words and the comparison should be captured as a vector difference. The simplest way to combine a difference vector with a third vector is via a dot product:

F((w⃗i−w⃗j)Tw⃗~k)=PikPjkF\left((\vec{w}_i - \vec{w}_j)^T \tilde{\vec{w}}_k\right) = \frac{P_{ik}}{P_{jk}}

Here w⃗~k\tilde{\vec{w}}_k is a separate vector for word kk when it is a context word, distinct from w⃗k\vec{w}_k when kk is the target word. This distinction is standard in embedding models: each word plays two roles (as a target and as a context), and the two roles are learned separately. Using separate vectors for the two roles avoids conflating them and is a key part of why the derivation works out cleanly.

Solving for the Functional Form

We need FF to satisfy the ratio decomposition property. When we expand the ratio Pik/PjkP_{ik}/P_{jk}, we are taking the ratio of two quantities that each involve a single target word and the probe word kk. So we need:

F((w⃗i−w⃗j)Tw⃗~k)=F(w⃗iTw⃗~k)F(w⃗jTw⃗~k)F\left((\vec{w}_i - \vec{w}_j)^T \tilde{\vec{w}}_k\right) = \frac{F(\vec{w}_i^T \tilde{\vec{w}}_k)}{F(\vec{w}_j^T \tilde{\vec{w}}_k)}

This is a functional equation: we need a function FF such that F(a−b)=F(a)/F(b)F(a - b) = F(a)/F(b) for all aa and bb. The exponential function satisfies this exactly, since:

exp⁡(a−b)=exp⁡(a)exp⁡(b)\exp(a - b) = \frac{\exp(a)}{\exp(b)}

This is the only continuous function (up to a constant multiplier) that converts subtraction into division. The uniqueness here is reassuring: we are not choosing exp⁡\exp arbitrarily, it is forced by the design requirement.

Plugging in F=exp⁡F = \exp and setting the result equal to the true ratio Pik/PjkP_{ik}/P_{jk} gives us, for each individual word pair (i,k)(i, k):

exp⁡(w⃗iTw⃗~k)=Pik=XikXi\exp(\vec{w}_i^T \tilde{\vec{w}}_k) = P_{ik} = \frac{X_{ik}}{X_i}

Taking the natural logarithm of both sides:

w⃗iTw⃗~k=log⁡Pik=log⁡XikXi=log⁡Xik−log⁡Xi\begin{aligned} \vec{w}_i^T \tilde{\vec{w}}_k &= \log P_{ik} \\ &= \log \frac{X_{ik}}{X_i} \\ &= \log X_{ik} - \log X_i \end{aligned}

The term log⁡Xi\log X_i depends only on word ii, not on the context word kk. It represents word ii's overall frequency as a context center: a word that appears in many contexts has a large XiX_i. We can absorb this word-frequency effect into a scalar bias bib_i learned during training. Similarly, by symmetry, we add a context bias b~k\tilde{b}_k for word kk's frequency as a context word:

w⃗iTw⃗~k+bi+b~k=log⁡Xik\vec{w}_i^T \tilde{\vec{w}}_k + b_i + \tilde{b}_k = \log X_{ik}

where:

  • bib_i: a scalar bias for word ii that absorbs its context frequency log⁡Xi\log X_i
  • b~k\tilde{b}_k: a scalar bias for word kk in its role as a context word

The context bias b~k\tilde{b}_k is not strictly required by the derivation, but it restores symmetry. Because Xik=XkiX_{ik} = X_{ki} for symmetric windows, both the target role and the context role of each word should have their own frequency-absorbing bias. Adding b~k\tilde{b}_k makes the formulation symmetric in the roles of target and context.

This equation is the core relationship GloVe trains for. We want, for every word pair (i,j)(i, j), the model's prediction w⃗iTw⃗~j+bi+b~j\vec{w}_i^T \tilde{\vec{w}}_j + b_i + \tilde{b}_j to equal the corpus-derived target log⁡Xij\log X_{ij}.

The Weighted Least Squares Objective

We want our word vectors and biases to satisfy w⃗iTw⃗~j+bi+b~j≈log⁡Xij\vec{w}_i^T \tilde{\vec{w}}_j + b_i + \tilde{b}_j \approx \log X_{ij} for all (i,j)(i, j) pairs. The natural formulation is a least squares objective over all V2V^2 word pairs:

J=∑i,j=1V(w⃗iTw⃗~j+bi+b~j−log⁡Xij)2J = \sum_{i,j=1}^{V} \left(\vec{w}_i^T \tilde{\vec{w}}_j + b_i + \tilde{b}_j - \log X_{ij}\right)^2

But this treats all co-occurrence pairs equally, and that creates two serious problems. First, rare co-occurrences (small XijX_{ij}) are statistically noisy. If "quasar" and "kumquat" appeared together once in a 100-billion-token corpus, that single co-occurrence is almost certainly an accident, not evidence of a semantic relationship. Yet the unweighted objective would treat this pair as equally important as "cat" co-occurring with "dog" 10,000 times. Second, most entries of XX are zero (words that never co-occurred), and log⁡0\log 0 is undefined, so the plain objective is not even computable.

The solution is to weight each term by a function f(Xij)f(X_{ij}) that satisfies four properties:

  1. Zero co-occurrences get zero weight, so log⁡0\log 0 never appears in the sum
  2. Rare co-occurrences get lower weight, reducing the influence of statistical noise
  3. Very frequent co-occurrences do not get excessive weight (stop words like "the" co-occur with almost everything, but without a semantic relationship)
  4. The function is continuous and non-decreasing

This gives the final GloVe objective:

J=∑i,j=1Vf(Xij)(w⃗iTw⃗~j+bi+b~j−log⁡Xij)2J = \sum_{i,j=1}^{V} f(X_{ij}) \left(\vec{w}_i^T \tilde{\vec{w}}_j + b_i + \tilde{b}_j - \log X_{ij}\right)^2

where:

  • f(Xij)f(X_{ij}): the weighting function that scales each term's contribution by the reliability of the co-occurrence count
  • w⃗iTw⃗~j+bi+b~j\vec{w}_i^T \tilde{\vec{w}}_j + b_i + \tilde{b}_j: the model's prediction of log⁡Xij\log X_{ij}
  • log⁡Xij\log X_{ij}: the target value derived from the corpus

This is the weighted least squares objective that GloVe minimizes. Because f(0)=0f(0) = 0, the sum effectively runs only over pairs where Xij>0X_{ij} > 0. For large vocabularies this is a small fraction of all V2V^2 pairs, making the sum tractable.