Riemannian Optimization for Skip-Gram Negative Sampling

Skip-Gram Negative Sampling (SGNS) word embedding model, well known by its implementation in “word2vec” software, is usually optimized by stochastic gradient descent. However, the optimization of SGNS objective can be viewed as a problem of searching for a good matrix with the low-rank constraint. The most standard way to solve this type of problems is to apply Riemannian optimization framework to optimize the SGNS objective over the manifold of required low-rank matrices. In this paper, we propose an algorithm that optimizes SGNS objective using Riemannian optimization and demonstrates its superiority over popular competitors, such as the original method to train SGNS and SVD over SPPMI matrix.


Introduction
In this paper, we consider the problem of embedding words into a low-dimensional space in order to measure the semantic similarity between them. As an example, how to find whether the word "table" is semantically more similar to the word "stool" than to the word "sky"? That is achieved by constructing a low-dimensional vector representation for each word and measuring similarity between the words as the similarity between the corresponding vectors.
One of the most popular word embedding models (Mikolov et al., 2013) is a discriminative neural network that optimizes Skip-Gram Negative Sampling (SGNS) objective (see Equation 3). It aims at predicting whether two words can be found close to each other within a text. As shown in Section 2, the process of word embeddings training * The first two authors contributed equally to this work using SGNS can be divided into two general steps with clear objectives: Step 1. Search for a low-rank matrix X that provides a good SGNS objective value; Step 2. Search for a good low-rank representation X = W C in terms of linguistic metrics, where W is a matrix of word embeddings and C is a matrix of so-called context embeddings.
Unfortunately, most previous approaches mixed these two steps into a single one, what entails a not completely correct formulation of the optimization problem. For example, popular approaches to train embeddings (including the original "word2vec" implementation) do not take into account that the objective from Step 1 depends only on the product X = W C : instead of straightforward computing of the derivative w.r.t. X, these methods are explicitly based on the derivatives w.r.t. W and C, what complicates the optimization procedure. Moreover, such approaches do not take into account that parametrization W C of matrix X is non-unique and Step 2 is required. Indeed, for any invertible matrix S, we have therefore, solutions W 1 C 1 and W 2 C 2 are equally good in terms of the SGNS objective but entail different cosine similarities between embeddings and, as a result, different performance in terms of linguistic metrics (see Section 4.2 for details).
A successful attempt to follow the above described steps, which outperforms the original SGNS optimization approach in terms of various linguistic tasks, was proposed in . In order to obtain a low-rank matrix X on Step 1, the method reduces the dimensionality of Shifted Positive Pointwise Mutual Informa-tion (SPPMI) matrix via Singular Value Decomposition (SVD). On Step 2, it computes embeddings W and C via a simple formula that depends on the factors obtained by SVD. However, this method has one important limitation: SVD provides a solution to a surrogate optimization problem, which has no direct relation to the SGNS objective. In fact, SVD minimizes the Mean Squared Error (MSE) between X and SPPMI matrix, what does not lead to minimization of SGNS objective in general (see Section 6.1 and Section 4.2 in  for details).
These issues bring us to the main idea of our paper: while keeping the low-rank matrix search setup on Step 1, optimize the original SGNS objective directly. This leads to an optimization problem over matrix X with the lowrank constraint, which is often (Mishra et al., 2014) solved by applying Riemannian optimization framework (Udriste, 1994). In our paper, we use the projector-splitting algorithm (Lubich and Oseledets, 2014), which is easy to implement and has low computational complexity. Of course, Step 2 may be improved as well, but we regard this as a direction of future work.
As a result, our approach achieves the significant improvement in terms of SGNS optimization on Step 1 and, moreover, the improvement on Step 1 entails the improvement on Step 2 in terms of linguistic metrics. That is why, the proposed two-step decomposition of the problem makes sense, what, most importantly, opens the way to applying even more advanced approaches based on it (e.g., more advanced Riemannian optimization techniques for Step 1 or a more sophisticated treatment of Step 2).
To summarize, the main contributions of our paper are: • We reformulated the problem of SGNS word embedding learning as a two-step procedure with clear objectives; • For Step 1, we developed an algorithm based on Riemannian optimization framework that optimizes SGNS objective over low-rank matrix X directly; • Our algorithm outperforms state-of-the-art competitors in terms of SGNS objective and the semantic similarity linguistic metric Mikolov et al., 2013;. In this paper, we consider the Skip-Gram Negative Sampling (SGNS) word embedding model (Mikolov et al., 2013), which is a probabilistic discriminative model. Assume we have a text corpus given as a sequence of words w 1 , . . . , w n , where n may be larger than 10 12 and w i ∈ V W belongs to a vocabulary of words V W . A context c ∈ V C of the word w i is a word from set {w i−L , ..., w i−1 , w i+1 , ..., w i+L } for some fixed window size L. Let w, c ∈ R d be the word embeddings of word w and context c, respectively. Assume they are specified by the following mappings: The ultimate goal of SGNS word embedding training is to fit good mappings W and C. Let D be a multiset of all word-context pairs observed in the corpus. In the SGNS model, the probability that word-context pair (w, c) is observed in the corpus is modeled as a following dsitribution: where #(w, c) is the number of times the pair (w, c) appears in D and x, y is the scalar product of vectors x and y. Number d is a hyperparameter that adjusts the flexibility of the model. It usually takes values from tens to hundreds. In order to collect a training set, we take all pairs (w, c) from D as positive examples and k randomly generated pairs (w, c) as negative ones. The number of times the word w and the context c appear in D can be computed as accordingly. Then negative examples are generated from the distribution defined by #(c) counters: In this way, we have a model maximizing the following logarithmic likelihood objective for all word-context pairs (w, c): In order to maximize the objective over all observations for each pair (w, c), we arrive at the following SGNS optimization problem over all possible mappings W and C: Usually, this optimization is done via the stochastic gradient descent procedure that is performed during passing through the corpus (Mikolov et al., 2013;Rong, 2014).

