Chapter 1 of 5
Embeddings
A computer has to turn a word into numbers before it can do anything with it. The obvious way gives every word its own slot and makes every pair of words equally unrelated. So what does the better way actually buy you, and where does it still fall over?
The idea in plain words
Give each word a list of numbers, a few dozen long, and arrange those lists so that words used in similar ways end up pointing in similar directions. That is the whole trick. The arrangement is learned from counting which words appear near which other words across billions of words of text, so nobody has to write down what anything means.
Once meaning is a direction, questions about meaning become questions about geometry, and geometry you can measure. Which words point the same way as this one. What happens if I subtract one direction and add another. How far apart are these two.
The vectors on this page are the real thing:glove-wiki-gigaword-50, 400,000 words at 50 dimensions each, trained on Wikipedia and Gigaword. It is the same file the notebook loads in Part 1, so the numbers here and the numbers in the live demo are the same numbers.
Word Space Explorer
See 25,000 real word vectors at once, and find out who each word's neighbours are.
UMAP is not in this build. PCA is shown.
Loading the word vectors.
What to notice
- Turn on the group colours. Countries land near countries, numbers near numbers. Nobody told the model about those categories.
- Search for
python. Two lists come back: what your browser can see, and what the pipeline found over all 400,000. The full one isreticulated,spamalot,php,owl,mouse,reticulatus: two snake words, a Monty Python musical, a programming language and two animals, in a single ranking. Not one of them is about programming in the sense you probably meant. The word has several senses and exactly one vector to carry all of them. - Now search for
bankoruniversity. The two lists are identical, down to the scores. The smaller vocabulary in your browser costs you nothing on a common word; it only starts to matter out in the long tail, which is wherepythonlives. - Fifty dimensions squeezed into two loses a great deal. The first two PCA directions carry only about 12 percent of the variation between them, so two points that look close on screen may not be close in the full space. The neighbour list is computed in all 50 dimensions, so trust the list over the picture.
Go deeper: the equations, the shapes and the code
The projection is plain PCA, done the way the notebook does it: subtract the mean, take the singular value decomposition, keep the first two or three right singular vectors. Neighbours are cosine similarity computed in the full 50 dimensions and precomputed by the pipeline, 20 per word.
The map holds the first 25,000 lines of the GloVe file, which is ordered by corpus frequency, rather than all 400,000, because the whole file is a 40 MB download and that is not something to inflict on a phone. Any search you run here is over those25,000; anything wider comes from the pipeline, and says so.
Analogy Calculator
Do arithmetic on meaning, and see where the arithmetic stops working.
king - man + woman
Loading the word vectors.
What to notice
king - man + womangives queen, and that result is real. It is also the example everybody shows, which should make you suspicious.- Press the doctor probe. The answer says more about the text the model read than about doctors, and no amount of vector arithmetic fixes that.
- Try the harder cases. Some return something sensible, some return the nearest frequent word and nothing more. The ones that fail are not shown less often here than the ones that work.
Go deeper: the equations, the shapes and the code
Each input is scaled to unit length first, then the positives are added and the negatives subtracted, and the result is compared by cosine against every vector with the three input words excluded. That is what gensim's most_similar does, and excluding the inputs matters: without it the top answer to almost any analogy is one of the words you typed.
Scaling to unit length first matters too. Frequent words have longer vectors, so on raw vectors a common word pulls the sum towards itself and you get a different answer.
Cosine Playground
Measure the distance between two words three ways, and see why one hot vectors cannot.
Loading the word vectors.
What to notice
- Try
goodandbad. They score high, because opposites appear in the same kinds of sentences. Similar context is not the same thing as similar meaning. - Watch the dot product and the cosine disagree. The dot product rewards long vectors, which means it rewards frequent words. Cosine divides the lengths out and asks only about direction.
- The one hot table at the bottom is all zeros off the diagonal. That is not a measurement, it is what the representation forces.
Same Word, Different Vector
Watch one word split into its senses as it moves up through BERT's layers.
Loading the measured vectors.
What to notice
- Set the layer to the embedding layer. All four occurrences of the word are nearly identical, because nothing about the sentence has reached them yet. This is roughly what GloVe gives you.
- Now move to layer 12. The two senses pull apart, and the separation figure grows. Attention has mixed in the rest of the sentence.
- The separation is real but not total. Some pairs across senses still score higher than you might expect, which is a reminder that these are directions in a space and not labelled categories.
Go deeper: the equations, the shapes and the code
Each number is the cosine between two occurrences of the same word form, taken from a named hidden state of bert-base-uncased. The notebook uses the last hidden state, index 12. Index 0 is the embedding output, before the first attention layer runs.
These are measured and then read from a file. The browser is not running BERT: a 110 million parameter model is not a 300 KB page.
Go deeper
Two papers sit behind this chapter. Mikolov and colleagues in 2013 introduced word2vec and the analogy arithmetic. Pennington, Socher and Manning in 2014 introduced GloVe, which is what the vectors here come from, and which fits vectors directly to co occurrence counts rather than predicting neighbours. Both are listed with their sources on the resources page, which lands later this week.
The limitation this chapter ends on is what the rest of the session is about. One vector per word cannot represent a word whose meaning depends on its sentence, and nearly every interesting word is one of those. The fix is to let the sentence participate in building the vector, which needs a model that reads sequences, which is chapter 2.
Three questions before you move on
Nothing is scored or saved. Pick an answer and the reason appears either way.
Two words sit close together in the map. What does that actually tell you?
Why does the cosine playground show 0 for every pair of different one hot vectors?
In the four bank sentences, why does GloVe give the same vector every time?