Visualized ML
Topics

D · Modern ideas

Embeddings

Represent each word (or item) as a learned vector, so that similar meanings end up pointing in similar directions.

Try it: D2 · Embeddings

Intuition

Neural networks compute with numbers, but much of the world comes as symbols: words, products, users, songs. The obvious encoding, one-hot (a vector with a 1 in the word's own position and 0 everywhere else), is huge and says nothing: every word is equally far from every other, "cat" no closer to "dog" than to "carburettor".

An embedding gives each symbol a short, dense vector instead, typically 50 to a few thousand numbers, and lets training choose the numbers. Symbols that behave alike end up with similar vectors, because that is what helps the network do its job. The vectors become a map of meaning: related words cluster together, and directions in the space can carry consistent meanings, such as "more royal" or "plural".

An illustrative 2-D map of word vectors (real ones have hundreds of dimensions). The arrow from "man" to "king" is copied to start at "woman" and lands next to "queen": the nearest word to king − man + woman. Fruits sit together, far from the people.

The math

A lookup table

For a vocabulary of \(V\) symbols and dimension \(d\), the embedding is a \(V \times d\) matrix \(E\) of weights. Symbol \(i\)'s vector is row \(i\):

\[ \mathbf{e}_i = E^\top \mathbf{x}_i = E_{i,:} \]

where \(\mathbf{x}_i\) is the one-hot vector. So an embedding is just a dense layer without bias or activation, applied to a one-hot input; implemented as an index lookup, it costs nothing. Its weights are trained by backpropagation like any others: only the rows of the symbols in a batch get gradients.

Learning from context

"You shall know a word by the company it keeps." Word2vec's skip-gram model trains each word's vector to predict the words around it. For a word \(w\) and a nearby word \(c\), with separate "context" vectors \(\mathbf{c}\), it raises

\[ \sigma(\mathbf{e}_w \cdot \mathbf{c}_c) \]

for pairs that really occur together and lowers it for random "negative" pairs. Words used in similar contexts, like "cat" and "dog", get pushed toward the same context vectors, and so toward each other.

Similarity

Closeness is measured by cosine similarity, \(\cos(\mathbf{a}, \mathbf{b}) = \mathbf{a}\cdot\mathbf{b} / (\lVert\mathbf{a}\rVert\lVert\mathbf{b}\rVert)\): the angle matters, not the length, which often tracks how frequent a word is.

Worked example. Toy 3-D vectors whose dimensions mean roughly (male, royal, female): king \((0.9, 0.8, 0.1)\), man \((0.8, 0.1, 0.1)\), woman \((0.1, 0.1, 0.9)\), queen \((0.1, 0.85, 0.9)\).

king − man + woman \(= (0.2, 0.8, 0.9)\).

Cosine similarity of the result with queen: 0.996; with woman: 0.818; with king: 0.617; with man: 0.333. The nearest word, excluding the three inputs as analogy solvers do, is queen.

Where embeddings are used

  • Language models. The first layer of every language model maps tokens to embeddings, and attention layers then refine each token's vector by its context: "bank" ends up differently in "river bank" and "bank loan".
  • Search and retrieval. Embed documents and queries with the same model, then find the nearest vectors. This "semantic search" matches meaning rather than exact words, and supplies context to AI assistants.
  • Recommendations. Users and items get embeddings whose dot product predicts a rating or a click.
  • Other modalities. Images, audio and molecules can be embedded too; models like CLIP put images and their captions in one shared space.

Pitfalls

  • Analogies are fragile. The famous examples are cherry-picked; many analogies fail, and the arithmetic only works when the input words are excluded from the answers.
  • Bias. Embeddings learn the associations in their training text, including stereotypes (gendered professions are a well-documented case), and pass them on to whatever uses them.
  • One vector per word. Static embeddings like word2vec give "bank" a single vector, blending its meanings. Contextual embeddings from transformers solve this.
  • Rare and unknown symbols. Rare words get poor vectors, and new words get none. Subword tokens (pieces like "un", "break", "able") let models embed any word.
  • Comparing across models. Each training run produces its own coordinate system. Vectors from different models, or different runs, cannot be compared directly.

History

The distributional idea comes from linguists Zellig Harris (1954) and John Rupert Firth (1957), whose line is quoted above. Yoshua Bengio and colleagues learned word vectors inside a neural language model in 2003. Tomas Mikolov and colleagues at Google made them famous with word2vec in 2013, including the king − man + woman example; GloVe (Pennington, Socher and Manning, 2014) followed. Since BERT (2018), contextual embeddings from transformers have become the standard.

Connections

Builds on

Leads to

Further reading