WordRank: Learning Word Embeddings via Robust Ranking

Embedding words in a vector space has gained a lot of attention in recent years. While state-of-the-art methods provide efficient computation of word similarities via a low-dimensional matrix embedding, their motivation is often left unclear. In this paper, we argue that word embedding can be naturally viewed as a ranking problem due to the ranking nature of the evaluation metrics. Then, based on this insight, we propose a novel framework WordRank that efficiently estimates word representations via robust ranking, in which the attention mechanism and robustness to noise are readily achieved via the DCG-like ranking losses. The performance of WordRank is measured in word similarity and word analogy benchmarks, and the results are compared to the state-of-the-art word embedding techniques. Our algorithm is very competitive to the state-of-the- arts on large corpora, while outperforms them by a significant margin when the training set is limited (i.e., sparse and noisy). With 17 million tokens, WordRank performs almost as well as existing methods using 7.2 billion tokens on a popular word similarity benchmark. Our multi-node distributed implementation of WordRank is publicly available for general usage.


Introduction
Embedding words into a vector space, such that semantic and syntactic regularities between words are preserved, is an important sub-task for many applications of natural language processing. Mikolov et al. (2013a) generated considerable excitement in the machine learning and natural language processing communities by introducing a neural network based model, which they call word2vec. It was shown that word2vec produces state-of-the-art performance on both word similarity as well as word analogy tasks. The word similarity task is to retrieve words that are similar to a given word. On the other hand, word analogy requires answering queries of the form a:b;c:?, where a, b, and c are words from the vocabulary, and the answer to the query must be semantically related to c in the same way as b is related to a. This is best illustrated with a concrete example: Given the query king:queen;man:? we expect the model to output woman.
The impressive performance of word2vec led to a flurry of papers, which tried to explain and improve the performance of word2vec both theoretically (Arora et al., 2015) and empirically (Levy and Goldberg, 2014). One interpretation of word2vec is that it is approximately maximizing the positive pointwise mutual information (PMI), and Levy and Goldberg (2014) showed that directly optimizing this gives good results. On the other hand, Pennington et al. (2014) showed performance comparable to word2vec by using a modified matrix factorization model, which optimizes a log loss.
Somewhat surprisingly, Levy et al. (2015) showed that much of the performance gains of these new word embedding methods are due to certain hyperparameter optimizations and system-design choices. In other words, if one sets up careful experiments, then existing word embedding models more or less perform comparably to each other. We conjecture that this is because, at a high level, all these methods are based on the following template: From a large text corpus eliminate infrequent words, and compute a |W| × |C| word-context co-occurrence count matrix; a context is a word which appears less than d distance away from a given word in the text, where d is a tunable parameter. Let w ∈ W be a word and c ∈ C be a context, and let X w,c be the (potentially normalized) co-occurrence count. One learns a function f (w, c) which approximates a transformed version of X w,c . Different methods differ essentially in the transformation function they use and the parametric form of f (Levy et al., 2015). For example, GloVe (Pennington et al., 2014) uses f (w, c) = u w , v c where u w and v c are k dimensional vectors, ·, · denotes the Euclidean dot product, and one approximates f (w, c) ≈ log X w,c . On the other hand, as Levy and Goldberg (2014) show, word2vec can be seen as using the same f (w, c) as GloVe but trying to approximate f (w, c) ≈ PMI(X w,c ) − log n, where PMI(·) is the pairwise mutual information (Cover and Thomas, 1991) and n is the number of negative samples.
In this paper, we approach the word embedding task from a different perspective by formulating it as a ranking problem. That is, given a word w, we aim to output an ordered list (c 1 , c 2 , · · · ) of context words from C such that words that cooccur with w appear at the top of the list. If rank(w, c) denotes the rank of c in the list, then typical ranking losses optimize the following objective: (w,c)∈Ω ρ (rank(w, c)), where Ω ⊂ W × C is the set of word-context pairs that co-occur in the corpus, and ρ(·) is a ranking loss function that is monotonically increasing and concave (see Sec. 2 for a justification).
Casting word embedding as ranking has two distinctive advantages. First, our method is discriminative rather than generative; in other words, instead of modeling (a transformation of) X w,c directly, we only aim to model the relative order of X w,· values in each row. This formulation fits naturally to popular word embedding tasks such as word similarity/analogy since instead of the likelihood of each word, we are interested in finding the most relevant words in a given context 1 . Second, casting word 1 Roughly speaking, this difference in viewpoint is analogous to the difference between pointwise loss function vs list-embedding as a ranking problem enables us to design models robust to noise (Yun et al., 2014) and focusing more on differentiating top relevant words, a kind of attention mechanism that has been proved very useful in deep learning (Larochelle and Hinton, 2010;Mnih et al., 2014;Bahdanau et al., 2015). Both issues are very critical in the domain of word embedding since (1) the co-occurrence matrix might be noisy due to grammatical errors or unconventional use of language, i.e., certain words might cooccur purely by chance, a phenomenon more acute in smaller document corpora collected from diverse sources; and (2) it's very challenging to sort out a few most relevant words from a very large vocabulary, thus some kind of attention mechanism that can trade off the resolution on most relevant words with the resolution on less relevant words is needed. We will show in the experiments that our method can mitigate some of these issues; with 17 million tokens our method performs almost as well as existing methods using 7.2 billion tokens on a popular word similarity benchmark.