Optimization over Low-Rank Matrices
Relying on the prospect proposed in , let us show that the optimization problem given by (3) can be considered as a problem of searching for a matrix that maximizes a certain objective function and has the rank-d constraint (Step 1 in the scheme described in Section 1).

SGNS Loss Function
As shown in , the logarithmic likelihood (3) can be represented as the sum of l w,c (w, c) over all pairs (w, c), where l w,c (w, c) has the following form: A crucial observation is that this loss function depends only on the scalar product w, c but not on embeddings w and c separately: and x w,c is the scalar product w, c , and a w,c = #(w, c), b w,c = k #(w)#(c) |D| are constants.

Matrix Notation
Denote |V W | as n and |V C | as m. Let W ∈ R n×d and C ∈ R m×d be matrices, where each row w ∈ R d of matrix W is the word embedding of the corresponding word w and each row c ∈ R d of matrix C is the context embedding of the corresponding context c. Then the elements of the product of these matrices X = W C are the scalar products x w,c of all pairs (w, c): Note that this matrix has rank d, because X equals to the product of two matrices with sizes (n × d) and (d × m). Now we can write SGNS objective given by (3) as a function of X: (5) This arrives us at the following proposition: Proposition 1 SGNS optimization problem given by (3) can be rewritten in the following constrained form: where M d is the manifold (Udriste, 1994) of all matrices in R n×m with rank d: The key idea of this paper is to solve the optimization problem given by (6) via the framework of Riemannian optimization, which we introduce in Section 3. Important to note that this prospect does not suppose the optimization over parameters W and C directly. This entails the optimization in the space with ((n + m − d) · d) degrees of freedom (Mukherjee et al., 2015) instead of ((n + m) · d), what simplifies the optimization process (see Section 5 for the experimental results).

