intermediate representations 40 min read

GloVe and the Geometry of Word Embeddings

GloVe fits log co-occurrence counts by a tempered Poisson least squares that never sees the pairs that did not occur; its biases widen the gauge to affine maps, so an exact analogy is a fact about count ratios while the answer a nearest-neighbor search returns depends on the factorization

Overview & Motivation

Take the eight-word-window co-occurrence counts of the distributional-semantics topic and fit three models to that one matrix at one rank, d=25d = 25: the truncated SVD of shifted PPMI that topic selected, the skip-gram with negative sampling of the word2vec topic, and GloVe with its published weight and settings. Score each on the planted-sector benchmark, the probability that two nouns from the same sector are closer than two from different sectors. The SVD scores 0.9730.973 and negative sampling 0.8860.886. GloVe scores 0.4760.476, about 1.11.1 standard errors from a coin flip.

Nothing went wrong in training. GloVe fits the logarithms of the counts, and a logarithm needs a count: its loss runs over the pairs that occurred and ignores every other cell. In this corpus 69%69\% of the pairs that occurred occurred once, and log⁡1=0\log 1 = 0. Most of what GloVe sees is a scatter of zeros, and what distinguishes the sectors, which pairs occur at all, is the one thing its loss cannot see.

The second experiment is about geometry. The corpus states facts from a fixed knowledge base, among them the region of each headquarters city, so we can ask the word2vec model Houston : Texas :: Boston : ? and score its answers by vector arithmetic. It solves 63.3%63.3\% of the 150150 city-to-region questions. Now change the basis of its vectors without changing a single word–context score, so the training loss is identical. It solves 25.3%25.3\%.

This topic proves what lies behind both experiments.

  1. GloVe is a tempered Poisson fit. Its loss is the quadratic approximation of a Poisson log-bilinear likelihood, with the count’s own weight capped and every unobserved pair dropped. Negative sampling, near its optimum, is the same least squares with frozen biases and a third weight.
  2. What the loss cannot see. With every pair observed and flat weights, GloVe is exactly an SVD of the double-centered PMI matrix. With only the observed pairs, an additive pattern of log counts is fitted by the biases alone, and the vectors learn nothing about which pairs occur.
  3. The geometry it leaves free. GloVe’s biases widen the freedom of the factorization from changes of basis to affine maps. An exact analogy parallelogram is a statement about count ratios and survives all of them. The answer vector arithmetic returns does not.

A log-bilinear model of counts

Write XijX_{ij} for the distributional-semantics topic’s pair counts at the eight-word window (its Definition 1): N=576,098N = 576{,}098 pair occurrences over 143,345143{,}345 observed cells, 1.22%1.22\% of the 3,42523{,}425^2 possible pairs. Let Ω\Omega be the set of observed cells, those with Xij>0X_{ij} \gt 0. Each word gets a vector wi∈Rd\mathbf{w}_i \in \mathbb{R}^d and a bias bib_i, each context a vector cj\mathbf{c}_j and a bias b~j\tilde b_j, and the score of a pair is

sij=⟨wi,cj⟩+bi+b~j.s_{ij} = \langle \mathbf{w}_i, \mathbf{c}_j \rangle + b_i + \tilde b_j .

Definition 1 (GloVe).

GloVe chooses WW, CC, bb, and b~\tilde b to minimize the weighted least-squares loss on the observed cells,

J(W,C,b,b~)=∑(i,j)∈Ωf(Xij)(sij−log⁡Xij)2,f(x)=min⁡{1,(x/xmax⁡)γ},J(W, C, b, \tilde b) = \sum_{(i,j) \in \Omega} f(X_{ij}) \bigl( s_{ij} - \log X_{ij} \bigr)^2, \qquad f(x) = \min\bigl\{1, (x / x_{\max})^\gamma\bigr\},

with xmax⁡=100x_{\max} = 100 and γ=34\gamma = \tfrac34. The reported word vectors are W+CW + C.

This is Pennington, Socher, and Manning’s (2014) model. Their exponent is called α\alpha; we write γ\gamma because α\alpha is already the context-smoothing exponent of the last two topics. The weight ff grows with the count and stops growing at xmax⁡x_{\max}. On this corpus only 816816 observed cells reach the cap, so nearly every cell has weight (Xij/100)3/4(X_{ij}/100)^{3/4}, and a pair seen once has weight 0.03160.0316.

Why logarithms, and why a dot product? Pennington and colleagues argued from ratios. Whether a context jj tells words ii and kk apart shows in the ratio P(j∣i)/P(j∣k)P(j \mid i)/P(j \mid k), so the model should turn differences of word vectors into ratios of probabilities. The argument can be made exact, and stating its hypotheses shows how much it assumes.

Proposition 1 (The ratio argument, with its hypotheses).

Let F:R→(0,∞)F: \mathbb{R} \to (0, \infty) be continuous with F(u−v)=F(u)/F(v)F(u - v) = F(u)/F(v) for all u,vu, v. Then F(u)=eλuF(u) = e^{\lambda u} for some λ∈R\lambda \in \mathbb{R}. If in addition every Xij>0X_{ij} \gt 0, so that every ratio below is defined and positive, and

F(⟨wi−wk,cj⟩)=P(j∣i)P(j∣k)F\bigl(\langle \mathbf{w}_i - \mathbf{w}_k, \mathbf{c}_j \rangle\bigr) = \frac{P(j \mid i)}{P(j \mid k)}

for all words i,ki, k and contexts jj, and λ≠0\lambda \ne 0, then ⟨wi,cj⟩=λ−1(log⁡Xij−log⁡Xi⋅+βj)\langle \mathbf{w}_i, \mathbf{c}_j \rangle = \lambda^{-1}\bigl(\log X_{ij} - \log X_{i\cdot} + \beta_j\bigr) for constants βj\beta_j, where Xi⋅=∑jXijX_{i\cdot} = \sum_j X_{ij}.

Proof.

