Negative Sampling › Negative Sampling

Michael BrenndoerferApril 4, 20254 min read
Language AI HandbookAll chapters
Part 4 of 67Word Embeddings
Chapter 21Negative Sampling › Negative Sampling
Reading section 1 of 15 · Negative Sampling · 4 minSection 1 of 15 · 4 min

Book contents

Section 1 of 15 · 4 min read

Negative Sampling › Negative SamplingOne section · Sections in this chapter1 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The Skip-gram model learns powerful word representations, but it has a computational bottleneck: the softmax denominator. Every training step requires computing a sum over the entire vocabulary. Given a center word wtw_t, the probability of a context word wcw_c is:

P(wc∣wt)=exp⁡(wc′⋅wt)∑k=1Vexp⁡(wk′⋅wt)P(w_c | w_t) = \frac{\exp(\mathbf{w}'_c \cdot \mathbf{w}_t)}{\sum_{k=1}^{V} \exp(\mathbf{w}'_k \cdot \mathbf{w}_t)}

where:

  • P(wc∣wt)P(w_c | w_t): probability of context word wcw_c given center word wtw_t
  • wc′\mathbf{w}'_c: context embedding vector for word wcw_c
  • wt\mathbf{w}_t: center embedding vector for word wtw_t
  • VV: vocabulary size

For a vocabulary of 100,000 words, this means 100,000 dot products and exponentials per training step. With billions of training examples, full softmax becomes impractical. Training would take months instead of days.

Negative sampling solves this problem by reformulating the task as binary classification. Instead of predicting the exact probability of a context word, we ask: is this word pair a observed co-occurrence from the corpus, or a fake pair we constructed? This transformation reduces the computational cost from O(V)O(V) to O(k)O(k), where kk is a small constant (typically 5 to 20), making large-scale training feasible.

This chapter develops negative sampling from first principles. We'll derive the objective function, understand why the sampling distribution matters, implement efficient training, and see how this approximation achieves nearly the same quality as full softmax at a fraction of the cost.