TL;DR
A word embedding gives every word a short list of numbers, a Vector. The numbers are not written by hand. They are learned from large amounts of text, adjusted until they help predict the words around each word. Words that are used in similar ways end up with similar vectors.
Once each word is a vector, arithmetic can work on text. Closeness and direction between vectors carry meaning: "cat" and "dog" end up close together, and "car" ends up far from both.
Where you'll see it: at the input of nearly every language model, in search and recommendation systems that match words by meaning, and in classic tools such as word2vec and GloVe.
Intuition
Words as points in a room
A computer sees a word as a string of letters, or as an index such as 4817 in a dictionary. Neither says anything about meaning. To a plain index, "cat" is as unrelated to "dog" as it is to "carburettor".
Picture a large room in which every word is a point. Place the points so that words that behave alike stand close together: animals in one corner, vehicles in another, numbers in a third. A word embedding is that placement, written down as coordinates. With 300 numbers per word, each word is a point in 300-dimensional space. You cannot picture that space, but the same geometry works in it.
Why "used alike" is the right signal
Linguists have long noticed that a word's neighbours tell you much of what it means. J. R. Firth put it as "you shall know a word by the company it keeps". Words that fit the same blank tend to mean similar things. "The ___ barked all night" fits dog, puppy and wolf. It does not fit carburettor.
An older approach counted co-occurrences into very long, mostly empty vectors. An embedding compresses that information into a short, dense vector, usually a few hundred numbers, where every number carries a little of everything.
flowchart LR A["word: dog"] --> B["index 4817"] B --> C["row 4817 of the table E"] C --> D["vector of 300 numbers"] D --> E["fed to the network"]
The math
The vocabulary and the one-hot vector
Let be the number of distinct words we keep, the vocabulary, and give each word an index . The one-hot vector for word is . It has zeros everywhere except a single 1 in position . It identifies the word exactly, but it carries no similarity: for two different words , , so every pair of words is equally unrelated.
The embedding matrix
Choose an embedding size , commonly between 50 and a few thousand. Store a matrix whose -th column is the embedding of word :
Multiplying by the one-hot vector simply picks out column . In practice a lookup does the same job faster. The matrix form matters for training: an embedding is a linear layer applied to a one-hot input, so its entries are weights that gradient methods can adjust like any other matrix weights.
Measuring closeness
The dot product is the usual starting point. Its size depends on the length of the vectors, so for meaning we usually use cosine similarity:
It lies between and and ignores length. Near 1 means the two vectors point the same way, near 0 means they are unrelated, and near means they point in opposite directions.
How the numbers are learned
The table starts as random numbers. Training picks a task that uses word vectors and adjusts to improve it. In the skip-gram version of word2vec (Mikolov et al., 2013), a centre word must predict the words around it. Each word gets an input vector (a column of ) and an output vector . The probability of context word given centre word is a Softmax over the vocabulary:
Training maximises over observed pairs. Its gradient with respect to is
Read it as: move toward the output vector of the word that really appeared, and away from the average output vector the model currently expects. The full sum over words is expensive for a large vocabulary, so word2vec replaces it with negative sampling: the model learns to tell the real pair from a few randomly drawn words.
Worked examples
Vocabulary: cat, dog, car. Embedding size . Suppose training produced:
- Dot product: .
- Lengths: . The same squares appear for dog, so .
- Cosine: .
- Dot product: .
- Length: .
- Cosine: .
Interpretation: in this toy, cat and dog point almost the same way (0.993), while cat and car point away from each other (-0.646). The geometry has put the animals on one side and the vehicle on the other.
Toy coordinates: the first number measures royalty, the second measures maleness.
- .
- Add woman: .
- This is exactly .
Interpretation: subtracting man removes maleness and leaves royalty, and adding woman supplies femaleness. Learned embeddings never line up this neatly. In word2vec the analogy holds only approximately: the word whose vector is closest by cosine to , excluding the three query words, is often "queen". Mikolov et al. reported examples of this kind.
Vocabulary of 50,000 words, embedding size .
- Entries in : .
- Stored as 32-bit floats (4 bytes each): bytes, about 60 MB.
- A one-hot vector of length 50,000 holds 50,000 numbers, nearly all zero. The 300-number embedding is about 170 times shorter: .
Interpretation: the whole table fits in ordinary memory, and one lookup costs almost nothing. Most of a large model's parameters sit elsewhere, but this table is already a substantial part of a small model.
Tradeoffs and failure modes
Static embeddings give one vector per word, whatever the sentence. That is cheap and easy to inspect, and it fails in predictable ways.
| Situation | What goes wrong | Better choice |
|---|---|---|
| A word with several meanings ("bank" the riverside and "bank" the lender) | One vector must cover every sense, so its neighbours are a blend | Contextual embeddings from a Transformer, which give each occurrence its own vector |
| Rare words, misspellings, names | Too few examples to learn a good vector; some words are missing entirely | Subword Tokenization, which splits unknown words into pieces that are known |
| Meaning of a whole sentence or document | Averaging word vectors loses word order and emphasis | A sentence encoder, or the pooled output of a transformer |
| Biased training text | The vectors absorb associations in the text. A 2016 study found gender stereotypes in word2vec neighbours | Audit neighbours before use, debias, or use better-curated data |
| One fixed table for every task | The vectors are not tuned to your task | Fine-tune the embedding table with the rest of the model |
Use static embeddings when you need a cheap, fast representation for search, clustering or a small classifier trained on limited data. Avoid them when the sense of a word depends on its context.
History
The idea goes back to the mid-1950s. Firth's remark about the company a word keeps came in 1957. In 1986 Geoffrey Hinton argued that concepts could be represented as patterns across many units, which he called distributed representations.
Count-based methods came first in practice. Latent semantic analysis, around 1990, factored a word-document count matrix into dense dimensions. Learned neural embeddings followed. In the early 2000s Yoshua Bengio and colleagues trained word vectors jointly with a neural language model, and the vectors were a by-product of predicting the next word.
Mikolov and colleagues released word2vec in 2013. It trained on very large corpora quickly, and its vectors showed the arithmetic analogies that made the idea famous. Pennington, Socher and Manning's GloVe (2014) combined counts with a learning objective. Both spread because the vectors were pretrained once, shared, and useful in other people's systems. From about 2018, contextual embeddings from transformer models such as BERT largely replaced static vectors for demanding tasks, though static embeddings remain common for simpler ones.
Check yourself
QuestionRecall: what is a one-hot vector, and why does it say nothing about similarity between words?Show answer
QuestionComputation: find the dot product and the cosine similarity of a = (3, 4) and b = (4, 3).Show answer
QuestionWhy: why does looking up row i of E give the same result as multiplying E by the one-hot vector for word i, and why does that equivalence matter for training?Show answer
QuestionApplied: you have 2,000 medical notes and need search to match 'heart attack' with 'myocardial infarction'. Would you train word vectors from scratch on these notes alone?Show answer
Sources
- Mikolov, Chen, Corrado, Dean, Efficient Estimation of Word Representations in Vector Space (2013): https://arxiv.org/abs/1301.3781
- Bengio, Ducharme, Vincent, Jauvin, A Neural Probabilistic Language Model (2003): https://jmlr.org/papers/v3/bengio03a.html
- word2vec (the paper node in this graph)
- Word embedding (Wikipedia): https://en.wikipedia.org/wiki/Word_embedding