Notation
We use w to denote a word and c to denote a context. The set of all words, that is, the vocabulary is denoted as W and the set of all context words is denoted C. We will use Ω ⊂ W × C to denote the set of all word-context pairs that were observed in the data, Ω w to denote the set of contexts that cooccured with a given word w, and similarly Ω c to denote the words that co-occurred with a given context c. The size of a set is denoted as |·|. The inner product between vectors is denoted as ·, · .

Ranking Model
Let u w denote the k-dimensional embedding of a word w, and v c denote that of a context c. For convenience, we collect embedding parameters for words and contexts as U := {u w } w∈W , and V := {v c } c∈C .
We aim to capture the relevance of context c for word w by the inner product between their embedding vectors, u w , v c ; the more relevant a context is, the larger we want their inner product to be. wise loss function used in ranking (Lee and Lin, 2013). We achieve this by learning a ranking model that is parametrized by U and V. If we sort the set of contexts C for a given word w in terms of each context's inner product score with the word, the rank of a specific context c in this list can be written as (Usunier et al., 2009): where I(x ≤ 0) is a 0-1 loss function which is 1 if x ≤ 0 and 0 otherwise. Since I(x ≤ 0) is a discontinuous function, we follow the popular strategy in machine learning which replaces the 0-1 loss by its convex upper bound (·), where (·) can be any popular loss function for binary classification such as the hinge loss (x) = max (0, 1 − x) or the logistic loss (x) = log 2 (1 + 2 −x ) ( Bartlett et al., 2006). This enables us to construct the following convex upper bound on the rank: It is certainly desirable that the ranking model positions relevant contexts at the top of the list; this motivates us to write the objective function to minimize as: where r w,c is the weight between word w and context c quantifying the association between them, ρ(·) is a monotonically increasing and concave ranking loss function that measures goodness of a rank, and α > 0, β > 0 are the hyperparameters of the model whose role will be discussed later. Following Pennington et al. (2014), we use where we set x max = 100 and = 0.75 in our experiments. That is, we assign larger weights (with a saturation) to contexts that appear more often with the word of interest, and vice-versa. For the ranking loss function ρ(·), on the other hand, we consider the class of monotonically increasing and concave functions. While monotonicity is a natural requirement, we argue that concavity is also important so that the derivative of ρ is always non-increasing; this implies that the ranking loss to be the most sensitive at the top of the list (where the rank is small) and becomes less sensitive at the lower end of the list (where the rank is high). Intuitively this is desirable, because we are interested in a small number of relevant contexts which frequently co-occur with a given word, and thus are willing to tolerate errors on infrequent contexts 2 . Meanwhile, this insensitivity at the bottom of the list makes the model robust to noise in the data either due to grammatical errors or unconventional use of language. Therefore, a single ranking loss function ρ(·) serves two different purposes at two ends of the curve (see the example plots of ρ in Figure 1); while the left hand side of the curve encourages "high resolution" on most relevant words, the right hand side becomes less sensitive (with "low resolution") to infrequent and possibly noisy words 3 . As we will demonstrate in our experiments, this is a fundamental attribute (in addition to the ranking nature) of our method that contributes its superior performance as compared to the state-of-the-arts when the training set is limited (i.e., sparse and noisy).
What are interesting loss functions that can be used for ρ (·)? Here are four possible alternatives, all of which have a natural interpretation (see the plots of all four ρ functions in Figure 1(a) and the related work in Sec. 3 for a discussion). We will explore the performance of each of these variants in our experiments. For now, we turn our attention to efficient stochastic optimization of the objective function (3).

