07 · Embeddings
Where One Vector per Word Runs Out
A word with two meanings gets one row, and the row is an average.
One row for two meanings
The ten sentences use apple in one sense only: a fruit that is eaten, grows on a tree, and falls from it. Real text is not so tidy. Much of the text that mentions apple is about the company, and a table with one row per word has one row for both. Once text is lowercased, as the running corpus is, Apple the company and apple the fruit are the same string; and even with capitals kept, the fruit is capitalised too whenever it opens a sentence.
To see what happens to the row, add four sentences in which apple is the company:
company = [
"apple released a new phone",
"apple sold the new phone",
"the company apple released a laptop",
"the company apple sold the laptop",
]
Count contexts over the fourteen sentences the same way the distributional chapter did, whole sentence as the window, function words ignored. Split the count by which kind of sentence it came from, and apple turns out to have two rows that have nothing in common:
| context from | columns with a non-zero count | length |
|---|---|---|
| fruit sentences | king 1, ate 3, ripe 1, tree 2, fell 1 | 4.000 |
| company sentences | released 2, new 2, phone 2, sold 2, company 2, laptop 2 | 4.899 |
The cosine between the two is 0.000: no context word appears in both kinds of sentence. They are two different words that happen to be spelled the same. The table, though, only ever sees the spelling, and stores their sum.
The row is a weighted average of its senses
Adding the two rows gives the single apple row the table actually learns. Because the two sense rows share no columns, the cosine of the sum with either sense has a simple form: it is that sense's length divided by the length of the sum,
with
Vary the balance and the row slides between the two senses:
With no company sentences the row is the fruit, cosine 1.000. With one company sentence in five it is still mostly fruit, 0.918 against 0.281. With four company sentences to every fruit one it has cosine 0.980 with the company and only 0.200 with the fruit. Arora and colleagues showed that trained word2vec and GloVe vectors behave the same way: the vector of an ambiguous word is approximately a sum of one vector per sense, each weighted by how often that sense occurs, and the individual senses can be partly recovered from it.
What the average costs shows in the neighbours. Rank every word in the fourteen sentences by cosine with the apple row: the top six are released and sold (0.447), then new, phone, company and laptop (0.400), and only then ripe (0.335) and tree (0.258). A model that reads she ate a ripe apple gets, as its first impression of apple, a vector whose nearest neighbours are about phones. The minority sense is still in the row, but it has to be recovered from the context by whatever layers come after the lookup, and a model with no layers after the lookup cannot recover it at all.
Opposites keep the same company
The distributional hypothesis says that words used in the same contexts have similar meanings. The trouble is what "similar" is allowed to mean. Consider six sentences:
tea = ["the tea was hot", "the tea was cold",
"the soup was hot", "the soup was cold",
"the bath was hot", "the bath was cold"]
Count contexts for hot and cold and the two rows are identical: both appear once each with tea, soup and bath, and three times with was. Their cosine is 1.000. By the only evidence the method uses, hot and cold are the same word.
Real corpora are not quite this symmetric, but close. Antonyms fill the same slots in the same sentences, good and bad both describe films, buy and sell both take a price, and embeddings trained on context place them among each other's nearest neighbours. What the vectors capture is closer to "can be substituted here" than to "means the same". Substitutable words are often synonyms and often opposites, and the geometry cannot tell which.
This is not a defect a bigger corpus would fix. Opposites differ in exactly the respect that context does not record: which way the sentence turns out. Telling them apart takes a signal that does, such as a sentiment label, a dictionary of antonyms, or a model that reads the rest of the sentence and sees whether the tea was drunk or sent back.
The corpus's habits become the geometry
The first number in this book is a small example of something larger. king has a cosine of 0.314 with apple, while queen has 0.000. Nothing about kings makes them fruit-adjacent. One sentence, the king ate an apple, put them together, and no sentence put a queen and an apple together. Remove that one sentence and king and apple drop to 0.000, while king and queen become identical rows with cosine 1.000. Replace it with the queen ate an apple and the two numbers swap: queen and apple get 0.314, king and apple 0.000.
Scale that up and it becomes a well-documented problem. An embedding learns whatever regularities its corpus contains, including the ones nobody would endorse. If a profession is written about mostly with one pronoun, its vector moves towards that pronoun's direction. Bolukbasi and colleagues found gendered occupation pairs in word2vec vectors trained on news text and proposed removing a gender direction from words that should not have one. Caliskan, Bryson and Narayanan showed that the associations measured by psychologists' implicit-association tests reappear as cosine similarities in GloVe vectors trained on web text.
Two cautions keep this honest. First, measuring bias with analogies inherits every fragility of the analogy test, including the rule that excludes the input words; Nissim, van Noord and van der Goot showed that some widely quoted biased analogies are produced by that rule rather than by the vectors. Measurements that average many cosines at once, as the association tests do, depend less on any single choice of words than one analogy does. Second, removing a direction afterwards hides the association from that particular probe without necessarily removing it from the space; neighbouring words often still cluster the way they did. The reliable lesson is the one the king and the apple teach: the table is a record of the text, and it records what the text happened to say.
What the static table cannot do
The three problems in this chapter have one cause. The table gives each word a row, the row is fixed after training, and whatever the word means in a particular sentence has to be squeezed out of that fixed row by the reader. apple is an average of fruit and company, hot and cold are indistinguishable without the rest of the sentence, and the row for any word carries the accidents of the corpus with it.
The way out is to stop asking the table for the final vector. Keep the table as a starting point, then let the model read the sentence and adjust each word's vector to its surroundings, so that apple next to ripe and apple next to phone come out as different vectors. That is the idea behind contextual embeddings, and the next chapter builds one, one attention step at a time, from the same fourteen sentences.