Computing Embeddings from a Low-Rank Solution
Once X is found, we need to recover W and C such that X = W C (Step 2 in the scheme described in Section 1). This problem does not have a unique solution, since if (W, C) satisfy this equation, then W S −1 and CS satisfy it as well for any non-singular matrix S. Moreover, different solutions may achieve different values of the linguistic metrics (see Section 4.2 for details). While our paper focuses on Step 1, we use, for Step 2, a heuristic approach that was proposed in (Levy et al., 2015) and it shows good results in practice.
We compute SVD of X in the form where U and V have orthonormal columns, and Σ is the diagonal matrix, and use as matrices of embeddings.
A simple justification of this solution is the following: we need to map words into vectors in a way that similar words would have similar embeddings in terms of cosine similarities: It is reasonable to assume that two words are similar, if they share contexts. Therefore, we can estimate the similarity of two words w 1 , w 2 as what is the element of the matrix XX with indices (w 1 , w 2 ). Note that If we choose W = U Σ, we exactly obtain w 1 , w 2 = s(w 1 , w 2 ), since W W = XX in this case. That is, the cosine similarity of the embeddings w 1 , w 2 coincides with the intuitive similarity s(w 1 , w 2 ). However, scaling by √ Σ instead of Σ was shown in (Levy et al., 2015) to be a better solution in experiments. The main idea of Riemannian optimization (Udriste, 1994) is to consider (6) as a constrained optimization problem. Assume we have an approximated solution X i on a current step of the optimization process, where i is the step number. In order to improve X i , the next step of the standard gradient ascent outputs the point where ∇F (X i ) is the gradient of objective F at the point X i . Note that the gradient ∇F (X i ) can be naturally considered as a matrix in R n×m . Point X i + ∇F (X i ) leaves the manifold M d , because its rank is generally greater than d. That is why Riemannian optimization methods map point The standard Riemannian gradient method first projects the gradient step onto the tangent space at the current point X i and then retracts it back to the manifold: where R is the retraction operator, and P T M is the projection onto the tangent space.
Although the optimization problem is nonconvex, Riemannian optimization methods show good performance on it. Theoretical properties and convergence guarantees of such methods are discussed in (Wei et al., 2016) more thoroughly.

Projector-Splitting Algorithm
In our paper, we use a simplified version of such approach that retracts point X i + ∇F (X i ) directly to the manifold and does not require projection onto the tangent space P T M as illustrated in Figure 1: Intuitively, retractor R finds a rank-d matrix on the manifold M d that is similar to high-rank matrix X i + ∇F (X i ) in terms of Frobenius norm. How can we do it? The most straightforward way to reduce the rank of X i + ∇F (X i ) is to perform the SVD, which keeps d largest singular values of it: However, it is computationally expensive. Instead of this approach, we use the projectorsplitting method (Lubich and Oseledets, 2014), which is a second-order retraction onto the manifold (for details, see the review (Absil and Oseledets, 2015)). Its practical implementation is

CONCLUSIONS
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee.

CONCLUSIONS
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. WOODSTOCK '97 El Paso, Texas USA Copyright 20XX ACM X-XXXXX-XX-X/XX/XX ...$15.00.

CONCLUSIONS
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee.

CONCLUSIONS
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee.

CONCLUSIONS
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. WOODSTOCK '97 El Paso, Texas USA Copyright 20XX ACM X-XXXXX-XX-X/XX/XX ...$15.00. Figure 1: Geometric interpretation of one step of projector-splitting optimization procedure: the gradient step an the retraction of the high-rank matrix X i +∇F (X i ) to the manifold of low-rank matrices M d .
also quite intuitive: instead of computing the full SVD of X i + ∇F (X i ) according to the gradient projection method, we use just one step of the block power numerical method (Bentbib and Kanber, 2015) which computes the SVD, what reduces the computational complexity.
Let us keep the current point in the following factorized form: where matrices U i ∈ R n×d and V i ∈ R m×d have d orthonormal columns and S i ∈ R d×d . Then we need to perform two QR-decompositions to retract point X i + ∇F (X i ) back to the manifold: In this way, we always keep the solution X i+1 = U i+1 S i+1 V i+1 on the manifold M d and in the form (8).
What is important, we only need to compute ∇F (X i ), so the gradients with respect to U , S and V are never computed explicitly, thus avoiding the subtle case where S is close to singular (so-called singular (critical) point on the manifold). Indeed, the gradient with respect to U (while keeping the orthogonality constraints) can be written (Koch and Lubich, 2007) as: which means that the gradient will be large if S is close to singular. The projector-splitting scheme is free from this problem.

