Tokens and Embeddings, lesson 6
The distributional hypothesis
Words used in the same contexts end up with similar vectors.
You can score two vectors with cosine similarity ("Cosine similarity and the dot product"). This lesson shows where the numbers in an embedding come from.
Part 1. Count the neighbours
Here's a tiny pile of text, six sentences long:
"the cat sat on the mat", "the dog sat on the rug", "the cat ate the fish", "the dog ate the meat", "the car drove on the road", "the bus drove on the road".
For each word, write down the words right next to it, one on each side. Skip "the", because it sits next to everything and would make every word look alike.
Example 1.1
Rows of neighbour counts
- cat sat on matdog sat on rugcat ate fishdog ate meatcar drove on roadbus drove on road
The sentences with "the" taken out.
- catsat 1ate 1dogsat 1ate 1cardrove 1
Each word's neighbours, counted. Each row is a vector, with one slot per word in the vocabulary. Most slots are 0.
- cosine(cat, dog)=1.0cosine(cat, car)=0.0
Cat and dog have identical rows, so their cosine is 1. Cat and car share no neighbours, so theirs is 0.
Context window
How far either side of a word you look for neighbours. Here it's 1. Real systems use somewhere from 2 to 10. A small window tends to group words that do the same job (cat, dog); a wide one also pulls in words from the same topic (cat, fish).
Your turn
Cat and dog never appear in the same sentence in that pile. Why do their rows match anyway?
Distributional hypothesis
The idea that words used in the same contexts tend to mean similar things. The linguist J. R. Firth put it this way in 1957: "You shall know a word by the company it keeps." Every method in this lesson leans on it.
Part 2. Squeeze the rows
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Squeeze the rows
- A guessing game
- Watch it learn
Tokens and Embeddings overviewSpotted a mistake? Tell us.