How can a model learn that hotel and motel are related without being given a dictionary of synonyms? One answer is to learn from the contexts in which words occur. Word2Vec made this idea practical with small neural objectives and efficient training tricks.
From identity to geometry
A one-hot vector says which word we have, but not how it relates to another word. Every pair of distinct one-hot vectors has dot product zero. Dense embeddings instead assign each word a vector . Related contexts can lead to related representations.
Cosine similarity compares their directions:
Similarity is learned from the training objective; it is not guaranteed to match human semantic judgments for every word.
Skip-gram predicts nearby words
Take a center word and an observed context word . Skip-gram assigns them separate vectors and and scores the pair using .
A full-softmax model would estimate
The denominator sums over the vocabulary . With a large vocabulary, computing it for every observed center-context pair is expensive.
Negative sampling asks a different question
Instead of normalizing over all words, negative sampling trains a binary discriminator: is this center-context pair observed in the data, or sampled from a noise distribution?
For one positive pair and sampled negatives , a standard loss is
where . Positive scores are encouraged to rise; sampled negative scores are encouraged to fall. The sigmoid scores are not a normalized distribution over context words.
A common noise distribution uses a smoothed unigram frequency:
Compared with sampling proportional to raw counts, the exponent reduces the dominance of the most frequent words while retaining their relative prevalence.
Which negative contexts get sampled?
Toy skip-gram example: center word “cat”, one observed positive context, and K negatives drawn from q(w) ∝ count(w)p. Fixed illustrative dot-product scores are used; no embeddings are trained here.
Expected negative-sampling loss ≈ 4.616
This expectation uses independent draws from the displayed noise distribution, so a sampled “negative” can also be a genuine context elsewhere. The positive term and expected negative terms are evaluated at fixed scores; changing K changes the objective, not learned embeddings.
The widget holds example dot-product scores fixed and shows the expected negative-sampling loss as you vary the noise distribution and number of negatives. It does not train embeddings or visualize a learned embedding space; its purpose is to isolate the sampling objective.
A compact implementation of the objective
import torchimport torch.nn.functional as F
def negative_sampling_loss(center, positive, negatives): # center: [batch, dim] # positive: [batch, dim] # negatives: [batch, K, dim] positive_scores = (center * positive).sum(dim=-1) negative_scores = torch.einsum("bd,bkd->bk", center, negatives) return -( F.logsigmoid(positive_scores) + F.logsigmoid(-negative_scores).sum(dim=1) ).mean()This assumes that center, positive, and negatives have already been looked up from trainable embedding tables. In practice, one also chooses a context-window policy, negative-sampling distribution, subsampling of frequent words, and optimizer.
What do the vectors capture?
The objective rewards words that appear in similar contexts, so embeddings can encode semantic and syntactic regularities. But they also reflect corpus frequency, domain, and biases in the training text. A nearest-neighbor list is evidence of what the representation learned—not a definitive statement about language.
Negative sampling is a useful example of a general ML design pattern: change a difficult probability estimation task into a collection of easier discrimination tasks, then examine carefully what statistical object the new objective actually learns.
Further reading
- Mikolov et al., Efficient Estimation of Word Representations in Vector Space.
- Mikolov et al., Distributed Representations of Words and Phrases and their Compositionality.