Stochastic Optimization
Plugging (2) into (3), and replacing w∈W c∈Ωw by (w,c)∈Ω , the objective function becomes: This function contains summations over Ω and C, both of which are expensive to compute for a large corpus. Although stochastic gradient descent (SGD) (Bottou and Bousquet, 2011) can be used to replace the summation over Ω by random sampling, the summation over C cannot be avoided unless ρ(·) is a linear function. To work around this problem, we propose to optimize a linearized upper bound of the objective function obtained through a first-order Taylor approximation. Observe that due to the concavity of ρ(·), we have for any x and ξ = 0. Moreover, the bound is tight when ξ = x −1 . This motivates us to introduce a set of auxiliary parameters Ξ := {ξ w,c } (w,c)∈Ω and define the following upper bound of J (U, V): Note that J (U, V) ≤ J (U, V, Ξ) for any Ξ, due to (10) 4 . Also, minimizing (11) yields the same U and V as minimizing (9). To see this, supposeÛ := {û w } w∈W andV := {v c } c∈C minimizes (9). Then, we have J Û ,V,Ξ = J Û ,V . Therefore, it suffices to optimize (11). However, unlike (9), (11) admits an efficient SGD algorithm. To see this, rewrite (11) as where (w, c, c ) ∈ Ω × (C \ {c}). Then, it can be seen that if we sample uniformly from (w, c) ∈ Ω and c ∈ C \ {c}, then j(w, c, c ) := which does not contain any expensive summations and is an unbiased estimator of (13), i.e., On the other hand, one can optimize ξ w,c exactly by using (12). Putting everything together yields a stochastic optimization algorithm WordRank, which can be specialized to a variety of ranking loss functions ρ(·) with weights r w,c (e.g., DCG (Discounted Cumulative Gain) (Manning et al., 2008) is one of many possible instantiations). Algorithm 1 contains detailed pseudo-code. It can be seen that the algorithm is divided into two stages: a stage that updates (U, V) and another that updates Ξ. Note that the time complexity of the first stage is O(|Ω|) since the cost of each update in Lines 8-10 is independent of the size of the corpus. On the other hand, the time complexity of updating Ξ in Line 15 is O(|Ω| |C|), which can be expensive. To amortize this cost, we employ two tricks: we only update Ξ after a few iterations of U and V update, and we exploit the fact that the most computationally expensive operation in (12) involves a matrix and matrix multiplication which can be calculated efficiently via the SGEMM routine in BLAS (Dongarra et al., 1990).

Parallelization
The updates in Lines 8-10 have one remarkable property: To update u w , v c and v c , we only need to read the variables u w , v c , v c and ξ w,c . What this means is that updates to another triplet of variables uŵ, vĉ and vĉ can be performed independently. This observation is the key to developing a parallel optimization strategy, by distributing the computation of the updates among multiple processors. Due to lack of space, details including pseudo-code are relegated to the supplementary material.

Interpreting of α and β
The update (12) indicates that ξ −1 w,c is proportional to rank (w, c). On the other hand, one can observe that the loss function (·) in (14) is weighted by a ρ ξ −1 w,c term. Since ρ (·) is concave, its gradient ρ (·) is monotonically non-increasing (Rockafellar, 1970). Consequently, when rank (w, c) and hence ξ −1 w,c is large, ρ ξ −1 w,c is small. In other words, the loss function "gives up" on contexts with high ranks in order to focus its attention on top of the list. The rate at which the algorithm gives up is determined by the hyperparameters α and β. For the illustration of this effect, see the example plots of ρ 1 with different α and β in Figure 1(b). Intuitively, α can be viewed as a scale parameter while β can be viewed as an offset parameter. An equivalent interpretation is that by choosing different values of α and β one can modify the behavior of the ranking loss ρ (·) in a problem dependent fashion. In our experiments, we found that a common setting of α = 1 and β = 0 often yields uncompetitive performance, while setting α = 100 and β = 99 generally gives good results.

