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

Yoav GoldbergOmer Levy

article2014arXiv1,722 citations

Explains the mathematical derivation and intuitive rationale behind Mikolov et al.'s negative-sampling objective, providing a clear formulation of how word2vec optimizes word embeddings.

Listen

Modern natural language processing relies heavily on converting text into numerical vectors that capture word meanings. While the popular word2vec software achieved state-of-the-art results, its original mathematical explanations and design choices remained difficult to interpret. The article provides a clear, step-by-step derivation of the negative-sampling method used in word2vec and analyzes practical implementation choices embedded in the software.

The analysis demonstrates that negative sampling fundamentally reframes the learning problem. Instead of predicting a context word given a target word—which requires computationally prohibitive calculations across hundreds of thousands of vocabulary words—negative sampling converts the task into a binary classification problem. The model learns to distinguish true word-context pairs observed in the training text from randomly generated negative pairs. Without these negative samples, the model would produce a trivial solution where all vector representations collapse into identical values.

In addition to the mathematical formulation, the article highlights critical implementation details in the software that significantly alter the training data. The software utilizes a dynamic context window, choosing a random window size up to a defined maximum for each word. Furthermore, infrequent words are pruned entirely, and highly frequent words are down-sampled before generating context windows. This preprocessing step effectively expands the window size across remaining words, allowing the model to capture broader topical relationships between distant, content-heavy words.

These findings show that word2vec's high performance and computational efficiency stem not only from its classification objective, but also from subtle data preparation heuristics. The authors note, however, that a theoretical gap remains: while the system relies on the assumption that words appearing in similar contexts share similar meanings, a formal mathematical explanation for why this specific optimization objective consistently produces high-quality word representations has yet to be established.

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

Abstract

The word2vec software of Tomas Mikolov and colleagues (this https URL ) has gained a lot of traction lately, and provides state-of-the-art word embeddings. The learning models behind the software are described in two research papers. We found the description of the models in these papers to be somewhat cryptic and hard to follow. While the motivations and presentation may be obvious to the neural-networks language-modeling crowd, we had to struggle quite a bit to figure out the rationale behind the equations.

This note is an attempt to explain equation (4) (negative sampling) in "Distributed Representations of Words and Phrases and their Compositionality" by Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado and Jeffrey Dean.

Table of Contents

  • 1 The skip-gram model
  • 1.1 Parameterization of the skip-gram model
  • 2 Negative Sampling
  • 2.1 Remarks
  • 3 Context definitions
  • 4 Why does this produce good word representations?
  • References

