word2vec Explained: deriving Mikolov et al.'s negative-sampling word-embedding method

Yoav Goldberg, Omer Levy

The skip-gram model

The departure point of the paper is the skip-gram model. In this model we are given a corpus of words ww and their contexts cc. We consider the conditional probabilities p(c∣w)p(c|w), and given a corpus TextText, the goal is to set the parameters θ\theta of p(c∣w;θ)p(c|w;\theta) so as to maximize the corpus probability:

in this equation, C(w)C(w) is the set of contexts of word ww. Alternatively:

here DD is the set of all word and context pairs we extract from the text.

One approach for parameterizing the skip-gram model follows the neural-network language models literature, and models the conditional probability p(c∣w;θ)p(c|w;\theta) using soft-max:

where vcv_{c} and vw∈Rdv_{w}\in R^{d} are vector representations for cc and ww respectively, and CC is the set of all available contexts.Throughout this note, we assume that the words and the contexts come from distinct vocabularies, so that, for example, the vector associated with the word dog will be different from the vector associated with the context dog. This assumption follows the literature, where it is not motivated. One motivation for making this assumption is the following: consider the case where both the word dog and the context dog share the same vector vv. Words hardly appear in the contexts of themselves, and so the model should assign a low probability to p(dog∣dog)p(dog|dog), which entails assigning a low value to v⋅vv\cdot v which is impossible. The parameters θ\theta are vciv_{c_{i}}, vwiv_{w_{i}} for w∈Vw\in V, c∈Cc\in C, i∈1,⋯ ,di\in 1,\cdots,d (a total of ∣C∣×∣V∣×d|C|\times|V|\times d parameters). We would like to set the parameters such that the product (2) is maximized.

Now will be a good time to take the log and switch from product to sum:

An assumption underlying the embedding process is the following:

maximizing objective 4 will result in good embeddings vw      ∀ w∈ Vv_{w}\;\;\;\forall~{}w\in~{}V, in the sense that similar words will have similar vectors.

It is not clear to us at this point why this assumption holds.

While objective (4) can be computed, it is computationally expensive to do so, because the term p(c∣w;θ)p(c|w;\theta) is very expensive to compute due to the summation ∑c′∈Cevc′⋅vw\sum_{c^{\prime}\in C}e^{v_{c^{\prime}}\cdot v_{w}} over all the contexts c′c^{\prime} (there can be hundreds of thousands of them). One way of making the computation more tractable is to replace the softmax with an hierarchical softmax. We will not elaborate on this direction.

Negative Sampling

Mikolov et al. present the negative-sampling approach as a more efficient way of deriving word embeddings. While negative-sampling is based on the skip-gram model, it is in fact optimizing a different objective. What follows is the derivation of the negative-sampling objective.

Consider a pair (w,c)(w,c) of word and context. Did this pair come from the training data? Let’s denote by p(D=1∣w,c)p(D=1|w,c) the probability that (w,c)(w,c) came from the corpus data. Correspondingly, p(D=0∣w,c)=1−p(D=1∣w,c)p(D=0|w,c)=1-p(D=1|w,c) will be the probability that (w,c)(w,c) did not come from the corpus data. As before, assume there are parameters θ\theta controlling the distribution: p(D=1∣w,c;θ)p(D=1|w,c;\theta). Our goal is now to find parameters to maximize the probabilities that all of the observations indeed came from the data:

The quantity p(D=1∣c,w;θ)p(D=1|c,w;\theta) can be defined using softmax:

This objective has a trivial solution if we set θ\theta such that p(D=1∣w,c;θ)=1p(D=1|w,c;\theta)=1 for every pair (w,c)(w,c). This can be easily achieved by setting θ\theta such that vc=vwv_{c}=v_{w} and vc⋅vw=Kv_{c}\cdot v_{w}=K for all vc,vwv_{c},v_{w}, where KK is large enough number (practically, we get a probability of 1 as soon as K≈40K\approx 40).