Algorithm
In case of SGNS objective given by (5), an element of gradient ∇F has the form: To make the method more flexible in terms of convergence properties, we additionally use λ ∈ R, which is a step size parameter. In this case, re- The whole optimization procedure is summarized in Algorithm 1.

Training Models
We compare our method ("RO-SGNS" in the tables) performance to two baselines: SGNS embeddings optimized via Stochastic Gradient Descent, implemented in the original "word2vec", ("SGD-SGNS" in the tables) (Mikolov et al., 2013) and embeddings obtained by SVD over SPPMI matrix ("SVD-SPPMI" in the tables) . We have also experimented with the blockwise alternating optimization over factors W and C, but the results are almost the same to SGD results, that is why we do not to include them into the paper. The source code of our experiments is available online 1 .
The models were trained on English Wikipedia "enwik9" corpus 2 , which was previously used in most papers on this topic. Like in previous studies, we counted only the words which occur more than 200 times in the training corpus Mikolov et al., 2013). As a result, we obtained a vocabulary of 24292 unique tokens (set of words V W and set of contexts V C are equal). The size of the context window was set to 5 for all experiments, as it was done in Mikolov et al., 2013). We conduct three series of experiments: for dimensionality d = 100, d = 200, and d = 500.
Algorithm 1 Riemannian Optimization for SGNS Require: Dimentionality d, initialization W 0 and C 0 , step size λ, gradient function ∇F : R n×m → R n×m , number of iterations K Ensure: Factor W ∈ R n×d 1: X 0 ← W 0 C 0 # get an initial point at the manifold 2: U 0 , S 0 , V 0 ← SVD(X 0 ) # compute the first point satisfying the low-rank constraint 3: for i ← 1, . . . , K do 4: # perform one step of the block power method 5:

Evaluation
We evaluate word embeddings via the word similarity task. We use the following popular datasets for this purpose: "wordsim-353" ( (Finkelstein et al., 2001); 3 datasets), "simlex-999" (Hill et al., 2016) and "men" (Bruni et al., 2014). Original "wordsim-353" dataset is a mixture of the word pairs for both word similarity and word relatedness tasks. This dataset was split (Agirre et al., 2009) into two intersecting parts: "wordsim-sim" ("ws-sim" in the tables) and "wordsim-rel" ("wsrel" in the tables) to separate the words from different tasks. In our experiments, we use both of them on a par with the full version of "wordsim-353" ("ws-full" in the tables). Each dataset contains word pairs together with assessor-assigned similarity scores for each pair. As a quality measure, we use Spearman's correlation between these human ratings and cosine similarities for each pair. We call this quality metric linguistic in our paper.

