Embeddings and Similarity
Representing words, images or items as vectors learned so that similar things point in similar directions, compared by cosine similarity.
Needs a step first: 34
A network can turn each token or item into a vector of a few hundred numbers, learned during training rather than designed by hand. Meaning becomes geometry: related things end up close, and directions can carry meaning, such as a consistent offset between singular and plural. Closeness is usually measured by cosine similarity, the dot product of unit vectors, which ignores length. Embeddings are the input to transformers, where attention scores are exactly such dot products, and they power search and recommendation by finding nearest neighbours.
Where it shows up: Token embeddings are what attention operates on in a transformer, and what a language model's softmax output layer compares against.
How to hold it: In your head
See meaning as direction in a space of vectors, compared by cosine similarity, which ignores length and keeps only the angle.
What knowing it looks like
- Compute the cosine similarity of two embedding vectors and rank neighbours by it.
- Explain why cosine similarity, not the plain dot product, is used to compare meanings.
- Describe how an embedding table is a weight matrix looked up by row and learned by backpropagation.
Test yourself
Three embeddings are , and , and . Compute the cosine similarity of with each of , and . Which is nearest to by cosine, and which by plain dot product?
Show the worked answer
Needs a step first: 34
Learn it with an AI
Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.
Be my maths tutor. I'm learning "Embeddings and Similarity" (Frontier tier): Representing words, images or items as vectors learned so that similar things point in similar directions, compared by cosine similarity. I already know: Neural Networks, Orthogonality and Projections. Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away. Finish by testing me with this: Three embeddings are $\vec a = (1, 2, 2)$, $\vec b = (2, 1, 2)$ and $\vec c = (-2, 1, 0)$, and $\vec d = (2, 4, 4)$. Compute the cosine similarity of $\vec a$ with each of $\vec b$, $\vec c$ and $\vec d$. Which is nearest to $\vec a$ by cosine, and which by plain dot product? Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.
Needs first
- Neural NetworksLayers of simple units, each a weighted sum passed through a nonlinear function, composed into a function that can fit almost anything.Frontier · Machine Learning
- Orthogonality and ProjectionsPerpendicular vectors, and how to split any vector into a part inside a subspace and a part at right angles to it.Undergraduate I · Linear Algebra
Leads to
On the way to
Recommended
- Neural Networks: Zero to HeroAndrej Karpathy · VideosFree lectures that build backpropagation, a language model and a small GPT from scratch in Python, explaining every line.
- Understanding Deep LearningSimon J. D. Prince (MIT Press) · BookA clear, well-illustrated 2023 textbook covering networks, training, transformers, diffusion models and reinforcement learning, free as a PDF with Python notebooks.
- 动手学深度学习阿斯顿·张、李沐等 · Book · 中文The Chinese edition of Dive into Deep Learning: a free textbook in which every model, from linear regression to transformers, runs as code beside the maths.
- 跟李沐学AI李沐 · Videos · 中文Mu Li's own Bilibili channel: the video course for Dive into Deep Learning and paragraph-by-paragraph readings of key papers, including the Transformer paper.