0004 β€” Vector Spaces, Bases & Embeddings

Linear Algebra Pillar Β· ~25 minutes

🧩 Puzzle Piece: When a token enters the transformer, it's converted into a vector via the embedding layer β€” a learned mapping from discrete tokens to points in a continuous vector space. But what does it mean to represent a word as a vector? This lesson answers that question by teaching you what a vector space is, what a basis does, and why learned embeddings are the foundation of everything a transformer understands.
"King" is not a vector. It's a word β€” a discrete symbol. Yet inside a transformer, "king" becomes a list of 768 numbers. How? And more importantly: why does that work?

Vector Spaces

A vector space is the set of all possible vectors of a given dimension. ℝ² is the 2D plane β€” every point (x, y) is a vector. ℝ⁷⁢⁸ is the 768-dimensional space where transformer embeddings live.

What makes something a vector space? Two properties:

  1. Closure under addition: v + w is still in the space
  2. Closure under scalar multiplication: cΒ·v is still in the space

That's it. These two properties mean you can navigate the space freely β€” any linear combination of vectors stays in the space.

Bases: Coordinate Systems for Vector Spaces

A basis is a set of vectors that lets you describe any vector in the space as a unique linear combination. In ℝ², the standard basis is:

e₁ = [1, 0]   eβ‚‚ = [0, 1]

Any vector [x, y] = xΒ·e₁ + yΒ·eβ‚‚. The coordinates (x, y) are relative to this basis. Choose a different basis, and the same point gets different coordinates.

Coordinates are not the vector β€” they're the vector's description in a particular basis. Change the basis, and the coordinates change, even though the underlying vector (the arrow in space) is the same. This distinction is critical for understanding embeddings: the embedding space's basis is learned, not given.

Embeddings: Learned Bases for Meaning

An embedding maps discrete items (words, tokens) to vectors in a continuous space. The embedding layer in a transformer is literally a matrix E of shape (vocab_size Γ— dmodel). Token i β†’ row i of E.

During training, the entries of E are adjusted so that:

The embedding space learns a basis where proximity equals semantic similarity. The dimensions aren't pre-labeled ("dimension 37 = royalty"). They emerge from training as the model discovers what representations make the prediction task easier.

Embeddings are the model's "understanding." Everything the transformer knows about language is encoded in how it places tokens in vector space. The rest of the transformer β€” attention, feed-forward layers β€” is about transforming these embeddings based on context. But the embedding layer is where raw symbols first acquire meaning.

Positional Encoding: Adding "Where" to "What"

Vectors alone don't encode order. [0.5, βˆ’0.2, …] doesn't say "I'm the third word." The transformer solves this by adding a position-dependent vector to each token embedding:

input_vector = embedding(token) + position_encoding(position)

The position encoding uses sine and cosine functions at different frequencies, creating a unique pattern for each position. This addition is why transformers can process all tokens in parallel β€” position information is baked into the vectors themselves, not into the order of processing.

Check Your Understanding

Q1: What does the embedding layer of a transformer actually do mathematically?

A. It runs a neural network on each token B. It looks up the token's ID in a matrix and returns the corresponding row as a vector C. It counts how many times each token appears D. It applies the attention formula to each token individually

Q2: Why does the transformer add positional encodings instead of processing tokens sequentially?

A. Sequential processing would be too slow β€” adding position vectors lets all tokens be processed in parallel B. The model can't do addition, so this is the only option C. Position is irrelevant β€” the encoding is just a historical artifact D. It's required by the GPU hardware

Q3: The embedding matrix E has shape (50,257 Γ— 768) for a typical GPT model. What does the number 50,257 represent?

A. The maximum context length in tokens B. The vocabulary size β€” the number of distinct tokens the model knows C. The number of attention heads D. The number of transformer layers
πŸ“Ί Primary Source Watch: Change of basis β€” 3Blue1Brown (Essence of Linear Algebra, Ch. 13)
πŸ“– Recommended Reading Strang, Linear Algebra and Its Applications β€” Sections 2.1–2.3 (Vector Spaces and Subspaces)
The idea that "coordinates depend on your choice of basis" is subtle but essential. If embeddings feel abstract, ask me to walk through a 2D analogy: imagine placing words on a 2D map based on meaning, and using (x,y) coordinates to represent them. The 768D embedding space works the same way β€” just with more dimensions.
← 0003: Matrix Multiplication Next: 0005 β€” Projections & Residuals β†’