Lesson 16: Embeddings and Representing Meaning
A computer cannot work with words directly; it needs numbers. The simplest way to turn a word into numbers is a list with a 1 in one position and 0 everywhere else, but that treats every word as equally unrelated to every other: "cat" is no closer to "dog" than to "democracy". Embeddings solve this by giving each word a list of numbers — a vector — in which similar words sit close together. In embedding space, "cat" is near "dog", "king" is near "queen", and directions in the space can carry meaning: the step from "man" to "woman" points in a similar direction to the step from "king" to "queen".
Embeddings are learned, not designed. During training, the model adjusts each word's vector so that words appearing in similar contexts end up near each other. That is the key insight: words that are used in similar ways tend to mean similar things, so a model that learns to predict text also learns a map of meaning. The transformer then refines these vectors with attention, so the same word gets a slightly different representation in different sentences — "bank" near a river is not "bank" near a cheque.
More Than Words
The embedding idea reaches beyond language. Images can be embedded too: a network can turn a picture into a vector, so that pictures of similar things sit close together. The same holds for sounds, products, or users. Once things are vectors, new operations become possible: find the nearest neighbours of a picture to recommend similar ones, or measure how close a search query is to a document. Much of modern machine learning is, in this sense, the art of learning good embeddings.
Embeddings also carry the biases of their training data. If the text a model read associates certain jobs more with one gender than another, the embedding space will reflect that, and the model may reproduce the association in its answers. This is not a flaw in the mathematics — it is a faithful mirror of the data. Spotting such reflections, and deciding what to do about them, is part of deploying models responsibly, which the next unit takes up.
A Worked Example
Imagine a worked example: you embed the words in a small collection of school texts. You find that "teacher", "lesson" and "homework" cluster together, while "goal", "match" and "team" form another cluster — the model has discovered topics without being told what a topic is. Then you check the direction from "doctor" to "nurse" against the direction from "man" to "woman" and find they line up more than they should. That single observation tells you two things: embeddings can reveal real structure, and they can also reveal the biases hiding in the text. Both are worth knowing before you build on them.
Embeddings in Everyday Products
Embeddings quietly power many products you use. When a music service recommends a song you like, it is often because the song's embedding sits near the embeddings of songs you already play. When a search engine finds a document that answers your question even though it shares no words with it, it is comparing the embedding of your query with the embeddings of its documents. When a photo app groups your pictures, it is clustering their embeddings. In each case the trick is the same: turn things into vectors, and let closeness in the vector space stand for similarity in the world.
This is also why embeddings are worth inspecting before you build on them. If the embeddings of job titles cluster by gender, a recommendation system built on them will reproduce that pattern. If the embeddings of neighbourhoods cluster by income, a service built on them may quietly serve some areas worse than others. The vectors are a map of the data's assumptions, and a careful builder reads the map before navigating by it.
Watch & Learn
One video to watch for this unit, from a trusted educator.
- Attention in transformers, step-by-step | Deep Learning Chapter 6 — 3Blue1Brown
Queries, keys, values and multiple heads, demystified step by step — the mechanism behind this unit.
Key Terms
- Embedding
- A list of numbers representing a word, image or other item, in which similar items sit close together.
- Vector
- A list of numbers; embeddings are vectors.
- Vector space
- The space in which embeddings live, where distance means similarity.
- Nearest neighbour
- The closest item in embedding space — the most similar item to a given one.
- Contextual embedding
- A word's representation after attention has adjusted it for the sentence it appears in.
- Bias in embeddings
- When an embedding space reflects unfair associations present in the training data.
Discussion Questions
- Why is a 1-in-one-position list a bad way to represent words? What does it fail to capture?
- If 'king' minus 'man' plus 'woman' lands near 'queen', what does that say about how meaning is stored?
- An embedding reflects its training data, including its biases. Is that a reason to avoid embeddings, or a reason to inspect them?
Practice Questions
- What is a word embedding?
- How are embeddings learned?
- What is a contextual embedding?
- In the worked example, what did the clusters of words show?
- Why can embeddings contain bias?
Show answers
- A list of numbers — a vector — representing a word, in which words with similar meanings sit close together.
- During training, the model adjusts each word's vector so that words appearing in similar contexts end up near each other. They are learned from the text, not designed by hand.
- A word's representation after attention has adjusted it for the sentence it appears in. The same word gets a different vector near a river than near a cheque.
- That the model had discovered topics — school words together, sport words together — without being told what a topic is.
- Because they are learned from real text, and real text contains associations — such as linking certain jobs more with one gender — that the embeddings faithfully reproduce.