Let G=log⁡FG = \log F, so G(u−v)=G(u)−G(v)G(u - v) = G(u) - G(v). Taking u=vu = v gives G(0)=0G(0) = 0, taking u=0u = 0 gives G(−v)=−G(v)G(-v) = -G(v), and so G(u+v)=G(u−(−v))=G(u)+G(v)G(u + v) = G\bigl(u - (-v)\bigr) = G(u) + G(v), Cauchy’s equation. By induction G(nu)=nG(u)G(nu) = nG(u), so G(p/q)=(p/q) G(1)G(p/q) = (p/q)\,G(1) for every rational p/qp/q, and continuity extends this to G(u)=λuG(u) = \lambda u with λ=G(1)\lambda = G(1) (Aczél and Dhombres 1989). Then the ratio identity says F(⟨wi,cj⟩)/P(j∣i)=F(⟨wk,cj⟩)/P(j∣k)F(\langle \mathbf{w}_i, \mathbf{c}_j \rangle)/P(j \mid i) = F(\langle \mathbf{w}_k, \mathbf{c}_j \rangle)/P(j \mid k) for all i,ki, k, so this quantity is some gj>0g_j \gt 0 that depends on the context alone. Hence λ⟨wi,cj⟩=log⁡P(j∣i)+log⁡gj\lambda \langle \mathbf{w}_i, \mathbf{c}_j \rangle = \log P(j \mid i) + \log g_j, with P(j∣i)=Xij/Xi⋅P(j \mid i) = X_{ij}/X_{i\cdot}.

∎

Absorb λ\lambda into the vectors and the model is log⁡Xij≈⟨wi,cj⟩+log⁡Xi⋅−βj\log X_{ij} \approx \langle \mathbf{w}_i, \mathbf{c}_j \rangle + \log X_{i\cdot} - \beta_j. The word term log⁡Xi⋅\log X_{i\cdot} is fixed and the context term is free, a lopsidedness the counts do not have, since a symmetric window makes XX symmetric. Replacing the word term with a free bias bib_i gives Definition 1’s score. Each step is a choice, not a consequence of anything about language: that ratios are the statistic to model, that a function of a dot product of differences should produce them, and that symmetry should be restored by hand.

The model the argument lands on is one the distributional-semantics topic already met.

Proposition 2 (An exact fit makes PMI rank at most d + 2).

If every Xij>0X_{ij} \gt 0 and sij=log⁡Xijs_{ij} = \log X_{ij} on every cell, then

PMI⁡=WC⊤+a1⊤+1β⊤,ai=bi−log⁡Xi⋅+log⁡N,βj=b~j−log⁡X⋅j,\operatorname{PMI} = W C^\top + a \mathbf{1}^\top + \mathbf{1} \beta^\top, \qquad a_i = b_i - \log X_{i\cdot} + \log N, \quad \beta_j = \tilde b_j - \log X_{\cdot j},

so rank⁡PMI⁡≤d+2\operatorname{rank} \operatorname{PMI} \le d + 2. With context smoothing, PMI⁡α\operatorname{PMI}_\alpha differs from PMI⁡\operatorname{PMI} by a column offset alone and has the same bound.

Proof.

PMI⁡ij=log⁡Xij−log⁡Xi⋅−log⁡X⋅j+log⁡N\operatorname{PMI}_{ij} = \log X_{ij} - \log X_{i\cdot} - \log X_{\cdot j} + \log N. Substitute log⁡Xij=sij\log X_{ij} = s_{ij}; the two offset matrices have rank one each, and rank is subadditive. For PMI⁡α\operatorname{PMI}_\alpha, the term log⁡Pα(j)\log P_\alpha(j) replaces log⁡(X⋅j/N)\log(X_{\cdot j}/N).

∎

This is the third part of the distributional-semantics topic’s Theorem 3, the log-bilinear model, whose PMI matrix has rank at most d+2d + 2 even though the ratio matrix ePMI⁡e^{\operatorname{PMI}} need not be low rank at all. GloVe is that model fitted to counts.


Three weights

Revenue occurs in five contexts that span the range of counts: with plantline once, subswap twice, outperformed 1010 times, fiscal 5353 times, and cost 129129 times. GloVe’s weight runs from f(1)=0.0316f(1) = 0.0316 to f(129)=1f(129) = 1. Negative sampling’s local weight from the word2vec topic, ab/(a+b)ab/(a + b), runs from 0.3400.340 to 21.221.2. Which is right? Neither is forced, and a likelihood shows what both are approximating.

Theorem 1 (GloVe is a tempered, support-restricted Poisson fit).

Let Xij∼Poisson⁡(esij)X_{ij} \sim \operatorname{Poisson}(e^{s_{ij}}) independently, so a cell’s negative log-likelihood is ℓ(s;x)=es−xs+log⁡x!\ell(s; x) = e^s - xs + \log x!.

(i) For x>0x \gt 0, ℓ\ell has the unique minimizer s⋆=log⁡xs^\star = \log x. With δ=s−log⁡x\delta = s - \log x,

ℓ(s;x)−ℓ(s⋆;x)=x(eδ−1−δ)=DA(s,s⋆),\ell(s; x) - \ell(s^\star; x) = x\bigl(e^\delta - 1 - \delta\bigr) = D_A(s, s^\star),

the Bregman divergence of the Poisson log-partition A(s)=esA(s) = e^s. Its curvature at s⋆s^\star is xx, and

12xδ2emin⁡(δ,0)≤x(eδ−1−δ)≤12xδ2emax⁡(δ,0).\tfrac12 x \delta^2 e^{\min(\delta, 0)} \le x\bigl(e^\delta - 1 - \delta\bigr) \le \tfrac12 x \delta^2 e^{\max(\delta, 0)}.

(ii) For x=0x = 0, ℓ(s;0)=es\ell(s; 0) = e^s decreases strictly toward 00 as s→−∞s \to -\infty and has no minimizer.

(iii) Hence 12∑ΩXij(sij−log⁡Xij)2\tfrac12 \sum_{\Omega} X_{ij} (s_{ij} - \log X_{ij})^2 is the second-order Poisson fit on the observed cells, and GloVe’s loss is, up to a factor of 22, that sum with the weight XijX_{ij} replaced by f(Xij)f(X_{ij}) and the unobserved cells’ term ∑(i,j)∉Ωesij\sum_{(i,j) \notin \Omega} e^{s_{ij}} dropped.