Results of Experiments
First of all, we compare the value of SGNS objective obtained by the methods. The comparison is demonstrated in Table 1.
We see that SGD-SGNS and SVD-SPPMI methods provide quite similar results, however, the proposed method obtains significantly better  SGNS values, what proves the feasibility of using Riemannian optimization framework in SGNS optimization problem. It is interesting to note that SVD-SPPMI method, which does not optimize SGNS objective directly, obtains better results than SGD-SGNS method, which aims at optimizing SGNS. This fact additionally confirms the idea described in Section 2.2.2 that the independent optimization over parameters W and C may decrease the performance. However, the target performance measure of embedding models is the correlation between semantic similarity and human assessment (Section 4.2). Table 2 presents the comparison of the methods in terms of it. We see that our method outperforms the competitors on all datasets except for "men" dataset where it obtains slightly worse results. Moreover, it is important that the higher dimension entails higher performance gain of our method in comparison to the competitors.
To understand how our model improves or degrades the performance in comparison to the baseline, we found several words, whose neighbors in terms of cosine distance change significantly. Table 3 demonstrates neighbors of the words "five", "he" and "main" for both SVD-SPPMI and RO-SGNS models. A neighbor is marked bold if we suppose that it has similar semantic meaning to the   Table 4: Examples of the semantic neighbors from 11th to 20th obtained for the word "usa" by all three methods. Top-10 neighbors for all three methods are exact names of states.
source word. First of all, we notice that our model produces much better neighbors of the words describing digits or numbers (see word "five" as an example). Similar situation happens for many other words, e.g. in case of "main" -the nearest neighbors contain 4 similar words for our model instead of 2 in case of SVD-SPPMI. The neighbourhood of "he" contains less semantically similar words in case of our model. However, it filters out irrelevant words, such as "promptly" and "dumbledore". Table 4 contains the nearest words to the word "usa" from 11th to 20th. We marked names of USA states bold and did not represent top-10 nearest words as they are exactly names of states for all three models. Some non-bold words are arguably relevant as they present large USA cities ("akron", "burbank", "madison") or geographical regions of several states ("midwest", "northeast", "southwest"), but there are also some completely irrelevant words ("uk", "cities", "places") presented by first two models.
Our experiments show that the optimal number of iterations K in the optimization procedure and step size λ depend on the particular value of d. For d = 100, we have K = 7, λ = 5 · 10 −5 , for d = 200, we have K = 8, λ = 5 · 10 −5 , and for d = 500, we have K = 2, λ = 10 −4 . Moreover, the best results were obtained when SVD-SPPMI embeddings were used as an initialization of Riemannian optimization process. Figure 2 illustrates how the correlation between semantic similarity and human assessment scores changes through iterations of our method. Optimal value of K is the same for both whole testing set and its 10-fold subsets chosen for cross-validation. The idea to stop optimization procedure on some iteration is also discussed in (Lai et al., 2015).
Training of the same dimensional models (d = 500) on English Wikipedia corpus using SGD-SGNS, SVD-SPPMI, RO-SGNS took 20 minutes, 10 minutes and 70 minutes respectively. Our method works slower, but not significantly. Moreover, since we were not focused on the code efficiency optimization, this time can be reduced. 6 Related Work

Word Embeddings
Skip-Gram Negative Sampling was introduced in (Mikolov et al., 2013). The "negative sampling" approach is thoroughly described in , and the learning method is explained in (Rong, 2014). There are several open-source implementations of SGNS neural network, which is widely known as "word2vec". 12 As shown in Section 2.2, Skip-Gram Negative Sampling optimization can be reformulated as a problem of searching for a low-rank matrix. In order to be able to use out-of-the-box SVD for this task, the authors of  used the surrogate version of SGNS as the objective function. There are two general assumptions made in their algorithm that distinguish it from the SGNS optimization: 1. SVD optimizes Mean Squared Error (MSE) objective instead of SGNS loss function.
2. In order to avoid infinite elements in SPMI matrix, it is transformed in ad-hoc manner (SPPMI matrix) before applying SVD.
This makes the objective not interpretable in terms of the original task (3). As mentioned in , SGNS objective weighs different (w, c) pairs differently, unlike the SVD, which works with the same weight for all pairs and may entail the performance fall. The comprehensive explanation of the relation between SGNS and SVD-SPPMI methods is provided in (Keerthi et al., 2015). (Lai et al., 2015;Levy et al., 2015) 1 Original Google word2vec: https://code. google.com/archive/p/word2vec/ 2 Gensim word2vec: https://radimrehurek. com/gensim/models/word2vec.html give a good overview of highly practical methods to improve these word embedding models.

Riemannian Optimization
An introduction to optimization over Riemannian manifolds can be found in (Udriste, 1994). The overview of retractions of high rank matrices to low-rank manifolds is provided in (Absil and Oseledets, 2015). The projector-splitting algorithm was introduced in (Lubich and Oseledets, 2014), and also was mentioned in (Absil and Oseledets, 2015) as "Lie-Trotter retraction".

Conclusions
In our paper, we proposed the general two-step scheme of training SGNS word embedding model and introduced the algorithm that performs the search of a solution in the low-rank form via Riemannian optimization framework. We also demonstrated the superiority of our method by providing experimental comparison to existing state-of-the-art approaches.
Possible direction of future work is to apply more advanced optimization techniques to the Step 1 of the scheme proposed in Section 1 and to explore the Step 2 -obtaining embeddings with a given low-rank matrix.