Knowls

  1. Knowl 1 — Skip-Gram with Negative Sampling Objective Function

    equation

    The Skip-gram model parameterized with Negative Sampling (SGNS) optimizes target word embeddings vw∈Rdv_w \in \mathbb{R}^d for words w∈Vw \in V and context embeddings vc∈Rdv_c \in \mathbb{R}^d for context items c∈Cc \in C over a corpus of observed word-context pairs DD and generated negative pairs D′D' by maximizing the objective function:

    arg⁡max⁡θ∑(w,c)∈Dlog⁡σ(vc⋅vw)+∑(w,c)∈D′log⁡σ(−vc⋅vw)\arg \max_\theta \sum_{(w,c) \in D} \log \sigma(v_c \cdot v_w) + \sum_{(w,c) \in D'} \log \sigma(-v_c \cdot v_w)

    where θ={vw}w∈V∪{vc}c∈C\theta = \{v_w\}_{w \in V} \cup \{v_c\}_{c \in C}, σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}} is the logistic sigmoid function, and vc⋅vwv_c \cdot v_w denotes the scalar dot product between the context vector vcv_c and target word vector vwv_w. In this framework, DD is the multiset of positive word-context pairs extracted from text, and D′D' is the multiset of randomly sampled negative pairs where each true pair (w,c)∈D(w,c) \in D is matched with kk sampled negative pairs (w,cj)∈D′(w, c_j) \in D'.

  2. Knowl 2 — Binary Classification Formulation of Negative Sampling

    theoretical result

    Unlike standard Skip-gram, which models the conditional probability p(c∣w)p(c|w) via a full softmax distribution over the entire context vocabulary CC:

    p(c∣w;θ)=evc⋅vw∑c′∈Cevc′⋅vwp(c \mid w; \theta) = \frac{e^{v_c \cdot v_w}}{\sum_{c' \in C} e^{v_{c'} \cdot v_w}}

    the negative sampling formulation does not model p(c∣w)p(c|w). Instead, it casts the objective as binary classification over the joint distribution of word-context pairs (w,c)(w, c). Letting the binary random variable D=1D=1 indicate that (w,c)(w, c) comes from the true corpus and D=0D=0 indicate that it was drawn from noise, the probabilities are parameterized as:

    p(D=1∣w,c;θ)=σ(vc⋅vw)=11+e−vc⋅vwp(D = 1 \mid w, c; \theta) = \sigma(v_c \cdot v_w) = \frac{1}{1 + e^{-v_c \cdot v_w}}

    p(D=0∣w,c;θ)=1−p(D=1∣w,c;θ)=σ(−vc⋅vw)=11+evc⋅vwp(D = 0 \mid w, c; \theta) = 1 - p(D = 1 \mid w, c; \theta) = \sigma(-v_c \cdot v_w) = \frac{1}{1 + e^{v_c \cdot v_w}}

    where vw,vc∈Rdv_w, v_c \in \mathbb{R}^d are the embedding vectors for target word ww and context word cc. Maximizing the likelihood of observed pairs DD alone admits a trivial degenerate solution where all vectors are identical and vc⋅vw→∞v_c \cdot v_w \to \infty; the inclusion of negative pairs D′D' prevents this collapse by penalizing p(D=1∣w,c;θ)p(D = 1 \mid w, c; \theta) on noise samples.

  3. Knowl 3 — Noise Distribution for Negative Context Sampling

    model/method

    For each observed positive pair (w,c)∈D(w, c) \in D in Skip-gram with Negative Sampling, kk negative context tokens c1,…,ckc_1, \ldots, c_k are drawn to form negative pairs (w,cj)∈D′(w, c_j) \in D'. Each context word cjc_j is sampled independently from a smoothed unigram distribution raised to the 3/43/4 power:

    P(c)=pcontexts(c)3/4Z=(count(c))3/4∑c′∈C(count(c′))3/4P(c) = \frac{p_{\text{contexts}}(c)^{3/4}}{Z} = \frac{(\text{count}(c))^{3/4}}{\sum_{c' \in C} (\text{count}(c'))^{3/4}}

    where pcontexts(c)=count(c)∣Text∣p_{\text{contexts}}(c) = \frac{\text{count}(c)}{|Text|} is the empirical unigram probability of context cc across corpus TextText, CC is the context vocabulary, and Z=∑c′∈Cpcontexts(c′)3/4Z = \sum_{c' \in C} p_{\text{contexts}}(c')^{3/4} is the normalization constant. When all words appear as contexts, pcontexts(x)=pwords(x)p_{\text{contexts}}(x) = p_{\text{words}}(x). Raising the distribution to the 3/43/4 exponent increases the relative sampling frequency of rare words compared to standard unigram sampling.

  4. Knowl 4 — Convexity and Optimization Properties of SGNS

    theoretical result

    In the Skip-gram Negative Sampling (SGNS) objective:

    ∑(w,c)∈Dlog⁡σ(vc⋅vw)+∑(w,c)∈D′log⁡σ(−vc⋅vw)\sum_{(w,c) \in D} \log \sigma(v_c \cdot v_w) + \sum_{(w,c) \in D'} \log \sigma(-v_c \cdot v_w)

    if the target word embeddings {vw}w∈V\{v_w\}_{w \in V} are held fixed while only context embeddings {vc}c∈C\{v_c\}_{c \in C} are optimized (or conversely, if context embeddings are held fixed while word embeddings are optimized), the problem reduces to standard binary logistic regression and is convex. However, because the algorithm optimizes both word and context representations simultaneously, the joint optimization problem over all parameters θ={vw}w∈V∪{vc}c∈C\theta = \{v_w\}_{w \in V} \cup \{v_c\}_{c \in C} is non-convex.

  5. Knowl 5 — Geometric Motivation for Dual Word and Context Vector Spaces

    assumption

    The Skip-gram architecture maintains two separate vector representations for every vocabulary term: a target word vector vw∈Rdv_w \in \mathbb{R}^d and a context vector vc∈Rdv_c \in \mathbb{R}^d, requiring 2⋅∣V∣⋅d2 \cdot |V| \cdot d total parameters when the word and context vocabularies coincide.

    This separation is necessary to avoid a geometric inconsistency: if target words and contexts shared a single representation vector vv, identical self-context pairs (w,w)(w, w) would have dot product v⋅v=∥v∥2≥0v \cdot v = \|v\|^2 \ge 0. Because words rarely occur within their own immediate local context window, the model must assign a low probability to p(w∣w)p(w \mid w) (or low p(D=1∣w,w)p(D=1 \mid w, w)), which requires a low or negative dot product v⋅vv \cdot v. Assigning a low dot product to v⋅vv \cdot v is impossible under a single shared Euclidean vector parameterization.

  6. Knowl 6 — Dynamic Context Window Sizing in word2vec

    model/method

    In word2vec's Skip-gram implementation, the context window around each word token is dynamic rather than fixed. Given a user-defined maximum window radius parameter k∈N+k \in \mathbb{N}^+, for each token occurrence wiw_i at sentence position i∈{1,…,n}i \in \{1, \ldots, n\}, an effective window size k′k' is sampled uniformly at random from the discrete set {1,…,k}\{1, \ldots, k\}.

    The context set C(wi)C(w_i) extracted for word wiw_i is then defined as:

    C(wi)={wi−k′,…,wi−1,wi+1,…,wi+k′}C(w_i) = \{w_{i-k'}, \ldots, w_{i-1}, w_{i+1}, \ldots, w_{i+k'}\}

    Because smaller window radii are sampled alongside larger ones, context words located closer to the focus word wiw_i are included in the training objective with higher probability across the corpus than words near the maximum distance kk, implementing a linear distance decay without explicit weighting terms.

  7. Knowl 7 — Text Pre-Pruning and Subsampling Impact on Context Windows

    model/method

    In word2vec, vocabulary filtering and frequent-word subsampling are applied directly to the text stream before context window extraction occurs:

    1. Words appearing fewer than min-count\text{min-count} times across the corpus are pruned and omitted as both target words and contexts.
    2. High-frequency words are probabilistically removed according to a down-sampling formula parameterized by a threshold.

    Because pruned and subsampled words are deleted from the token sequence prior to context window formation, the effective text span spanned by a window of size k′k' expands across deleted positions. This expansion connects target words to informative, content-bearing context words that were originally linearly separated by frequent or rare tokens, increasing the topicality of the learned embeddings.

Coverage note — No substantial contributed material was omitted; the knowls cover the complete derivation of the SGNS objective, the negative sampling noise distribution, the binary classification formulation, convexity properties, dual-embedding representation assumptions, dynamic window sizing, and subsampling effects.

References

  1. 1.Tomas Mikolov, Kai Chen, Greg Corrado, and Jeffrey Dean. Efficient estimation of word representations in vector space. CoRR, abs/1301.3781, 2013.
  2. 2.Tomas Mikolov, Ilya Sutskever, Kai Chen, Gregory S. Corrado, and Jeffrey Dean. Distributed representations of words and phrases and their compositionality. In Advances in Neural Information Processing Systems 26: 27th Annual Conference on Neural Information Processing Systems 2013. Proceedings of a meeting held December 5-8, 2013, Lake Tahoe, Nevada, United States, pages 3111–3119, 2013.

Citation

MLA
Goldberg, Y., and O. Levy. “Word2vec Explained: Deriving Mikolov Et Al.'s Negative-sampling Word-embedding Method”. arXiv, 2014, http://arxiv.org/abs/1402.3722v1.
APA
Goldberg, Y., & Levy, O. (2014). word2vec Explained: deriving Mikolov et al.'s negative-sampling word-embedding method. arXiv. http://arxiv.org/abs/1402.3722v1
Chicago
Goldberg, Y., and O. Levy. 2014. “Word2vec Explained: Deriving Mikolov Et Al.'s Negative-sampling Word-embedding Method”. arXiv. http://arxiv.org/abs/1402.3722v1.
Harvard
Goldberg, Y. and Levy, O. (2014) “word2vec Explained: deriving Mikolov et al.'s negative-sampling word-embedding method”, arXiv [Preprint]. Available at: http://arxiv.org/abs/1402.3722v1.
Vancouver
1. Goldberg Y, Levy O (2014) word2vec Explained: deriving Mikolov et al.'s negative-sampling word-embedding method. arXiv

BibTeX

@article{goldberg2014word2vec,
  title = {word2vec Explained: deriving Mikolov et al.'s negative-sampling word-embedding method},
  author = {Goldberg, Yoav and Levy, Omer},
  year = {2014},
  journal = {arXiv},
  url = {http://arxiv.org/abs/1402.3722v1},
  eprint = {1402.3722}
}
Metadata:arXiv

Access the Paper

This paper is available from its original source. Click below to access the PDF.

Open PDF
License: Published with permission