Algorithm Search⌘K

Theoremadvanced

SGNS as implicit matrix factorization

Also called Levy-Goldberg theorem.

If every dot product w . c is free, the best skip-gram with k negative samples has w . c = PMI(w, c) - log k, so it factorizes a shifted PMI matrix.

Statement

Let DD be a multiset of word and context pairs (w,c)(w, c), for example the pairs that a skip-gram model reads from a corpus with a window. Write #(w,c)\#(w, c) for the number of copies of (w,c)(w, c) in DD, #(w)=∑c#(w,c)\#(w) = \sum_c \#(w, c), #(c)=∑w#(w,c)\#(c) = \sum_w \#(w, c) and ∣D∣=∑w,c#(w,c)|D| = \sum_{w, c} \#(w, c). All logarithms are natural. The pointwise mutual information of a pair is

PMI⁡(w,c)=log⁡#(w,c) ∣D∣#(w) #(c).\operatorname{PMI}(w, c) = \log \frac{\#(w, c)\,|D|}{\#(w)\,\#(c)}.

Each word ww has a vector w⃗∈Rd\vec w \in \mathbb{R}^d and each context cc a vector c⃗∈Rd\vec c \in \mathbb{R}^d. Stack them as the rows of WW and CC. Skip-gram with negative sampling (SGNS) with kk noise samples per pair and the noise distribution PD(c)=#(c)/∣D∣P_D(c) = \#(c)/|D| maximizes the expected objective

ℓ(W,C)=∑w,c#(w,c)(log⁡σ(w⃗⋅c⃗)+k EcN∼PDlog⁡σ(−w⃗⋅c⃗N)),\ell(W, C) = \sum_{w, c} \#(w, c)\Big(\log \sigma(\vec w \cdot \vec c) + k\,\mathbb{E}_{c_N \sim P_D} \log \sigma(-\vec w \cdot \vec c_N)\Big),

where σ(x)=1/(1+e−x)\sigma(x) = 1/(1 + e^{-x}). Collect the terms of each cell xwc=w⃗⋅c⃗x_{wc} = \vec w \cdot \vec c:

ℓ=∑w,cgwc(xwc),gwc(x)=#(w,c)log⁡σ(x)+k #(w) #(c)∣D∣log⁡σ(−x).\ell = \sum_{w, c} g_{wc}(x_{wc}), \qquad g_{wc}(x) = \#(w, c) \log \sigma(x) + k\,\frac{\#(w)\,\#(c)}{|D|} \log \sigma(-x).

Theorem (Levy and Goldberg 2014, Section 3.1). Let VWV_W and VCV_C be the words and the contexts with #(w)>0\#(w) > 0 and #(c)>0\#(c) > 0, and suppose that #(w,c)>0\#(w, c) > 0 for every w∈VWw \in V_W and c∈VCc \in V_C. Let MM be the ∣VW∣×∣VC∣|V_W| \times |V_C| matrix of the shifted PMI, Mwc=PMI⁡(w,c)−log⁡kM_{wc} = \operatorname{PMI}(w, c) - \log k.

  1. Over all real matrices XX, the function ∑w,cgwc(Xwc)\sum_{w, c} g_{wc}(X_{wc}) has exactly one maximizer, X=MX = M.
  2. If d≥rank⁡Md \ge \operatorname{rank} M, the global maximizers of ℓ(W,C)\ell(W, C) are exactly the pairs (W,C)(W, C) with WC⊤=MW C^\top = M. That is, at a global maximum, w⃗⋅c⃗=PMI⁡(w,c)−log⁡k\vec w \cdot \vec c = \operatorname{PMI}(w, c) - \log k for every pair.

