1.2Vectors & dot products
Vectors are lists of numbers that double as arrows. The dot product measures how much two arrows agree, and it is the similarity score behind embeddings, search and attention.
Why AI runs on lists of numbers
A neural network can only do arithmetic. So everything it touches, whether a word, an image, a sound or a user’s taste in movies, must first become a list of numbers. That list is a vectorVectorAn ordered list of numbers. It can be read as an arrow in space or as a point with coordinates..
Some vectors are obvious. A pixel’s color is three numbers (red, green, blue). A house might be (square meters, bedrooms, age). Others are learned. In a language model, every token is mapped to a vector of a few thousand numbers called its embeddingEmbeddingA learned vector representing a token, word, image or document, arranged so that similar meanings get nearby vectors., and training arranges these so that tokens with related meanings get similar vectors. Once meanings are vectors, “how similar are these two words?” becomes a geometry question: do their arrows point the same way?
The tool that answers that question is the dot productDot productMultiply two vectors element by element and add up the results. It is large when the vectors point in similar directions.. It is probably the single most-used operation in all of deep learning. Every neuron computes one, every matrix multiplication is a grid of them, and attention, the heart of the Transformer, is built on them.
Hold a pencil at an angle above a table, with a lamp directly overhead. Its shadow on the table is long if the pencil lies nearly flat along the direction you care about, and shrinks to a dot if the pencil points straight up. If you tilt it backwards, the shadow points the other way.
The pencil is one vector, and the direction along the table is another. The shadow’s length is the projection, and the dot product is that shadow times the length of the second vector. Same direction: big positive shadow. Perpendicular: no shadow at all. Opposite: a negative shadow.
Two ways to see a vector
A vector like can be read two ways, and switching between them freely is the main skill in linear algebra:
- As a list: “3, then 1”. This is how a computer stores it, and it works in any number of dimensions. An LLM embedding might be a list of 4,096 numbers.
- As an arrow: start at the origin, go 3 right and 1 up. This is how you build intuition, and it works best in 2D or 3D.
The arrow has a length (also called its norm), given by Pythagoras: . And it has a direction. For meaning, the direction usually matters more than the length.
Adding and scaling vectors
Two operations come before the dot product, and both are entry by entry.
Scaling multiplies every entry by the same number: . As an arrow, it points the same way but is twice as long. Multiplying by flips it around. Dividing a vector by its own length gives a unit vector, length exactly 1, which keeps only the direction. This step is called normalizing, and you will see it everywhere in deep learning.
Adding two vectors adds matching entries: . As arrows, you place the second arrow’s tail at the first arrow’s tip and see where you end up. Subtraction works the same way, and is the arrow that points from to : the difference between two things, as a direction.
That last idea produced one of the most famous results in early embedding research. In word vectors learned by word2vec, the arithmetic lands closer to than to most other words. The difference “woman minus man” behaves roughly like a consistent direction in the space, and adding it to “king” moves you along it. Such analogies work only some of the time, and later research showed the effect is easy to overstate, but it was a vivid demonstration that directions in embedding space can carry meaning.
Inside a Transformer, addition matters even more. Each layer reads a vector, computes an update and adds it back, so a token’s representation is a running sum of contributions from every layer. That design, the residual stream, is only possible because vectors can be added.
The dot product
To take the dot product of two vectors, multiply matching entries and add:
For and : . That arithmetic recipe has a geometric meaning:
where is the angle between the arrows. So the dot product is positive when the arrows point roughly the same way (angle under 90°), zero when they are perpendicular, and negative when they point roughly opposite ways. It also grows with the lengths.
Divide out the lengths and you are left with just the angle information. That is cosine similarityCosine similarityThe dot product of two vectors divided by the product of their lengths: the cosine of the angle between them, from −1 to 1.:
A value of 1 means same direction, 0 means unrelated, and −1 means opposite.
Things to try:
- Make them perpendicular. Rotate until the angle reads 90°. The dot product is exactly 0 and the shadow vanishes.
- Push b past 90°. The dot product turns negative and the shadow (now red) points backward along ‘s line.
- “Same direction, 2× longer”. The dot product changes when you stretch an arrow, but the cosine stays at 1.00. Cosine only cares about direction.
- The word presets. In this invented 2D space, the first axis is roughly “animal-ness” and the second “food-ness”. “cat” and “dog” have cosine ≈ 0.99, “cat” and “pizza” only ≈ 0.20. “chicken”, which is both an animal and a food, sits in between and is fairly similar to “pasta” (≈ 0.85).
Projection: the shadow of one vector on another
The ochre bar in the demo is the projectionProjectionThe shadow of one vector on the direction of another: the part of b that points along a. Its signed length is a·b / |a|. of onto : how much of points along ‘s direction. Its signed length is . If has length 1 (a unit vector), the dot product is the shadow length.
This reading explains why a neuron is a dot product. A neuron holds a weight vector and computes for each input . In effect it asks how much does this input point in my favorite direction? A neuron whose weights point in the “this text is about sports” direction will respond strongly to sports-like inputs and weakly to everything else. You will build neurons like this in lesson 1.6.
From arrows to meanings
Real embeddings are not 2D, and their axes don’t have tidy names like “animal-ness”. They have hundreds or thousands of dimensions, and each meaningful property is typically spread across many of them. But the geometry carries over exactly: the dot product and cosine are computed the same way in 4,096 dimensions as in 2, and “similar meaning” still shows up as “small angle”.
For a sense of scale: the classic pretrained word2vec vectors released with the 2013 paper had 300 dimensions, popular sentence-embedding models today output a few hundred to a few thousand, and the internal vectors of large LLMs are often several thousand entries long. Nobody can picture a 4,096-dimensional arrow, and nobody needs to. You compute the dot product the same way, and the 2D intuition (same direction means similar, perpendicular means unrelated) remains a reliable guide.
This is how a lot of practical AI works:
- Semantic search and RAG. Turn a question and every document into vectors, then return the documents with the highest cosine similarity to the question. See vector search.
- Attention. Inside a Transformer, each token produces a “query” vector and a “key” vector, and the dot product of a query with each key decides how much attention to pay. See self-attention.
- Recommendations. Represent each user and each movie as a vector; the predicted rating is their dot product. A user who likes action films has a vector pointing in roughly the same direction as action movies.
- Next-token prediction. An LLM’s final step takes the dot product of its internal state with every token’s vector to score which token comes next.
MATH CORNER · optionalWhy the two formulas agree, and a worked example
Why . Consider the triangle with sides , and . The law of cosines says
Expanding the left side entry by entry gives . Matching the two expressions, .
Worked example. , .
| quantity | computation | value |
|---|---|---|
| 5 | ||
| 3.162 | ||
| 2.236 | ||
| 0.707 | ||
| 45° | ||
| shadow length | 1.581 | |
| projection vector | (1.5, 0.5) |
You can set this exact pair in the demo (with snapping on) and check every number.
In high dimensions almost all pairs of random directions are nearly perpendicular: for random vectors with entries, the cosine is typically on the order of . In 4,096 dimensions that is about 0.016. This is why a cosine of even 0.3 between two embeddings can be a strong signal: random chance would give nearly zero.
In code
import numpy as np
a = np.array([3.0, 1.0])
b = np.array([1.0, 2.0])
dot = a @ b # 5.0 (same as np.dot(a, b))
cos = dot / (np.linalg.norm(a) * np.linalg.norm(b))
print(dot, cos, np.degrees(np.arccos(cos))) # 5.0 0.7071 45.0
# Cosine similarity of one query against many embeddings at once
E = np.random.randn(10_000, 768) # 10k fake 768-dim embeddings
E /= np.linalg.norm(E, axis=1, keepdims=True) # normalize rows to length 1
q = E[42] + 0.02 * np.random.randn(768) # a noisy copy of row 42
q /= np.linalg.norm(q)
scores = E @ q # 10k dot products in one go
print(scores.argmax()) # 42
The last part is semantic search in miniature: one matrix–vector product computes thousands of similarities at once, which is exactly the kind of work GPUs are built for.
- “A bigger dot product always means more similar.” The dot product also grows with length. Two long vectors at 60° can have a bigger dot product than two short ones at 0°. Use cosine similarity to compare direction alone.
- “A dot product of zero means the vectors are zero.” It means they are perpendicular (or one of them is zero). Perpendicular vectors are “unrelated” in direction, not empty.
- “Each dimension of an embedding means one human concept.” Real embedding dimensions are rarely that clean. Concepts are usually spread across many dimensions; the “animal-ness” axis in the demo is a teaching simplification.
- “Vectors only make sense in 2D or 3D.” Every formula here works unchanged in any number of dimensions. You just can’t draw them.
Check your understanding
Q1.What is the dot product of (2, −1, 3) and (1, 4, 2)?
Q2.Two vectors have a dot product of 0. What can you conclude, assuming neither is the zero vector?
Q3.You double the length of vector b without changing its direction. What happens?
Q4.Why is cosine similarity popular for comparing embeddings?
Further reading
- videoEssence of Linear Algebra, chapter 9: Dot products and duality (3Blue1Brown) — The best visual explanation of why the arithmetic and geometric definitions agree.
- paperEfficient Estimation of Word Representations in Vector Space (word2vec) — The 2013 paper that made learned word vectors and cosine similarity mainstream.
- blogThe Illustrated Word2vec (Jay Alammar) — A gentle, picture-heavy tour of word embeddings and similarity.
- bookMathematics for Machine Learning, Chapter 3: Analytic Geometry — Norms, inner products, angles and projections, done properly. Free PDF.