Math Fundamentals · lesson 03/15
Vectors
A vector is an ordered list of numbers that you can add, scale, and compare. Every activation, every row of a weight matrix, and every token embedding in a transformer is a vector, so the basic operations below show up on every forward pass. The two you will use most are the dot product and the norm.
The idea
A vector in is written . Addition is element-wise, and scaling multiplies every entry by the same number. Those two operations are the whole of linear algebra's "linear" part.
Three quantities carry most of the meaning:
- Dot product — . It measures alignment. Positive means the vectors point roughly the same way, zero means orthogonal, negative means opposed.
- Norm — is the Euclidean length. The norm and the norm appear in regularization and clipping.
- Cosine similarity — . It ignores magnitude and lives in , which is why embedding search uses it: a long document and a short query can still be "similar".
Worked example
Let and .
- , and
- , so
Now take and . Their dot product is , so they are orthogonal — uncorrelated directions. The unit vector pointing along is , obtained by dividing by the norm.
In code
import numpy as np
a = np.array([3.0, 4.0])
b = np.array([1.0, 2.0])
print(a @ b) # 11.0
print(np.linalg.norm(a)) # 5.0
print((a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))) # 0.9838...
print(a / np.linalg.norm(a)) # [0.6 0.8]@ is the dot product for 1-D arrays. The same symbol becomes matrix multiplication for 2-D arrays, which is the subject of the next lesson.
Check yourself
- If , what geometric relationship holds, and what does that imply about information shared between the two feature directions?
- Compute and the unit vector in that direction.
- Why does cosine similarity tolerate different vector magnitudes while Euclidean distance does not?
Key takeaways
- A vector is a point and a direction; addition and scaling act entry by entry.
- The dot product measures alignment, the norm measures length.
- Cosine similarity is normalized dot product — the standard comparison for embeddings.