The hypothesis d≥rank⁡Md \ge \operatorname{rank} M is the precise form of the condition in the paper, that each product w⃗⋅c⃗\vec w \cdot \vec c "can assume a value independently of the others". It always holds for d≥min⁡(∣VW∣,∣VC∣)d \ge \min(|V_W|, |V_C|). Real embeddings have dd of a few hundred and ∣V∣|V| of many thousands, so they do not meet it, and their dot products only approximate the shifted PMI.

In plain words

Skip-gram with negative sampling learns a vector for each word and each context. Training pushes the score of every real pair up and the score of random pairs down. If the vectors are long enough that each score can be set on its own, the best score of a pair is a known number: how much more often the two appear together than chance predicts, on a log scale, minus the log of the number of random pairs. So the method fills a table of these numbers and writes it as a product of two thin matrices. This links neural word vectors to older methods that build the same table from counts. With short vectors the table cannot be matched exactly, and the method finds a compromise that cares most about frequent pairs.

Proof

Step 1: the objective splits into cells. The expectation over the noise is EcN∼PDlog⁡σ(−w⃗⋅c⃗N)=∑c′#(c′)∣D∣log⁡σ(−w⃗⋅c⃗′)\mathbb{E}_{c_N \sim P_D} \log \sigma(-\vec w \cdot \vec c_N) = \sum_{c'} \frac{\#(c')}{|D|} \log \sigma(-\vec w \cdot \vec c'). It does not depend on the observed context cc, so its total weight in ℓ\ell is ∑c#(w,c)=#(w)\sum_c \#(w, c) = \#(w). Hence

ℓ=∑w,c#(w,c)log⁡σ(w⃗⋅c⃗)+∑w#(w)∑c′k #(c′)∣D∣log⁡σ(−w⃗⋅c⃗′),\ell = \sum_{w, c} \#(w, c) \log \sigma(\vec w \cdot \vec c) + \sum_{w} \#(w) \sum_{c'} k\,\frac{\#(c')}{|D|} \log \sigma(-\vec w \cdot \vec c'),

and renaming c′c' as cc gives ℓ=∑w,cgwc(xwc)\ell = \sum_{w, c} g_{wc}(x_{wc}).

Step 2: one cell. Fix ww and cc and write a=#(w,c)a = \#(w, c) and b=k #(w)#(c)/∣D∣b = k\,\#(w)\#(c)/|D|, both positive by the hypothesis. Use ddxlog⁡σ(x)=σ(−x)\frac{d}{dx} \log \sigma(x) = \sigma(-x) and ddxlog⁡σ(−x)=−σ(x)\frac{d}{dx} \log \sigma(-x) = -\sigma(x). Then

g′(x)=a σ(−x)−b σ(x),g′′(x)=−(a+b) σ(x) σ(−x)<0.g'(x) = a\,\sigma(-x) - b\,\sigma(x), \qquad g''(x) = -(a + b)\,\sigma(x)\,\sigma(-x) < 0.

So gg is strictly concave and has at most one stationary point. The equation g′(x)=0g'(x) = 0 reads a σ(−x)=b σ(x)a\,\sigma(-x) = b\,\sigma(x). Since σ(x)/σ(−x)=ex\sigma(x)/\sigma(-x) = e^x, it is ex=a/be^x = a/b, with the one solution

x∗=log⁡ab=log⁡#(w,c) ∣D∣#(w) #(c)−log⁡k=PMI⁡(w,c)−log⁡k.x^* = \log \frac{a}{b} = \log \frac{\#(w, c)\,|D|}{\#(w)\,\#(c)} - \log k = \operatorname{PMI}(w, c) - \log k.

A strictly concave function with a stationary point has its unique maximum there.

Step 3: part 1. The sum ∑gwc(Xwc)\sum g_{wc}(X_{wc}) has a separate variable in each term. It is at most ∑gwc(Mwc)\sum g_{wc}(M_{wc}), and by Step 2 equality holds if and only if Xwc=MwcX_{wc} = M_{wc} for every cell.

