From an index to a direction
In a bag of words every term is its own axis, so cat and dog are exactly as related as cat and tuesday.
An embedding replaces that axis with a dense vector of a few hundred numbers, learned so that words used in similar contexts end up pointing in similar directions.
The idea goes back to the linguist J. R. Firth: "You shall know a word by the company it keeps".
That is the distributional hypothesis, and every embedding method is a way of turning it into an objective function.
Skip-gram, the objective
- : the window, how many words on each side count as context.
The model is given a word and asked to predict its neighbours, which is a task nobody wants solved. The vectors learned on the way are the product.
Written with a softmax, that denominator sums over the whole vocabulary at every training step, which is hundreds of thousands of dot products per word.
Negative sampling, which made it practical
- : the number of negative samples, 5 to 20 for small corpora and 2 to 5 for large ones.
The multiclass problem is replaced by a binary one: separate the real neighbour from words drawn at random.
Negatives are drawn from the unigram distribution raised to the power three quarters, which samples rare words more often than their frequency alone would.
The exponent was found empirically and it matters, which is a fair summary of how much of this method was tuning.
One training run, in order
- Build the vocabulary and discard words below a minimum count.
- Subsample frequent words, since the thousandth example of the adds nothing.
- Slide a window over the corpus, taking each centre word and its neighbours as positive pairs.
- Draw negatives per pair and take a gradient step on the binary objective.
- Keep the input matrix as the embeddings and discard the output matrix.
What the geometry ends up containing
Directions turn out to carry relations, because a consistent difference in context produces a consistent difference in vector.
The result is genuine and oversold.
It holds for frequent words and clean relations, and the nearest neighbour is often one of the three input words, which is why the query usually excludes them.
The same mechanism encodes the corpus's biases as geometry, which is not a flaw in the method but a faithful measurement of the text it was trained on.
The limit that ended the era
One vector per word means one meaning per word, so the river bank and the savings bank are averaged into a single point that fits neither.
Contextual models produce a different vector for each occurrence, which is the whole difference between word2vec and BERT.