Proof.

ℓ′(s)=es−x\ell'(s) = e^s - x and ℓ′′(s)=es>0\ell''(s) = e^s \gt 0, so s⋆=log⁡xs^\star = \log x is the unique minimizer and the curvature there is xx. With es=xeδe^s = x e^\delta, ℓ(s)−ℓ(s⋆)=xeδ−x−xδ\ell(s) - \ell(s^\star) = x e^\delta - x - x\delta, which is A(s)−A(s⋆)−A′(s⋆)(s−s⋆)A(s) - A(s^\star) - A'(s^\star)(s - s^\star) because A′(s⋆)=xA'(s^\star) = x. Taylor’s theorem with the Lagrange remainder (formalCalculus, Mean Value Theorem and Taylor Expansion) gives eδ−1−δ=12δ2eξe^\delta - 1 - \delta = \tfrac12 \delta^2 e^\xi for some ξ\xi between 00 and δ\delta, which is the sandwich. Part (ii) is immediate, and part (iii) collects parts (i) and (ii) over the cells.

∎

The Poisson family’s Bregman divergence is the same exponential-family identity behind the word2vec topic’s Theorem 2, with the exponential in place of the softplus (formalML, Information Geometry), so a low-rank Poisson fit is an exponential-family PCA (Collins, Dasgupta, and Schapire 2001; Cotterell et al. 2017 read skip-gram this way). In regression terms, one Fisher-scoring step of a Poisson regression with log link, taken at the saturated fit, is weighted least squares on log⁡X\log X with weight XX (formalStatistics, Generalized Linear Models, Theorem 2; McCullagh and Nelder 1989). Pennington and colleagues reached least squares on logarithms from skip-gram’s global cross-entropy and then replaced its weight by ff. Theorem 1 names the likelihood their loss is the quadratic of, and says what the replacement does: it caps the trust a frequent pair earns and, by dropping the zero cells, removes the push toward −∞-\infty that the Poisson likelihood, like negative sampling, puts on every pair that never occurred.

Corollary 1 (Three local weights).

Near their optima on observed cells, all three objectives are weighted least squares in a log-count coordinate.