Related Work
Our work sits at the intersection of word embedding and ranking optimization. As we discussed in Sec. 2.2 and Sec. 2.5, it's also related to the attention mechanism widely used in deep learning. We therefore review the related work along these three axes.
Word Embedding. We already discussed some related work (word2vec and GloVe) on word embedding in the introduction. Essentially, word2vec and GloVe derive word representations by modeling a transformation (PMI or log) of X w,c directly, while WordRank learns word representations via robust ranking. Besides these state-of-the-art techniques, a few ranking-based approaches have been proposed for word embedding recently, e.g., (Collobert and Weston, 2008;Vilnis and McCallum, 2015;Liu et al., 2015). However, all of them adopt a pair-wise binary classification approach with a linear ranking loss ρ 0 . For example, (Collobert and Weston, 2008;Vilnis and McCallum, 2015) employ a hinge loss on positive/negative word pairs to learn word representations and ρ 0 is used implicitly to evaluate ranking losses. As we discussed in Sec. 2.2, ρ 0 has no benefit of the attention mechanism and robustness to noise since its linearity treats all the ranking errors uniformly; empirically, sub-optimal performances are often observed with ρ 0 in our experiments. More recently, by extending the Skip-Gram model of word2vec, Liu et al. (2015) incorporates additional pair-wise constraints induced from 3rdparty knowledge bases, such as WordNet, and learns word representations jointly. In contrast, WordRank is a fully ranking-based approach without using any additional data source for training.
Robust Ranking. The second line of work that is very relevant to WordRank is that of ranking objective (3). The use of score functions u w , v c for ranking is inspired by the latent collaborative retrieval framework of Weston et al. (2012). Writing the rank as a sum of indicator functions (1), and upper bounding it via a convex loss (2) is due to Usunier et al. (2009). Using ρ 0 (·) (5) corresponds to the well-known pairwise ranking loss (see e.g., (Lee and Lin, 2013)). On the other hand, Yun et al. (2014) observed that if they set ρ = ρ 2 as in (7), then −J (U, V) corresponds to the DCG (Discounted Cumulative Gain), one of the most popular ranking metrics used in web search ranking (Manning et al., 2008). In their RobiRank algorithm they proposed the use of ρ = ρ 1 (6), which they considered to be a special function for which one can derive an efficient stochastic optimization procedure. However, as we showed in this paper, the general class of monotonically increasing concave functions can be handled efficiently. Another important difference of our approach is the hyperparameters α and β, which we use to modify the behavior of ρ, and which we find are critical to achieve good empirical results. Ding and Vishwanathan (2010) proposed the use of ρ = log t in the context of robust binary classification, while here we are concerned with ranking, and our formulation is very general and applies to a variety of ranking losses ρ (·) with weights r w,c . Optimizing over U and V by distributing the computation across processors is inspired by work on distributed stochastic gradient for matrix factorization (Gemulla et al., 2011).
Attention. Attention is one of the most important advancements in deep learning in recent years (Larochelle and Hinton, 2010), and is now widely used in state-of-the-art image recognition and machine translation systems (Mnih et al., 2014;Bahdanau et al., 2015). Recently, attention has also been applied to the domain of word embedding. For example, under the intuition that not all contexts are created equal, Wang et al. (2015) assign an importance weight to each word type at each context position and learn an attention-based Continuous Bag-Of-Words (CBOW) model. Similarly, within a ranking framework, WordRank expresses the context importance by introducing the auxiliary variable ξ w,c , which "gives up" on contexts with high ranks in order to focus its attention on top of the list.

