Sub topic 9
Negative Sampling
This chapter assumes you understand vectors, dot products, conditional probability, softmax, and gradient descent. It also assumes that a word2vec model creates positive (target, context) pairs from nearby words.
Why the full softmax is expensive
In skip-gram, the model receives a target word and predicts a context word. For example, from “the cat sits” and a suitable window, the pair (cat, the) is positive.
The model can produce a score for every word in the vocabulary. A softmax converts all scores into probabilities:
P(c \mid w) = \frac{\exp(s(w,c))}{\sum_{v \in V}\exp(s(w,v))}The denominator sums over the entire vocabulary. If there are millions of words, every training pair requires millions of scores and updates. This makes learning slow.
Negative sampling changes the task. Instead of distinguishing the correct context from every word, the model distinguishes one observed pair from a small number of randomly chosen incorrect pairs.
Turning prediction into binary classification
For a positive pair (cat, the), negative sampling creates examples such as:
| Target | Context | Label |
|---|---|---|
| cat | the | 1 |
| cat | banana | 0 |
| cat | engine | 0 |
The label is 1 for a real pair and 0 for a noise pair. The model scores a pair with the dot product of its target and context vectors:
s(w,c) = v_w \cdot u_cA sigmoid turns this score into a probability:
\sigma(s) = \frac{1}{1+\exp(-s)}For one positive pair and \(k\) negative samples, the objective is:
\log \sigma(v_w \cdot u_c)
+
\sum_{i=1}^{k}\log \sigma(-v_w \cdot u_{n_i})The first term rewards a high score for the real context. The other terms reward low scores for the sampled noise words. Only the target, positive context, and \(k\) negative contexts receive updates.
Choosing negative words
Noise words are usually sampled from a distribution based on word frequency. word2vec commonly raises each frequency to the power \(3/4\) before normalizing. This reduces the dominance of extremely common words while still sampling them more often than rare words.
The number \(k\) controls the trade-off:
- A small \(k\) makes training cheaper but gives a weaker learning signal.
- A larger \(k\) gives more negative evidence but increases computation.
- Common practical values range from 5 to 20, depending on the corpus and task.
Negative sampling works especially well with skip-gram because each target-context pair becomes a small, independent classification problem. CBOW can also use it after combining the context vectors to predict the target.
import numpy as np
def negative_sampling_loss(target, positive, negatives):
"""Return the loss for one target, one positive, and many negatives."""
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
positive_score = np.dot(target, positive)
negative_scores = np.array([np.dot(target, n) for n in negatives])
loss = -np.log(sigmoid(positive_score))
loss -= np.sum(np.log(sigmoid(-negative_scores)))
return float(loss)
rng = np.random.default_rng(1)
target = rng.normal(size=3)
positive = rng.normal(size=3)
negatives = rng.normal(size=(4, 3))
print(negative_sampling_loss(target, positive, negatives))Exercise
Why does negative sampling avoid computing a softmax over the whole vocabulary?
Show answer
It trains a binary classifier using one positive context and a small number of sampled negative contexts, so each update touches only a few words.