objectivelocal lossweightunobserved cell
Poisson12X(s−log⁡X)2\tfrac12 X (s - \log X)^2XXese^s, pushed toward −∞-\infty
negative sampling12aba+b(⟨w,c⟩+log⁡k#(w)+log⁡Pα(c)−log⁡a)2\tfrac12 \frac{ab}{a + b}\bigl(\langle \mathbf{w}, \mathbf{c} \rangle + \log k\#(w) + \log P_\alpha(c) - \log a\bigr)^2aba+b≤min⁡(a,b)\frac{ab}{a + b} \le \min(a, b)blog⁡(1+e⟨w,c⟩)b \log(1 + e^{\langle \mathbf{w}, \mathbf{c} \rangle}), pushed toward −∞-\infty
GloVef(X)(s−log⁡X)2f(X)(s - \log X)^2f(X)≤1f(X) \le 1no term

Negative sampling’s local loss is GloVe’s residual with the biases frozen at bw=log⁡k#(w)b_w = \log k\#(w) and b~c=log⁡Pα(c)\tilde b_c = \log P_\alpha(c). Because f(X)/Xf(X)/X decreases for every XX, as Xγ−1xmax⁡−γX^{\gamma - 1} x_{\max}^{-\gamma} below the cap and as 1/X1/X above it, GloVe’s share of its weight on every set of rare pairs {X≤t}\{X \le t\} is at least the Poisson fit’s, on every corpus, and strictly larger whenever there are counts on both sides of tt.

Proof.

The Poisson row is Theorem 1. For negative sampling, the word2vec topic’s Corollary 1 gives the local loss 12aba+b(x−x⋆)2\tfrac12 \frac{ab}{a + b}(x - x^\star)^2 with x⋆=log⁡a−log⁡bx^\star = \log a - \log b and b=k#(w)Pα(c)b = k\#(w)P_\alpha(c), and x−x⋆x - x^\star is the stated residual. For the last claim, write w1=r w2w_1 = r\, w_2 with r=f(X)/Xr = f(X)/X decreasing in the count, and let AkA_k and BkB_k be the totals of wkw_k on {X≤t}\{X \le t\} and on {X>t}\{X \gt t\}. Then A1≥r(t)A2A_1 \ge r(t) A_2 and B1≤r(t)B2B_1 \le r(t) B_2, with the second strict when B2>0B_2 \gt 0, so A1/B1>A2/B2A_1/B_1 \gt A_2/B_2 when both sets hold counts, and the share Ak/(Ak+Bk)A_k/(A_k + B_k) of w1w_1 is the larger. When either set is empty both shares are 00 or both are 11.

∎

Shi and Liu (2014) noticed the frozen biases at the optimum: negative sampling’s target is GloVe’s with the biases set to frequency terms rather than learned. The corollary adds the weights, and the corpus shows what they do. On the rows of the benchmark nouns, the word2vec topic’s mechanism check, here is each weight’s share on pairs seen at most four times:

share of cellsGloVe f(X)f(X)Poisson XXnegative sampling aba+b\frac{ab}{a+b}
benchmark nouns’ rows0.9230.9230.721\mathbf{0.721}0.5850.5850.5480.548
all observed cells0.8850.8850.5230.5230.2960.2960.3010.301

GloVe gives most of its weight to exactly the cells whose values carry the least, log counts from 00 to log⁡4\log 4.


What the loss cannot see

Set every observed count of the eight-word window to ee, keeping the pattern of which pairs occur. GloVe fits the result exactly with every word and context vector equal to zero: the biases carry bi+b~j=1b_i + \tilde b_j = 1, and the vectors are free to say nothing. Two results bracket what happens between that extreme and its opposite.

Theorem 2 (With flat weights and every pair observed, GloVe is an SVD of double-centered PMI).

Let X>0X \gt 0 in every cell, L=log⁡XL = \log X, f≡1f \equiv 1, and Jn=I−11⊤/nJ_n = I - \mathbf{1}\mathbf{1}^\top/n. Then min⁡J=∑i>dσi2\min J = \sum_{i \gt d} \sigma_i^2, where σ1≥σ2≥…\sigma_1 \ge \sigma_2 \ge \dots are the singular values of JnLJm=JnPMI⁡JmJ_n L J_m = J_n \operatorname{PMI} J_m. At every minimizer the score matrix is

S=(JnPMI⁡Jm)d+(L−JnLJm),S = \bigl(J_n \operatorname{PMI} J_m\bigr)_d + \bigl(L - J_n L J_m\bigr),

unique when σd>σd+1\sigma_d \gt \sigma_{d+1}, where (⋅)d(\cdot)_d is the best rank-dd approximation; one minimizer takes W=UdΣd1/2W = U_d \Sigma_d^{1/2} and C=VdΣd1/2C = V_d \Sigma_d^{1/2} from the SVD of JnLJmJ_n L J_m.

Proof.

Let D\mathcal{D} be the matrices with zero row and column sums and O={a1⊤+1β⊤}\mathcal{O} = \{a \mathbf{1}^\top + \mathbf{1} \beta^\top\}. For M∈DM \in \mathcal{D}, ⟨M,a1⊤+1β⊤⟩F=a⊤M1+1⊤Mβ=0\langle M, a \mathbf{1}^\top + \mathbf{1}\beta^\top \rangle_F = a^\top M \mathbf{1} + \mathbf{1}^\top M \beta = 0, and dim⁡D+dim⁡O=(n−1)(m−1)+(n+m−1)=nm\dim \mathcal{D} + \dim \mathcal{O} = (n - 1)(m - 1) + (n + m - 1) = nm, so the two are orthogonal complements and M↦JnMJmM \mapsto J_n M J_m is the projection onto D\mathcal{D}. The biases range over all of O\mathcal{O}, so by Pythagoras J=∥Jn(L−WC⊤)Jm∥F2J = \lVert J_n (L - WC^\top) J_m \rVert_F^2 plus a term the biases make zero. As WC⊤W C^\top ranges over matrices of rank at most dd, JnWC⊤JmJ_n W C^\top J_m ranges over the elements of D\mathcal{D} of rank at most dd, and each is attained with WC⊤W C^\top equal to itself. The best approximation of JnLJmJ_n L J_m among them is its truncated SVD (Eckart and Young 1936; formalML, Singular Value Decomposition, Theorem 2), which lies in D\mathcal{D} because its singular vectors lie in the row and column spaces of JnLJmJ_n L J_m. Finally L−PMI⁡=(log⁡Xi⋅)1⊤+1(log⁡X⋅j)⊤−log⁡N 11⊤∈OL - \operatorname{PMI} = (\log X_{i\cdot}) \mathbf{1}^\top + \mathbf{1} (\log X_{\cdot j})^\top - \log N\, \mathbf{1}\mathbf{1}^\top \in \mathcal{O}, so JnLJm=JnPMI⁡JmJ_n L J_m = J_n \operatorname{PMI} J_m.

∎

With every cell observed, GloVe sees the whole association matrix, as the distributional-semantics topic’s SVD did: it is that topic’s Definition 3 applied to the doubly centered PMI matrix, which is principal component analysis centered on both sides (formalML, PCA and Low-Rank Approximation). With general weights there is no closed form, and weighted low-rank approximation can have local minima (Srebro and Jaakkola 2003). The opposite limit takes one line.

Proposition 3 (Support blindness).

JJ depends on XX only through the observed triples {(i,j,Xij):(i,j)∈Ω}\{(i, j, X_{ij}) : (i, j) \in \Omega\}. If log⁡Xij=ui+vj\log X_{ij} = u_i + v_j for every observed cell, in particular if every observed count is the same, then (W,C,b,b~)=(0,0,u,v)(W, C, b, \tilde b) = (0, 0, u, v) attains J=0J = 0, the global minimum, whatever Ω\Omega is. Any vectors with ⟨wi,cj⟩=0\langle \mathbf{w}_i, \mathbf{c}_j \rangle = 0 on Ω\Omega are also optimal.

Proof.

J≥0J \ge 0, and substituting gives sij−log⁡Xij=0s_{ij} - \log X_{ij} = 0 on every observed cell.

∎

A sector shows in which nouns keep company with which, the pattern of Ω\Omega, and Proposition 3 says GloVe’s loss reads that pattern only through the values on it. The corpus is close to the proposition’s hypothesis. Of the observed cells, 69.0%69.0\% are seen once and 80.4%80.4\% at most twice. Fitting the biases alone explains 71.9%71.9\% of the weighted variation of log⁡X\log X on Ω\Omega; adding 2525-dimensional vectors, fitted by alternating least squares (below), explains another 27.3%27.3\% (fifty AdaGrad epochs, 25.6%25.6\%), with 178,100178{,}100 parameters, biases included, for 143,345143{,}345 observed cells, and that part carries no sector information:

the same counts, d=25d = 25dev AUCdev nearest neighbortest AUCtest nearest neighbor
GloVe, AdaGrad, W+CW + C0.4800.4800.3300.3300.476\mathbf{0.476}0.2700.270
GloVe, AdaGrad, WW0.4840.4840.3390.3390.4690.4690.3220.322
GloVe, alternating least squares, W+CW + C0.5170.5170.3300.3300.5070.5070.3480.348
negative sampling, 4040 epochs0.8700.8700.8660.8660.886\mathbf{0.886}0.8520.852
truncated SVD of SPPMI⁡5\operatorname{SPPMI}_5, p=0p = 00.9510.9510.9460.9460.973\mathbf{0.973}0.9650.965

The benchmark is the distributional-semantics topic’s (its Definition 4): 115115 test nouns, pair AUC, and nearest-neighbor accuracy, whose chance level is 0.2690.269. GloVe’s test AUC minus one half is −0.024-0.024, with a jackknife standard error of 0.0220.022. It trails negative sampling by 0.4100.410 (standard error 0.0270.027) and the SVD by 0.4970.497 (standard error 0.0210.021).

If the trouble is that the loss cannot see which pairs never occur, two remedies suggest themselves: more text, so that counts stop being mostly ones, or a loss that sees the zeros.

Definition 2 (GloVe with imputed zeros).

Set X~ij=Xij\tilde X_{ij} = X_{ij} on Ω\Omega and X~ij=12\tilde X_{ij} = \tfrac12 elsewhere, and minimize JJ for X~\tilde X over all V2V^2 cells. Every pair never seen counts as seen half a time, with GloVe’s own weight f(12)=0.0188f(\tfrac12) = 0.0188.

The variant is ours and has no parameter to tune; Swivel (Shazeer et al. 2016) is the published model that uses the unobserved cells, with a different loss on them. Its support is full, so with flat weights Theorem 2 describes it exactly. We train it, and GloVe for comparison, by alternating least squares, which handles the V2V^2 imputed cells with the identity behind implicit-feedback recommenders (Hu, Koren, and Volinsky 2008) and never forms a V×VV \times V matrix. The corpus generator, run on, keeps its sector vocabularies, so the benchmark nouns keep their sectors in a longer text:

documentsobserved cells seen onceGloVeGloVe with imputed zerosSVD of SPPMI⁡5\operatorname{SPPMI}_5
3003000.6900.6900.5070.5070.6020.6020.9730.973
1,2001{,}2000.5900.5900.5830.5830.8550.8550.9910.991
3,0003{,}0000.5380.5380.6950.6950.946\mathbf{0.946}0.9860.986

The entries are test AUC. As the text grows, fewer observed pairs are seen once and GloVe’s vectors begin to find the sectors. Counting the unseen pairs finds them far sooner. GloVe’s own regime, billions of tokens, is far beyond either corpus, and there the published comparisons find it competitive with negative sampling, though rarely ahead of it once both are tuned (Levy, Goldberg, and Dagan 2015).


The algorithm

Definition 3 (GloVe's AdaGrad, as run here).

Each epoch visits the observed cells in a fresh random order, in minibatches of 512512. Each cell takes the GloVe C code’s step on 12f(Xij)(sij−log⁡Xij)2\tfrac12 f(X_{ij})(s_{ij} - \log X_{ij})^2: with η=0.05\eta = 0.05, each parameter moves by η\eta times its gradient, summed over the minibatch, divided by the square root of an accumulator that starts at 11; after each step the accumulator adds the square of that undivided step, η\eta times the gradient (AdaGrad; Duchi, Hazan, and Singer 2011). Every vector entry and bias starts uniform on [−12d,12d][-\tfrac{1}{2d}, \tfrac{1}{2d}], and training runs 5050 epochs.

Two departures from the C code are worth stating. The code applies each cell’s update at once, from several threads without locks, while we sum a minibatch’s updates. And its counts weight a pair at distance δ\delta by 1/δ1/\delta, while we use the distributional-semantics topic’s unweighted window counts, so that all three methods factor one matrix.

As a cross-check we minimize J+λ(∥W∥F2+∥C∥F2)J + \lambda\bigl(\lVert W \rVert_F^2 + \lVert C \rVert_F^2\bigr), with a small ridge λ=10−3\lambda = 10^{-3} on the vectors and the biases unpenalized, by alternating least squares. With the contexts fixed, each word’s vector and bias solve a weighted ridge regression in closed form, by its normal equations (formalStatistics, Linear Regression), and then the roles swap. Each half-step is an exact minimization, so the objective never increases. In 4040 sweeps it reaches J=228.3J = 228.3, against AdaGrad’s 693.4693.4 after 5050 epochs (from 13,24713{,}247 after the first); per unit of weight, the mean squared residual is 0.0240.024 against 0.0740.074. Alternating least squares has not converged either: its objective, 347.0347.0 with the penalty’s 118.8118.8 included, is still falling about 0.9%0.9\% per sweep. AdaGrad is the further from a minimum, and its vectors are no better for the benchmark than alternating least squares’ (0.5070.507).


Gauge and analogies

The word2vec topic’s Proposition 3 showed that a loss depending only on WC⊤W C^\top fixes word geometry only up to a change of basis. GloVe’s biases add freedom.

Proposition 4 (GloVe's gauge is affine).

(i) For an invertible AA, vectors t,s∈Rdt, s \in \mathbb{R}^d, and β∈R\beta \in \mathbb{R}, the substitution

wi′=A⊤wi+t,cj′=A−1cj+s,\mathbf{w}'_i = A^\top \mathbf{w}_i + t, \qquad \mathbf{c}'_j = A^{-1} \mathbf{c}_j + s,bi′=bi−⟨A⊤wi,s⟩−⟨t,s⟩+β,b~j′=b~j−⟨A−1cj,t⟩−βb'_i = b_i - \langle A^\top \mathbf{w}_i, s \rangle - \langle t, s \rangle + \beta, \qquad \tilde b'_j = \tilde b_j - \langle A^{-1} \mathbf{c}_j, t \rangle - \beta

leaves every score sijs_{ij}, and so JJ, unchanged.

(ii) Conversely, if two parameter sets give the same scores and, for both sets, the matrices [W,b,1][W, b, \mathbf{1}] and [C,1,b~][C, \mathbf{1}, \tilde b] have full column rank d+2d + 2, they are related by exactly one such substitution.

(iii) Cosines between word vectors are not determined by JJ, and differences wi−wk\mathbf{w}_i - \mathbf{w}_k transform by A⊤A^\top alone.

Proof.

For part (i), ⟨A⊤w,A−1c⟩=⟨w,c⟩\langle A^\top \mathbf{w}, A^{-1}\mathbf{c} \rangle = \langle \mathbf{w}, \mathbf{c} \rangle, and the cross terms ⟨A⊤wi,s⟩+⟨t,A−1cj⟩+⟨t,s⟩\langle A^\top \mathbf{w}_i, s \rangle + \langle t, A^{-1}\mathbf{c}_j \rangle + \langle t, s \rangle are exactly what the bias changes remove. For part (ii), write Wˉ=[W,b,1]\bar W = [W, b, \mathbf{1}] and Cˉ=[C,1,b~]\bar C = [C, \mathbf{1}, \tilde b], so the score matrix is WˉCˉ⊤\bar W \bar C^\top. The word2vec topic’s Proposition 3, applied to these augmented matrices, which needs all four of them to have full column rank d+2d + 2, gives Wˉ′=WˉG\bar W' = \bar W G and Cˉ′=CˉG−⊤\bar C' = \bar C G^{-\top} for a unique invertible GG. The last column of Wˉ\bar W is 1\mathbf{1} on both sides, so Ged+2=ed+2G e_{d+2} = e_{d+2}; the (d+1)(d+1)-th column of Cˉ\bar C is 1\mathbf{1} on both sides, so ed+1⊤G=ed+1⊤e_{d+1}^\top G = e_{d+1}^\top. Such a GG has blocks AA, tt, uu, and a scalar, and reading off WˉG=[WA+1t⊤,Wu+b+β′1,1]\bar W G = [WA + \mathbf{1}t^\top, Wu + b + \beta' \mathbf{1}, \mathbf{1}] gives the substitution of part (i) with u=−Asu = -As. The rank condition on the context side cannot be dropped: with C=C′=0C = C' = 0 and the same biases, every W′W' gives the same scores, including one whose columns leave the span of WW‘s columns and 1\mathbf{1}, which no substitution reaches. Part (iii): translations cancel in differences.

∎

So a GloVe model can be translated as well as rotated and stretched without changing its loss, and cosine similarity changes under both. Which point of this family does training reach? With a penalty on the vectors there is a definite answer.

Proposition 5 (Ridge chooses the balanced, centered factorization).

Let Jλ=J+λ(∥W∥F2+∥C∥F2)J_\lambda = J + \lambda\bigl(\lVert W \rVert_F^2 + \lVert C \rVert_F^2\bigr) with λ>0\lambda \gt 0 and the biases unpenalized. At every stationary point of JλJ_\lambda, W⊤W=C⊤CW^\top W = C^\top C (balanced) and 1⊤W=1⊤C=0\mathbf{1}^\top W = \mathbf{1}^\top C = 0 (centered). For balanced factors, 12(∥W∥F2+∥C∥F2)=∥WC⊤∥∗\tfrac12\bigl(\lVert W \rVert_F^2 + \lVert C \rVert_F^2\bigr) = \lVert W C^\top \rVert_*, the nuclear norm, the least value over all factorizations of the product.

Proof.

Let G=∂J/∂SG = \partial J / \partial S, which is 2f(X)(s−log⁡X)2 f(X)(s - \log X) on observed cells and 00 elsewhere. Stationarity in the biases gives G1=0G \mathbf{1} = 0 and G⊤1=0G^\top \mathbf{1} = 0. Stationarity in the vectors gives GC+2λW=0GC + 2\lambda W = 0 and G⊤W+2λC=0G^\top W + 2\lambda C = 0, so W⊤GC=−2λW⊤WW^\top G C = -2\lambda W^\top W and C⊤G⊤W=−2λC⊤CC^\top G^\top W = -2\lambda C^\top C. The two left sides are transposes of each other and both right sides are symmetric, so W⊤W=C⊤CW^\top W = C^\top C. Multiplying the first condition by 1⊤\mathbf{1}^\top gives (G⊤1)⊤C+2λ1⊤W=2λ1⊤W=0(G^\top \mathbf{1})^\top C + 2\lambda \mathbf{1}^\top W = 2\lambda \mathbf{1}^\top W = 0, and likewise for CC. If W⊤W=C⊤CW^\top W = C^\top C, then W=PSQ⊤W = PSQ^\top and C=P′SQ⊤C = P'SQ^\top share SS and QQ, so WC⊤=PS2P′⊤W C^\top = P S^2 P'^\top has nuclear norm tr⁡S2=12(∥W∥F2+∥C∥F2)\operatorname{tr} S^2 = \tfrac12(\lVert W \rVert_F^2 + \lVert C \rVert_F^2); that this is the least value is the variational form of the nuclear norm (Srebro, Rennie, and Jaakkola 2005; formalML, Convex Analysis).

∎

When WC⊤=UΣV⊤W C^\top = U \Sigma V^\top has rank dd and distinct singular values, the word2vec topic’s Lemma 1 makes a balanced factorization W=UΣ1/2RW = U \Sigma^{1/2} R: the distributional-semantics topic’s square-root weighting of the fitted matrix, centered. A penalty chooses the geometry that the loss leaves free (formalStatistics, Regularization and Penalized Estimation). The proposition is about stationary points, and training does not reach them in the gauge directions: after 4040 sweeps of alternating least squares with λ=10−3\lambda = 10^{-3}, the imbalance ∥W⊤W−C⊤C∥/∥W⊤W∥\lVert W^\top W - C^\top C \rVert / \lVert W^\top W \rVert is 0.9990.999, because the first sweep fits WW through the small initial CC and so small a λ\lambda has barely pulled them together. Unpenalized AdaGrad, which the proposition does not cover, ends at 0.0930.093. Neither trained model sits in the ridge’s gauge.

Now the analogies. The standard test is vector arithmetic.

Definition 4 (Analogy by vector offset).

For a question a : b :: c : ?, 3CosAdd answers with the word x∉{a,b,c}x \notin \{a, b, c\} that maximizes cos⁡(x^,b^−a^+c^)\cos\bigl(\hat x, \hat b - \hat a + \hat c\bigr), where hats denote unit vectors.

This is Mikolov, Yih, and Zweig’s (2013) test. The exclusion matters: without it, the word nearest to b^−a^+c^\hat b - \hat a + \hat c is often bb itself (Linzen 2016), and Levy and Goldberg (2014) showed that 3CosAdd is a sum of similarities, not a test of offsets as such. What would it mean for an offset to be exact?

Theorem 3 (Exact parallelograms are facts about count ratios).

Let a,b,c,da, b, c, d be words and KK a set of contexts on which all four rows are observed and fitted exactly, sij=log⁡Xijs_{ij} = \log X_{ij}, and suppose the matrix [CK,1][C_K, \mathbf{1}] of the contexts’ vectors and a column of ones has full column rank d+1d + 1. Then

wb−wa=wd−wc  ⟺  PMI⁡bj−PMI⁡aj−PMI⁡dj+PMI⁡cj is constant over j∈K  ⟺  P(j∣b)P(j∣a)∝P(j∣d)P(j∣c) on K.\mathbf{w}_b - \mathbf{w}_a = \mathbf{w}_d - \mathbf{w}_c \iff \operatorname{PMI}_{bj} - \operatorname{PMI}_{aj} - \operatorname{PMI}_{dj} + \operatorname{PMI}_{cj} \text{ is constant over } j \in K \iff \frac{P(j \mid b)}{P(j \mid a)} \propto \frac{P(j \mid d)}{P(j \mid c)} \text{ on } K .

The condition is unchanged by every substitution of Proposition 4. For negative sampling at its exact optimum, with scores PMI⁡α−log⁡k\operatorname{PMI}_\alpha - \log k and no biases, the same holds with “constant” replaced by “zero”, given CKC_K of full column rank dd.

Proof.

Let v=wb−wa−wd+wcv = \mathbf{w}_b - \mathbf{w}_a - \mathbf{w}_d + \mathbf{w}_c and κ=bb−ba−bd+bc\kappa = b_b - b_a - b_d + b_c. The exact fit gives ⟨v,cj⟩+κ=log⁡Xbj−log⁡Xaj−log⁡Xdj+log⁡Xcj\langle v, \mathbf{c}_j \rangle + \kappa = \log X_{bj} - \log X_{aj} - \log X_{dj} + \log X_{cj} for j∈Kj \in K. Since log⁡Xij=PMI⁡ij+log⁡Xi⋅+log⁡X⋅j−log⁡N\log X_{ij} = \operatorname{PMI}_{ij} + \log X_{i\cdot} + \log X_{\cdot j} - \log N and the coefficients 1,−1,−1,11, -1, -1, 1 sum to zero, the column terms cancel, and the right side is Δj+κ′\Delta_j + \kappa', with Δj\Delta_j the PMI combination and κ′\kappa' a constant. If v=0v = 0, then Δj=κ−κ′\Delta_j = \kappa - \kappa' for every jj. Conversely, if Δj=δ0\Delta_j = \delta_0 on KK, then ⟨v,cj⟩+(κ−κ′−δ0)=0\langle v, \mathbf{c}_j \rangle + (\kappa - \kappa' - \delta_0) = 0 for every j∈Kj \in K, which says [CK,1][C_K, \mathbf{1}] annihilates a vector whose first dd entries are vv, so v=0v = 0. The ratio form is eΔj=P(j∣b)P(j∣c)/(P(j∣a)P(j∣d))e^{\Delta_j} = P(j \mid b) P(j \mid c) / \bigl(P(j \mid a) P(j \mid d)\bigr). A substitution of Proposition 4 maps vv to A⊤vA^\top v, again because the coefficients sum to zero. Without biases the same computation gives ⟨v,cj⟩=Δj\langle v, \mathbf{c}_j \rangle = \Delta_j exactly.

∎

Pennington and colleagues motivated GloVe by the conjecture that relations are ratios. Allen and Hospedales (2019) prove a general version with explicit error terms, by multiplying an identity between PMI rows by a pseudo-inverse of the context matrix, and Theorem 3 is its exact, error-free case adapted to GloVe’s biases. Ethayarajh, Duvenaud, and Hirst (2019) give a criterion through inner products between word vectors, which depend on the basis; stating it through the contexts, as here, is what makes it hold in every factorization. The answer to an analogy question is another matter.

Corollary 2 (Vector arithmetic depends on the factorization).

3CosAdd’s answers are unchanged if every word vector is scaled by its own positive factor and all are rotated by one orthogonal matrix. They can change under a general change of basis AA and under a translation, both of which leave JJ and every exact parallelogram of Theorem 3 unchanged.

Proof.

Normalizing to unit length undoes the scaling, and a rotation preserves every inner product. A general AA changes the angles between word vectors, and a translation changes their directions, so either can move the maximizer of the cosine; Proposition 4 and Theorem 3 give the invariances of JJ and of the parallelograms.

∎

Theorem 3 also gives an analogy method that reads the scores alone. Answer a : b :: c : ? with the candidate d′d', searched among the relation’s tails (its regions or sectors) other than bb, whose score row brings the four rows closest to a parallelogram, the one that minimizes the spread over all contexts of sbj−saj−sd′j+scjs_{bj} - s_{aj} - s_{d'j} + s_{cj}. Call this the gauge-free solver; it chooses among the tails only, so its accuracy compares with 3CosAdd among the tails, not over the whole vocabulary. Call that spread, divided by the spread of sbj−sajs_{bj} - s_{aj}, the defect of the quadruple: zero exactly for an exact parallelogram. Every factorization of the same scores gives the solver the same answers. Here are the two trained models, each refactored without changing a score, on the knowledge base’s city-to-region questions (the finance section below introduces them):

modelfactorizationtest AUC3CosAdd, whole vocabulary3CosAdd, among regionsgauge-free solver
negative samplingas trained0.8860.8860.6330.6330.7070.7070.6930.693
negative samplingUΣ0U\Sigma^00.8490.8490.6200.6200.6870.6870.6930.693
negative samplingUΣ1/2U\Sigma^{1/2} (balanced)0.9010.9010.6200.6200.7070.7070.6930.693
negative samplingUΣU\Sigma0.8900.8900.5730.5730.7070.7070.6930.693
negative samplingrandom AA, condition 1001000.7370.7370.253\mathbf{0.253}0.4400.4400.6930.693
GloVeas trained0.4760.4760.3670.3670.5800.5800.3600.360
GloVebalanced0.4760.4760.3600.3600.5800.5800.3600.360
GloVecentered0.4850.4850.3730.3730.5730.5730.3600.360
GloVetranslated0.4660.4660.3800.3800.5800.5800.3600.360
GloVerandom AA, condition 1001000.4700.4700.2130.2130.5000.5000.3600.360

Every row of a model has the same scores to within 1.3×10−141.3 \times 10^{-14} relative, the same defect, and the same gauge-free answers. The negative-sampling rows are the word2vec topic’s five factorizations, with the same random basis change. The rows for negative sampling have no translation because, without biases, translating its vectors would change its scores. A change of basis that the training loss cannot detect costs negative sampling’s vector arithmetic more than half its successes over the whole vocabulary (0.6330.633 to 0.2530.253), and GloVe’s about two-fifths (0.3670.367 to 0.2130.213).


Weights, offsets, and more text

The laboratory puts the topic’s three objects side by side. Panel A shows one cell: pick one of revenue’s contexts and see its loss under GloVe, the Poisson likelihood, and negative sampling, each against the distance from its own optimum. The curves’ widths are the three weights of Corollary 1. Change GloVe’s cap and exponent and watch its weight on the pair seen once, and its share of weight on rare pairs, move. Pick outlook, never seen with revenue, and GloVe’s curve is flat: it has no term, so its trained score of 3.5583.558 is free. Panel B has two views. The first draws a relation’s words in their own leading plane, with an arrow from each city to its region or each company to its sector, under each factorization of one set of scores. A random basis change scrambles the arrows and the answers, while the gauge-free solver and the defect stay fixed. The second view is the scale experiment.

A · Three weights, one count — the loss of one (word, context) cell under GloVe, Poisson, and SGNS
0123-3-2-10123loss above its minimum · revenue · plantline, seen 1 timesδ = distance of the score from that objective’s optimum
GloVe f(a)(s − log a)²Poisson a(e^δ − 1 − δ)SGNS −[a log σ + b log σ(−·)]· dot: trained GloVe · square: trained SGNS
times seen, a
1
GloVe weight f(a)
0.0316
Poisson weight a
1
SGNS weight ab/(a + b), b = 0.52
0.340
trained GloVe score s (log a)
0.069 (0.000)
trained SGNS score (x*)
-0.913 (0.662)
Each weight’s total, split by how often the pair was seen (all 143,345 observed cells at the eight-word window)
share of cellsseen ≤ 4: 0.885
GloVe f(X)seen ≤ 4: 0.523
Poisson Xseen ≤ 4: 0.296
SGNS ab/(a + b), k = 5, α = 0.75seen ≤ 4: 0.301
segments, left to right — times seen:12–45–1920–99100+

Curvature is weight: each curve’s width at its minimum is set by that objective’s weight on this cell. GloVe caps its weight at 1 and tempers it below x_max, so it spends most of its weight on pairs seen once or a few times, whose log counts are 0 or close to it. For a context never seen with revenue, Poisson and SGNS keep pushing the score down; GloVe has no term at all, and its score is free.


B · What the vectors know — relations as offsets, and what more text buys
bostonmassachusettscalgarycanadachicagoillinoiscolumbusohiodallastexasdenvercoloradodublinirelandfrankfurtgermanyhoustonmunichosakajapantorontozurichswitzerland
circles: heads · squares: tails · arrows: head → tail · the question’s words are bold
houston : texas :: boston : ? 3CosAdd's top three — massachusetts ✓ · dallas · switzerland (expected massachusetts)
3CosAdd, whole vocabulary (city → region)
0.367
3CosAdd, among the tails
0.580
b itself nearest, before exclusion
0.040
gauge-free solver (any variant)
0.360
median parallelogram defect
1.262

Every variant of a method has the same scores, so the same loss: the variants differ only by a change of basis (and, for GloVe, a translation its biases absorb). 3CosAdd reads cosines between word vectors, so its answers move. The gauge-free solver picks the tail whose score rows come closest to a parallelogram, and the defect measures how far they are; both read the scores alone, so neither moves. No model is near an exact parallelogram (defect near 1), yet the scores still point to the right tail far more often than chance.


Finance case study: a filings knowledge base as offsets


Where this goes next

  • Contextual embeddings. One vector per word type is the common premise of the last three topics. Contextual embeddings give each occurrence its own vector, trained by a masked-language-model objective.
  • Knowledge-graph embeddings. TransE and its successors make relations offsets by design, and train with negative sampling over corrupted triples. The filings knowledge base here becomes their training data.
  • Retrieval geometry. formalRAG’s hypersphere and von Mises–Fisher geometry develops the cosine geometry these comparisons use, and its dual encoders share the gauge freedom of Proposition 4.

Honest caveats


Implementation

The companion notebook (notebookPath) imports glove_embedding_geometry.py, which owns every number on this page. It imports the word2vec topic’s module for the trained negative-sampling model, its basis changes, and its benchmark statistics, and the distributional-semantics topic’s module for the counts, the PMI matrices, the SVD baselines, and the planted-sector benchmark. Running it as a script trains GloVe by AdaGrad and by alternating least squares in NumPy and executes a harness of 1919 assertion-based tests in under a minute on a laptop CPU. The tests cover:

  • Propositions 1 and 2 on random tables; the hand-written gradient against complex-step and central differences.
  • Theorem 1 against scipy.stats.poisson, with its sandwich and curvature; Corollary 1’s frozen biases and weight shares, including the word2vec topic’s 54.8%54.8\%.
  • Theorem 2 against L-BFGS (scipy.optimize) and alternating least squares on random tables; Proposition 3 on random supports and on the corpus; the benchmark, the scale experiment, and Definition 2’s identity.
  • AdaGrad’s and alternating least squares’ training curves; Propositions 4 and 5, Theorem 3, and Corollary 2 on random instances; the gauge table and the finance case study on the corpus.

The laboratory reads its data from a JSON file that one of those tests requires to equal the module’s viz_payload() exactly, so the figure cannot drift from the code.

References & Further Reading