We need a mechanism that prevents all the vectors from having the same value, by disallowing some (w,c)(w,c) combinations. One way to do so, is to present the model with some (w,c)(w,c) pairs for which p(D=1∣w,c;θ)p(D=1|w,c;\theta) must be low, i.e. pairs which are not in the data. This is achieved by generating the set D′D^{\prime} of random (w,c)(w,c) pairs, assuming they are all incorrect (the name “negative-sampling” stems from the set D′D^{\prime} of randomly sampled negative examples). The optimization objective now becomes:

If we let σ(x)=11+e−x\sigma(x)=\frac{1}{1+e^{-x}} we get:

which is almost equation (4) in Mikolov et al ().

The difference from Mikolov et al. is that here we present the objective for the entire corpus D∪D′D\cup D^{\prime}, while they present it for one example (w,c)∈D(w,c)\in D and kk examples (w,cj)∈D′(w,c_{j})\in D^{\prime}, following a particular way of constructing D′D^{\prime}.

Specifically, with negative sampling of kk, Mikolov et al.’s constructed D′D^{\prime} is kk times larger than DD, and for each (w,c)∈D(w,c)\in D we construct kk samples (w,c1),…,(w,ck)(w,c_{1}),\ldots,(w,c_{k}), where each cjc_{j} is drawn according to its unigram distribution raised to the 3/43/4 power. This is equivalent to drawing the samples (w,c)(w,c) in D′D^{\prime} from the distribution (w,c) ∼ pwords(w)pcontexts(c)3/4Z(w,c)~{}\sim~{}p_{words}(w)\frac{p_{contexts}(c)^{3/4}}{Z}, where pwords(w)p_{words}(w) and pcontexts(c)p_{contexts}(c) are the unigram distributions of words and contexts respectively, and ZZ is a normalization constant. In the work of Mikolov et al. each context is a word (and all words appear as contexts), and so pcontext(x)=pwords(x)=count(x)∣Text∣p_{context}(x)=p_{words}(x)=\frac{count(x)}{|Text|}

Unlike the Skip-gram model described above, the formulation in this section does not model p(c∣w)p(c|w) but instead models a quantity related to the joint distribution of ww and cc.

If we fix the words representation and learn only the contexts representation, or fix the contexts representation and learn only the word representations, the model reduces to logistic regression, and is convex. However, in this model the words and contexts representations are learned jointly, making the model non-convex.

Context definitions

This section lists some peculiarities of the contexts used in the word2vec software, as reflected in the code. Generally speaking, for a sentence of nn words w1,…,wnw_{1},\dots,w_{n}, contexts of a word wiw_{i} comes from a window of size kk around the word: C(w)=wi−k,…,wi−1,wi+1,…,wi+kC(w)=w_{i-k},\dots,w_{i-1},w_{i+1},\dots,w_{i+k}, where kk is a parameter. However, there are two subtleties:

the window size that is being used is dynamic – the parameter kk denotes the maximalmaximal window size. For each word in the corpus, a window size k′k^{\prime} is sampled uniformly from 1,…,k1,\dots,k.

word2vec has two additional parameters for discarding some of the input words: words appearing less than min-count times are not considered as either words or contexts, an in addition frequent words (as defined by the sample parameter) are down-sampled. Importantly, these words are removed from the text before generating the contexts. This has the effect of increasing the effective window size for certain words. According to Mikolov et al. , sub-sampling of frequent words improves the quality of the resulting embedding on some benchmarks. The original motivation for sub-sampling was that frequent words are less informative. Here we see another explanation for its effectiveness: the effective window size grows, including context-words which are both content-full and linearly far away from the focus word, thus making the similarities more topical.

Why does this produce good word representations?

The distributional hypothesis states that words in similar contexts have similar meanings. The objective above clearly tries to increase the quantity vw⋅ vcv_{w}\cdot~{}v_{c} for good word-context pairs, and decrease it for bad ones. Intuitively, this means that words that share many contexts will be similar to each other (note also that contexts sharing many words will also be similar to each other). This is, however, very hand-wavy.

Can we make this intuition more precise? We’d really like to see something more formal.

References