Vector Semantics and Embeddings
Vector semantics represents a word's meaning as a point in space, derived from the company the word keeps. This first part builds the count-based side: the distributional hypothesis, co-occurrence matrices in their term-document and word-word forms, cosine as the similarity measure, and the two weightings — tf-idf and PPMI — that fix what raw counts get wrong.
╌╌╌╌
The n-gram model and naive Bayes share a limitation: both treat a word as an atom with no internal structure. Ongchoi, water spinach, and kangkong are three distinct symbols with nothing in common, even though they name the same vegetable. Under that view the only thing a model knows about a word is whether it is identical to another word. Every notion of similar meaning — that delicious and tasty are close, that cat and dog are closer than cat and theorem — is invisible.
Vector semantics repairs this. It represents each word as a point in a high-dimensional space, a vector, positioned so that words with similar meanings sit near each other.1 A classifier trained on delicious reviews can then generalize to a review that says tasty, because the two words land in the same neighborhood. Meaning is no longer a discrete label but a position in a geometric space.
The distributional hypothesis
Where does the geometry come from? The answer, from 1950s linguistics, is that meaning can be read off from use. Suppose you have never met the word ongchoi but you see it in these sentences:
Ongchoi is delicious sauteed with garlic. Ongchoi is superb over rice. ...ongchoi leaves with salty sauces...
and you have separately seen spinach sauteed with garlic over rice, chard stems and leaves are delicious, and collard greens and other salty leafy greens. The company ongchoi keeps — garlic, rice, delicious, salty, leaves — is the company those leafy greens keep. You can conclude that ongchoi is a leafy green vegetable, having never seen it on a plate.1 This is the distributional hypothesis: words that occur in similar contexts tend to have similar meanings, so a word's distribution over contexts is a usable proxy for its meaning.
The plan follows directly. Count, for each word, the contexts it appears in; store those counts as a vector; and let vector geometry stand in for meaning. Everything in this lesson builds on this idea.
Words and vectors: co-occurrence matrices
The raw material is a co-occurrence matrix, a table of how often words appear
together. Two versions matter, differing only in what a context
is.
The term-document matrix
In a term-document matrix each row is a word in the vocabulary and each column is a document; cell holds the number of times word occurs in document . Four words across four Shakespeare plays give a slice:2
| As You Like It | Twelfth Night | Julius Caesar | Henry V | |
|---|---|---|---|---|
| battle | 1 | 0 | 7 | 13 |
| good | 114 | 80 | 62 | 89 |
| fool | 36 | 58 | 1 | 4 |
| wit | 20 | 15 | 2 | 3 |
Read the matrix two ways. Each column is a document vector: As You Like It is , a point in a space whose dimensions are the four words. This is the vector space model of information retrieval — documents with similar word counts are similar documents, and a query is just another vector to compare against.2 Each row is a word vector: battle is , a point in a space whose dimensions are the four plays. Similar words occur in similar documents, so the row for fool and the row for wit — both high in the comedies, low in the histories — come out close.
The word-word matrix
For word meaning specifically, a finer context works better than a whole document. The word-word matrix (also term-term or term-context matrix) has both rows and columns labeled by words; cell counts how often context word occurred near target word — typically within a window of words across a training corpus.3 The matrix is . A hand-picked slice from Wikipedia counts:
| computer | data | result | pie | sugar | |
|---|---|---|---|---|---|
| cherry | 2 | 8 | 9 | 442 | 25 |
| strawberry | 0 | 0 | 1 | 60 | 19 |
| digital | 1670 | 1683 | 85 | 5 | 4 |
| information | 3325 | 3982 | 378 | 5 | 13 |
The rows already carry meaning. cherry and strawberry both live near pie and sugar, so their vectors point the same way; digital and information both live near computer and data, so they cluster elsewhere. In a real corpus runs from 10,000 to 50,000, and almost every cell is zero — these are sparse, long vectors.3
Cosine: measuring similarity
Two words are similar when their vectors point the same direction. The standard measure is the cosine of the angle between them, built from the dot product (inner product),
The dot product is large when the two vectors have big values in the same dimensions, and zero for orthogonal vectors with no shared dimensions. But used raw it is a poor similarity metric: it favors long vectors. Frequent words co-occur with many things, so their vectors have large entries and large dot products with everything, similarity or not.4 The fix is to divide out the lengths, where the length of a vector is . Dividing the dot product by both lengths gives exactly the cosine of the angle, since :
For the non-negative count vectors here the cosine runs from (orthogonal, share nothing) to (identical direction). Two vectors are more similar when the angle between them is smaller and the cosine is larger.
On the actual Wikipedia counts, while : the measure puts information far closer to digital than to cherry, as it should.4
Weighting: why raw counts mislead
Raw frequency is skewed and undiscriminating. The most frequent context words — the, it, they — sit near everything and so tell you almost nothing about which words are alike. Yet it is the words that appear in only a few, specific contexts — pie, say — that are informative about cherry. Raw counts get this backwards: they reward ubiquity. Two weighting schemes fix it, one per matrix type.5
TF-IDF, for term-document matrices
TF-IDF balances two opposing intuitions, and is the product of two terms.
The term frequency rewards a word for appearing often in a document, but with diminishing returns — a word seen 100 times is not 100 times more relevant than a word seen once — so we squash the count with a log:
The inverse document frequency penalizes a word for appearing in many documents. A word confined to a few documents discriminates them from the rest; a word in every document (like good across all 37 Shakespeare plays) discriminates nothing. With documents in the collection and the number containing term ,
A word in all documents gets ; a word in one gets the maximum. The tf-idf weight is their product,
Under tf-idf the dimension for good collapses to everywhere — it appears in every play, so its idf is zero — and the near-ubiquitous fool is sharply downweighted, leaving the discriminative words to carry the meaning.5
Worked example: tf-idf on the Shakespeare matrix
Take the collection of Shakespeare plays and weight the four-word slice above. First the idf column, from each word's document frequency. good appears in all plays, so and . fool appears in , giving . wit appears in , giving , and battle in , giving — the rarest of the four, so the most discriminative.
| term | df | |
|---|---|---|
| good | 37 | |
| fool | 36 | |
| wit | 34 | |
| battle | 21 |
Now the tf-idf cell for wit in As You Like It. The raw count is , so the squashed term frequency is , and the weight is . The same procedure over the whole slice replaces every count with a weight:
| As You Like It | Twelfth Night | Julius Caesar | Henry V | |
|---|---|---|---|---|
| battle | ||||
| good | ||||
| fool | ||||
| wit |
Two things happened. The good row is now zero everywhere — a word in every document carries no information about which document you are in, and its idf of enforces exactly that. And battle, once a near-invisible count of in As You Like It, is now the row with the largest weights, because its rarity across the collection makes each occurrence informative. The weighting inverted the raw ranking: frequency alone had good dominating, and tf-idf shifts the discriminative power to battle.5
PPMI, for word-word matrices
When the dimensions are context words rather than documents, the natural question is different: does word co-occur with context more than we would expect by chance? Pointwise mutual information measures exactly that — the log ratio of the joint probability to the product of the marginals:6
The numerator is how often and actually occur together; the denominator is how often they would if they were independent. A ratio above (positive PMI) means association; below (negative PMI) means the pair co-occurs less than chance. But negative PMI is unreliable — asserting that two words co-occur less than chance demands an enormous corpus to establish — so in practice we clip it to zero, giving positive PMI:
Given a co-occurrence matrix with counts , the probabilities are the maximum-likelihood estimates: , with and the row and column marginals. PPMI has one known flaw — it is biased toward rare events, giving very infrequent words inflated scores. The common fix raises the context probability to a power , which inflates for rare contexts and so damps their PMI.6 The same correction reappears inside word2vec.
Worked example: PPMI from co-occurrence counts
Carry the whole computation through on a five-context slice of Wikipedia counts, with the row and column marginals attached. Pretend, for arithmetic's sake, that these are the only words and contexts in the corpus, so the grand total is .6
| computer | data | result | pie | sugar | count(w) | |
|---|---|---|---|---|---|---|
| cherry | 2 | 8 | 9 | 442 | 25 | 486 |
| strawberry | 0 | 0 | 1 | 60 | 19 | 80 |
| digital | 1670 | 1683 | 85 | 5 | 4 | 3447 |
| information | 3325 | 3982 | 378 | 5 | 13 | 7703 |
| count(c) | 4997 | 5673 | 473 | 512 | 61 | 11716 |
Compute . The joint probability is the cell over the grand total, . The two marginals are and . Then
A small positive value: information and data co-occur slightly more than chance. Now a pair that co-occurs less than chance, cherry and computer: the joint is , the marginals are and , so . Negative, so PPMI clips it to rather than assert a co-occurrence deficit that cannot be reliably measured. Applying this to every cell turns the raw counts into the PPMI matrix:
| computer | data | result | pie | sugar | |
|---|---|---|---|---|---|
| cherry | |||||
| strawberry | |||||
| digital | |||||
| information |
The block structure the raw counts only hinted at is now explicit: cherry and strawberry carry all their weight on pie and sugar, digital and information on computer and data, and the cross terms are zeroed out. The two fruit rows are close, the two tech rows are close, and the two groups are orthogonal — similarity is now encoded in the geometry, through association rather than raw frequency.6
From counts to embeddings
The vectors built so far already work: cosine over a PPMI-weighted word-word matrix recovers real similarity, and every dimension is an interpretable context word. But they have two practical defects. They are long — one dimension per vocabulary word, so tens of thousands wide — and sparse, almost all zeros. The next lesson keeps the distributional idea but compresses it, learning a short, dense vector per word whose dimensions mean nothing individually yet capture similarity better than any count matrix. That is the move from counting contexts to predicting them.
This continues in Static Word Embeddings: word2vec and After.
Footnotes
- Jurafsky & Martin, Speech and Language Processing (3rd ed.), §6.2 — Vector Semantics: representing words as points in space so that similar words are nearby, motivated by the distributional idea (Firth, Harris, Joos) that a word's meaning is fixed by its distribution over contexts. ↩ ↩2
- Jurafsky & Martin, §6.3.1 — Vectors and Documents: the term-document matrix, its columns as document vectors in the vector space model of information retrieval, and its rows as word vectors over documents. ↩ ↩2
- Jurafsky & Martin, §6.3.3 — Words as Vectors, Word Dimensions: the word-word (term-context) co-occurrence matrix built from a -word window, and why such vectors are long and sparse. ↩ ↩2
- Jurafsky & Martin, §6.4 — Cosine for Measuring Similarity: the dot product as similarity, its bias toward long (frequent) vectors, and the length-normalized cosine, with the // worked example. ↩ ↩2
- Jurafsky & Martin, §6.5 — TF-IDF: Weighing Terms in the Vector: log-squashed term frequency times inverse document frequency, collapsing ubiquitous words like to weight zero. ↩ ↩2 ↩3
- Jurafsky & Martin, §6.6 — Pointwise Mutual Information: PMI as the log ratio of joint to independent probability, its unreliability for negative values, positive PMI, and the correction for rare-context bias. ↩ ↩2 ↩3 ↩4
╌╌ END ╌╌