Step 4: part 2. For any (W,C)(W, C), ℓ(W,C)=∑gwc((WC⊤)wc)≤∑gwc(Mwc)\ell(W, C) = \sum g_{wc}((W C^\top)_{wc}) \le \sum g_{wc}(M_{wc}), with equality if and only if WC⊤=MW C^\top = M, by part 1. It remains to show that the bound is reached. Let r=rank⁡M≤dr = \operatorname{rank} M \le d, and take the singular value decomposition M=UΣV⊤M = U \Sigma V^\top with the rr nonzero singular values. Put W=UrΣr1/2W = U_r \Sigma_r^{1/2} and C=VrΣr1/2C = V_r \Sigma_r^{1/2}, padded with d−rd - r zero columns. Then WC⊤=MW C^\top = M, so the bound is the maximum and the maximizers are exactly the factorizations of MM. ■\blacksquare

Worked example. Take two words and two contexts with #(w1,c1)=#(w2,c2)=4\#(w_1, c_1) = \#(w_2, c_2) = 4 and #(w1,c2)=#(w2,c1)=1\#(w_1, c_2) = \#(w_2, c_1) = 1. Then ∣D∣=10|D| = 10 and every marginal count is 5. The PMI is log⁡(4⋅10/25)=log⁡1.6=0.4700\log(4 \cdot 10 / 25) = \log 1.6 = 0.4700 on the diagonal and log⁡(1⋅10/25)=log⁡0.4=−0.9163\log(1 \cdot 10/25) = \log 0.4 = -0.9163 off it. With k=2k = 2 the shift is log⁡2=0.6931\log 2 = 0.6931, so

M=(log⁡0.8log⁡0.2log⁡0.2log⁡0.8)=(−0.2231−1.6094−1.6094−0.2231).M = \begin{pmatrix} \log 0.8 & \log 0.2 \\ \log 0.2 & \log 0.8 \end{pmatrix} = \begin{pmatrix} -0.2231 & -1.6094 \\ -1.6094 & -0.2231 \end{pmatrix}.

For the cell (w1,c1)(w_1, c_1), a=4a = 4 and b=2⋅5⋅5/10=5b = 2 \cdot 5 \cdot 5 / 10 = 5, so x∗=log⁡0.8x^* = \log 0.8, as in the matrix. The model then gives the true pair the probability σ(x∗)=a/(a+b)=4/9\sigma(x^*) = a/(a + b) = 4/9 of being real. That is the share of real copies among the real and the noise copies of the pair. Here rank⁡M=2\operatorname{rank} M = 2, so d=2d = 2 is enough, for example W=MW = M and C=IC = I.

Consequences

  • SGNS and count methods fit the same matrix. Under the hypothesis, SGNS and a factorization of the shifted PMI matrix have the same optimum. Levy and Goldberg use this to propose the shifted positive PMI, max⁡(PMI⁡(w,c)−log⁡k,0)\max(\operatorname{PMI}(w, c) - \log k, 0), as a sparse count-based alternative, factored by a truncated SVD as in latent semantic analysis. See also positive pointwise mutual information.
  • The smoothed noise distribution. word2vec draws noise from the unigram distribution to the power 3/4, not from PDP_D (footnote 1 of the paper). The same proof with Pn(c)P_n(c) in place of #(c)/∣D∣\#(c)/|D| gives x∗=log⁡#(w,c)#(w) Pn(c)−log⁡kx^* = \log \frac{\#(w, c)}{\#(w)\,P_n(c)} - \log k. Since PnP_n gives more mass to rare contexts than PDP_D, rare contexts get a lower score. Levy, Goldberg and Dagan 2015 call this context distribution smoothing and apply it to PMI.
  • NCE. The same cell analysis for noise-contrastive estimation gives w⃗⋅c⃗=log⁡P(w∣c)−log⁡k\vec w \cdot \vec c = \log P(w \mid c) - \log k (equation 8 of the paper): NCE fits a shifted log conditional probability, not a PMI.
  • Unseen pairs. If #(w,c)=0\#(w, c) = 0 while #(w)#(c)>0\#(w)\#(c) > 0, then a=0a = 0 and gwc(x)=blog⁡σ(−x)g_{wc}(x) = b \log \sigma(-x) increases as x→−∞x \to -\infty. The supremum is not reached, which matches PMI⁡=−∞\operatorname{PMI} = -\infty. The real matrix of a corpus has many such cells, so the exact optimum does not exist there, and the low dimension and the training keep the scores finite.
  • Low dimension. With d<rank⁡Md < \operatorname{rank} M, SGNS maximizes ∑gwc((WC⊤)wc)\sum g_{wc}((W C^\top)_{wc}) over matrices of rank at most dd. Near the optimum gwcg_{wc} has the curvature (a+b)σ(x∗)σ(−x∗)(a + b)\sigma(x^*)\sigma(-x^*), which grows with the counts, so the fit is a weighted factorization that pays more attention to frequent pairs (Section 3.2 of the paper). GloVe makes a weighted least squares fit of the log counts explicit.

