Unit 2 · Length, angle and the dot product
Lesson 16 · Available to read
Word embeddings preview
Words as vectors: king − man + woman ≈ queen. A first look at how language models represent meaning.
This unit has developed tools for measuring vectors: length, distance, the dot product, angles, projection and similarity. We close with a preview of how these tools are used to represent language.
A word embedding assigns each word a vector, typically with a few hundred components, learned automatically from large amounts of text. The guiding principle is that words appearing in similar contexts should receive similar vectors. As a result, "doctor" and "physician" have high cosine similarity, while "doctor" and "banana" do not.
Remarkably, differences between vectors can capture relationships. In a well-trained embedding, the displacement from "man" to "king" is approximately the same as the displacement from "woman" to "queen". Using vector arithmetic, king − man + woman lands close to queen; similarly, Paris − France + Italy lands close to Rome. To answer such an analogy, one computes the vector and then searches for the word whose embedding has the greatest cosine similarity to it.
A toy two-dimensional version makes the arithmetic explicit. Suppose one axis measures "royalty" and the other "gender": king = [0.9, 0.8], man = [−0.6, 0.8], woman = [−0.6, −0.8]. Then king − man + woman = [0.9, −0.8], which is where queen sits. Real embeddings are far less tidy, and the relationships hold only approximately.
Embeddings also absorb the patterns, including the social biases, of the text they are trained on, which is an active concern in AI. Unit 10 returns to embeddings inside modern language models.
The takeaway
Word embeddings represent words as vectors, so similarity and analogy become geometry.
Practice
0 of 5 attempted
- 1Calculate
Using the toy vectors king = [0.9, 0.8], man = [−0.6, 0.8] and woman = [−0.6, −0.8], compute king − man + woman.
Saved in this browser
- 2Recall
After computing an analogy vector, how is the answer word usually chosen?
Saved in this browser
- 3Multiple choice
How many components does a typical word embedding have?
- 4Explain in your own words
Why can a difference vector such as king − man represent a relationship?
Saved in this browser
- 5Apply
Write the vector expression you would use to find the capital of Italy from the pair Paris–France, and explain each step.
Saved in this browser