Module 4 — Vectors & Data
Every input a model processes — an image, a sentence, a customer record — is represented as a vector: an ordered list of numbers. Once data is represented as vectors, the question "how similar are two items?" becomes a geometric question with a precise answer. This module develops vectors, the dot product, and the similarity measure by which a language model represents "king" and "queen" as related.
A vector as an ordered list and as a geometric object
A vector is an ordered list: \( \mathbf{x} = [x_1, x_2, \ldots, x_n] \). Each component is a feature — a measurable property. A house might be represented as \( [1500, 3, 2] \) (square feet, bedrooms, bathrooms); a word in a language model might be represented by 768 numbers encoding its meaning. With two features, a vector can be drawn as an arrow in the plane, which is used here to build geometric intuition.
Adjust the two arrows below; the length, dot product, and similarity update accordingly.
This activity needs JavaScript. The lesson below still covers everything.
Length (norm) of a vector
The norm \( \lVert \mathbf{x} \rVert \) is the length of the vector, given directly by the Pythagorean theorem:
It measures magnitude — how far the data point sits from the origin. Models often normalize vectors (scale them to length 1) so that comparisons are about direction, not size.
Adjust the vector's tip below. The dashed legs are \( x_1 \) and \( x_2 \); the arrow is the hypotenuse, so its length is \( \sqrt{x_1^2 + x_2^2} \). Click Normalize to scale the vector onto the unit circle — the same direction, with length 1.
This activity needs JavaScript. The norm is the arrow's length, \( \sqrt{x_1^2 + x_2^2} \); normalizing divides by that length to land on the unit circle (length 1, same direction).
The dot product
The dot product multiplies corresponding components and sums the results, yielding a single scalar:
It is large and positive when two vectors point in similar directions, zero when they are perpendicular (unrelated), and negative when they point in opposing directions. This scalar — a weighted sum — is precisely the quantity a single neuron computes, and it underlies search, recommendation, and the attention mechanism in transformers.
The geometry determines the sign: the dot product equals the projection of \( \mathbf{a} \) onto \( \mathbf{b} \), multiplied by \( \lVert \mathbf{b} \rVert \). Adjust the arrows and observe the projection change sign as the angle passes 90°.
This activity needs JavaScript. The dot product is the projection (shadow) of one vector onto the other, scaled by that vector's length — positive at acute angles, zero at 90°, negative beyond.
Cosine similarity: direction without size
To ask "do these point the same way?" while ignoring length, divide the dot product by both norms. The result is the cosine of the angle between them — cosine similarity:
It ranges from +1 (identical direction) through 0 (perpendicular) to −1 (opposite direction). The interactive plot above updates the value continuously; aligning the arrows drives it toward 1.
Below, set the angle and the two lengths independently. Changing the lengths leaves the cosine similarity unchanged; only the angle affects it. This is the meaning of "direction independent of magnitude."
This activity needs JavaScript. Cosine similarity depends only on the angle between two vectors, not their lengths: +1 at 0°, 0 at 90°, −1 at 180°.
Check your understanding
For each pair of vectors, determine the sign of their dot product and their approximate similarity.
This activity needs JavaScript.