Tightness

Each hypothesis is needed.

  • Dimension. In the worked example MM has rank 2, since det⁡M=0.0498−2.5903≠0\det M = 0.0498 - 2.5903 \ne 0. With d=1d = 1 every WC⊤W C^\top has rank at most 1, so no pair (W,C)(W, C) reaches X=MX = M, and the global maximum of ℓ\ell has some cell with w⃗⋅c⃗≠PMI⁡(w,c)−log⁡k\vec w \cdot \vec c \ne \operatorname{PMI}(w, c) - \log k.
  • Global maximum, not any stationary point. The point W=0W = 0, C=0C = 0 is stationary: the gradient of ℓ\ell with respect to w⃗\vec w is ∑cgwc′(xwc) c⃗\sum_c g_{wc}'(x_{wc})\,\vec c, which is zero when every c⃗=0\vec c = 0, and the same holds for c⃗\vec c. There every product is 0, not the shifted PMI. The objective is not concave in (W,C)(W, C), so the theorem speaks of its global maxima.
  • Positive counts. Without #(w,c)>0\#(w, c) > 0 the maximum does not exist, as shown under Consequences.
  • The expected objective. The theorem is about the expectation over the noise samples. Stochastic training with a finite number of epochs, a decaying learning rate, subsampling of frequent words and a dynamic window reaches an approximation, and the subsampling and the window change the effective counts #(w,c)\#(w, c).

History

Mikolov, Sutskever, Chen, Corrado and Dean introduced negative sampling in 2013 as a simplification of NCE for the skip-gram model. Goldberg and Levy wrote out its objective as a sum over pairs in an arXiv note in early 2014. Levy and Goldberg then showed at NIPS 2014 that its optimum with free dot products is the shifted PMI matrix. This tied the neural method to the PMI matrices of distributional semantics. Levy, Goldberg and Dagan 2015 carried the tuning choices of word2vec, such as the smoothed context distribution, over to the count methods and found the gaps between the two families small in their experiments.

References

  1. Levy and Goldberg 2014, Section 2 (the SGNS objective, equations 1 and 2), Section 3.1 (the cell objective, equation 5, and its optimum, equations 6 and 7; the paper has no numbered theorem), equation 8 (the same analysis for NCE), Section 3.2 (weighted factorization when d is small), footnote 1 (the smoothed noise distribution)
  2. Mikolov, Sutskever, Chen, Corrado and Dean 2013, the negative sampling objective and the unigram noise distribution to the power 3/4
  3. Goldberg and Levy 2014, the derivation of the negative sampling objective as a sum over word and context pairs
  4. Levy, Goldberg and Dagan 2015, the smoothed context distribution and the comparison of SGNS with PPMI and SVD

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW0 stub
  • the lead paragraph