Experiments
In our experiments, we first evaluate the impact of the weight r w,c and the ranking loss function ρ(·) on the test performance using a small dataset. We then pick the best performing model and compare it against word2vec (Mikolov et al., 2013b) and GloVe (Pennington et al., 2014). We closely follow the framework of Levy et al. (2015) to set up a careful and fair comparison of the three methods. Our code is publicly available at https://bitbucket. org/shihaoji/wordrank.
Training Corpus Models are trained on a combined corpus of 7.2 billion tokens, which consists of the 2015 Wikipedia dump with 1.6 billion tokens, the WMT14 News Crawl 5 with 1.7 billion tokens, the "One Billion Word Language Modeling Benchmark" 6 with almost 1 billion tokens, and UMBC   webbase corpus 7 with around 3 billion tokens. The pre-processing pipeline breaks the paragraphs into sentences, tokenizes and lowercases each corpus with the Stanford tokenizer. We further clean up the dataset by removing non-ASCII characters and punctuation, and discard sentences that are shorter than 3 tokens or longer than 500 tokens. In the end, we obtain a dataset of 7.2 billion tokens, with the first 1.6 billion tokens from Wikipedia. When we want to experiment with a smaller corpus, we extract a subset which contains the specified number of tokens.
Co-occurrence matrix construction We use the GloVe code to construct the co-occurrence matrix X, and the same matrix is used to train GloVe and WordRank models. When constructing X, we must choose the size of the vocabulary, the context window and whether to distinguish left context from right context. We follow the findings and design choices of GloVe and use a symmetric window of size win with a decreasing weighting function, so that word pairs that are d words apart contribute 1/d to the total count. Specifically, when the corpus is small (e.g., 17M, 32M, 64M) we let win = 15 and for larger corpora we let win = 10. The larger window size alleviates the data sparsity issue for small corpus at the expense of adding more noise to X. The parameter settings used in our experiments are summarized in Table 1.
Using the trained model It has been shown by Pennington et al. (2014) that combining the u w and v c vectors with equal weights gives a small boost 7 http://ebiquity.umbc.edu/resource/html/ id/351 in performance. This vector combination was originally motivated as an ensemble method (Pennington et al., 2014), and later Levy et al. (2015) provided a different interpretation of its effect on the cosine similarity function, and show that adding context vectors effectively adds first-order similarity terms to the second-order similarity function. In our experiments, we find that vector combination boosts the performance in word analogy task when training set is small, but when dataset is large enough (e.g., 7.2 billion tokens), vector combination doesn't help anymore. More interestingly, for the word similarity task, we find that vector combination is detrimental in all the cases, sometimes even substantially 8 . Therefore, we will always use u w on word similarity task, and use u w + v c on word analogy task unless otherwise noted.

Evaluation
Word Similarity We use six datasets to evaluate word similarity: WS-353 (Finkelstein et al., 2002) partitioned into two subsets: WordSim Similarity and WordSim Relatedness (Agirre et al., 2009); MEN (Bruni et al., 2012); Mechanical Turk (Radinsky et al., 2011); Rare words (Luong et al., 2013); and SimLex-999 (Hill et al., 2014). They contain word pairs together with human-assigned similarity judgments. The word representations are evaluated by ranking the pairs according to their cosine similarities, and measuring the Spearman's rank correlation coefficient with the human judgments. Word Analogies For this task, we use the Google analogy dataset (Mikolov et al., 2013a). It contains 19544 word analogy questions, partitioned into 8869 semantic and 10675 syntactic questions. A question is correctly answered only if the algorithm selects the word that is exactly the same as the correct word in the question: synonyms are thus counted as mistakes. There are two ways to answer these questions, namely, by using 3CosAdd or 3CosMul (see (Levy and Goldberg, 2014) for details). We will report scores by using 3CosAdd by default, and indicate when 3CosMul gives better performance.

