Word2vec

Word2vec (Mikolov et al., 2013) learns a vector for each word from unlabeled text. It rests on one idea. Words that appear in similar contexts have similar meanings. Each word in the vocabulary gets two vectors in . These are a center vector and a context vector . The skip-gram model predicts the words near a center word . A word within a small window around gets the probability

Training maximizes over all pairs of a center word and a nearby word in the corpus.

The denominator sums over the whole vocabulary, so each pair costs . Negative sampling replaces it with binary questions. The true pair should get a high score. A few random words should get low scores:

The first term pulls toward . The second pushes it away from the random words. After training, the embedding of a word is the average of its two vectors, .

Intuition

The embedding of a word should place it near words with similar meanings. No labels say which words are similar. Word2vec gets the signal from text itself, which makes it a self-supervised method. “Cat” and “dog” both appear before “food”, after “the” and near “pet”. A model that predicts neighbors from the center word gives the two words similar vectors. Both must predict similar neighbors.

The Fall 2022 lecture 19 notes add a precise point. Two words get close vectors when they can replace each other in a sentence. Sitting next to each other is not enough. “Strong” and “coffee” appear together often, but they cannot swap places. “Strong” and “weak” can swap, so their vectors end up close, even though their meanings are opposite.

The same notes ask why the model predicts the neighbors from the center and not the reverse. With one input word and one output word, the prediction is simple. Predicting the center from several neighbors needs a way to combine the neighbors. The task is only a surrogate for learning vectors, so the simpler version comes first.

Properties

(Why Negatives Are Needed) With only true pairs, every score can grow without bound, and all vectors line up.

Suppose training maximizes over true pairs alone. Making all vectors equal and long gives every pair a score near and a probability near . Such vectors carry no information. The negative terms add a repulsive force. A random word is rarely a real neighbor of , so is pushed away from . Only words that keep appearing together stay aligned.

(Why Few Negatives) Using every wrong word as a negative would let the negatives dominate the loss, so training samples a few.

A sum over all wrong words brings back the cost of the softmax. It would also outweigh the single true pair. The Fall 2022 lecture 20 notes balance the two forces by sampling random words per true pair. With , and , a full softmax needs multiply-adds per pair. Negative sampling needs , about times fewer.

Not Taught in Lecture

Mikolov et al. (2013) use from to for small corpora and to for large ones. They draw negatives with probability proportional to a word’s count raised to the power .

(Gradient as Attraction and Repulsion) The gradient with respect to adds with weight and subtracts each with weight .

Use and . The gradient of the objective is

Each weight measures an error. If the true pair already scores high, its pull is weak. If a random word already scores low, its push is weak. The vectors and get gradients of the same form, with in place of .

(Analogies Emerge) Differences between word vectors capture relations such as gender and plurality, although no part of training asks for this.

The Spring 2023 lecture 21 notes give the example . The same difference also links “king” and “queen”. Plural forms differ from singular forms by a similar vector. To solve “man is to woman as uncle is to ?”, form . Then return the closest word by cosine similarity, leaving out the three input words. The Fall 2022 lecture 20 notes report that the answers mix good results with nonsense. The analogies are an emergent property, since training only predicts neighbors.

(Embeddings Copy the Biases of the Text) Word2vec learns every regularity of its corpus, including stereotypes.

The relations in the vectors come from how people write. If a corpus links some jobs mostly with one gender, the vectors do too. The Fall 2022 lecture 20 notes mention two responses. One removes harmful examples from the data. The other augments the data so that the vectors become invariant to such regularities. Neither fully solves the problem.

(One Vector per Word) Word2vec gives a word the same vector in every sentence, so it cannot separate the senses of a word.

“Bank” has one vector, which blends its meaning near “river” and near “loan”. A language model fixes this. It reads the whole sentence and outputs a different vector for each use. These contextual vectors led to models like BERT. Word2vec vectors can still initialize the embedding table of such a model. Tokens that are fragments of words have no word2vec vector, though.

(Choosing the Dimension) The dimension is a hyperparameter, chosen on held-out data.

The Spring 2023 lecture 21 notes give two ways to pick it. One measures the loss of the skip-gram task on held-out text. The other measures performance on a downstream task that uses the vectors, with cross-validation. The same notes report that two vectors per word work better in practice than one.

Examples

(Easy) Skip-Gram Probabilities

A vocabulary has three context words with , and . The center word has .

(Medium) One Step of Negative Sampling

A center word has . Its true neighbor has . Two sampled negatives have and .

(Hard) Solving an Analogy

A toy embedding has , and . Other words are , and .

See Also