The impact of r w,c and ρ(·)
In Sec. 2.2 we argued the need for adding weight r w,c to ranking objective (3), and we also presented our framework which can deal with a variety of ranking loss functions ρ. We now study the utility of these two ideas. We report results on the 17 million token dataset in Table 2. For the similarity task, we use the WS-353 test set and for the analogy task we use the Google analogy test set. The best scores for each task are underlined. We set t = 1.5 for ρ 3 . "Off" means that we used uniform weight r w,c = 1, and "on" means that r w,c was set as in (4). For comparison, we also include the results using RobiRank (Yun et al., 2014) 9 .
It can be seen from Table 2 that adding the weight r w,c improves performance in all the cases, especially on the word analogy task. Among the four ρ functions, ρ 0 performs the best on the word similarity task but suffers notably on the analogy task, while ρ 1 = log performs the best overall. Given these observations, which are consistent with the results on large scale datasets, in the experiments that follow we only report WordRank with the best configuration, i.e., using ρ 1 with the weight r w,c as defined in (4).

Comparison to state-of-the-arts
In this section we compare the performance of Wor-dRank with word2vec 10 and GloVe 11 , by using the code provided by the respective authors. For a fair comparison, GloVe and WordRank are given as input the same co-occurrence matrix X; this eliminates differences in performance due to window size and other such artifacts, and the same parameters are used to word2vec. Moreover, the embedding dimensions used for each of the three methods is the same (see Table 1). With word2vec, we train the Skip-Gram with Negative Sampling (SGNS) model since it produces state-of-the-art performance, and is widely used in the NLP community (Mikolov et al., 2013b). For GloVe, we use the default parameters as suggested by (Pennington et al., 2014). The results are provided in Figure 2 (also see Table 4 in the supplementary material for additional details).
As can be seen, when the size of corpus increases, in general all three algorithms improve their prediction accuracy on both tasks. This is to be expected since a larger corpus typically produces better statistics and less noise in the co-occurrence matrix X. When the corpus size is small (e.g., 17M, 32M, 64M, 128M), WordRank yields the best performance with significant margins among three, followed by word2vec and GloVe; when the size of corpus increases further, on the word analogy task word2vec and GloVe become very competitive to WordRank, and eventually perform neck-to-neck to each other (Figure 2(b)). This is consistent with the findings of (Levy et al., 2015) indicating that when the number of tokens is large even simple algorithms can perform well. On the other hand, WordRank is dominant on the word similarity task for all the cases (Figure 2(a)) since it optimizes a ranking loss explicitly, which aligns more naturally with the objective of word similarity than the other methods; with 17 million tokens our method performs almost as well as existing methods using 7.2 billion tokens on the word similarity benchmark.
To further evaluate the model performance on the word similarity/analogy tasks, we use the best performing models trained on the 7.2-billion-token corpus to predict on the six word similarity datasets described in Sec. 4.1. Moreover, we breakdown the performance of the models on the Google word analogy dataset into the semantic and syntactic subtasks. Results are listed in Table 3. As can be seen, Wor-dRank outperforms word2vec and GloVe on 5 of 6 similarity tasks, and 1 of 2 Google analogy subtasks.    Table 3: Performance of the best word2vec, GloVe and WordRank models, learned from 7.2 billion tokens, on six similarity tasks and Google semantic and syntactic subtasks.

Visualizing the results
To understand whether WordRank produces syntatically and semantically meaningful vector space, we did the following experiment: we use the best performing model produced using 7.2 billion tokens, and compute the nearest neighbors of the word "cat". We then visualize the words in two dimensions by using t-SNE (Maaten and Hinton, 2008). As can be seen in Figure 3, our ranking-based model is indeed capable of capturing both semantic (e.g., cat, feline, kitten, tabby) and syntactic (e.g., leash, leashes, leashed) regularities of the English language.

Conclusion
We proposed WordRank, a ranking-based approach, to learn word representations from large scale textual corpora. The most prominent difference between our method and the state-of-the-art techniques, such as word2vec and GloVe, is that Wor-dRank learns word representations via a robust ranking model, while word2vec and GloVe typically model a transformation of co-occurrence count X w,c directly. Moreover, by a ranking loss function ρ(·), WordRank achieves its attention mechanism and robustness to noise naturally, which are usually lack- ing in other ranking-based approaches. These attributes significantly boost the performance of Wor-dRank in the cases where training data are sparse and noisy. Our multi-node distributed implementation of WordRank is publicly available for general usage.