0002 โ€” Matrices as Transformations

Linear Algebra Pillar ยท ~25 minutes

๐Ÿงฉ Puzzle Piece: In the transformer, the attention mechanism applies three matrix transformations to every token vector: Q (query), K (key), and V (value) projections. A matrix is a machine that takes a vector and produces a new vector โ€” transforming representation from one space to another. This lesson teaches you what that means.
You've seen vectors โ€” arrows in space. Now: what if you had a machine that could take any arrow, rotate it, stretch it, shear it, or flip it โ€” all with a single operation? That machine is a matrix.

What Is a Matrix?

A matrix is a rectangular grid of numbers. A 2ร—3 matrix has 2 rows and 3 columns:

A = โ”Œ โ”
   โ”‚ 2  1  0 โ”‚
   โ”‚ 0  3  1 โ”‚
   โ”” โ”˜

But a matrix isn't just a table of numbers. A matrix is a function โ€” it takes vectors as input and produces vectors as output. Specifically, multiplying a matrix by a vector applies a linear transformation to that vector.

Matrix ร— Vector: The Operation

To multiply a matrix A (mร—n) by a vector x (nร—1), each row of A takes a dot product with x:

โ”Œ โ” โ”Œ โ”   โ”Œ โ”
โ”‚ a b c โ”‚ โ”‚ x โ”‚  โ”‚ ax + by + cz โ”‚
โ”‚ d e f โ”‚ โ”‚ y โ”‚ = โ”‚ dx + ey + fz โ”‚
โ”” โ”˜ โ”‚ z โ”‚  โ”‚ gx + hy + iz โ”‚
          โ”” โ”˜  โ”” โ”˜

Each output component is a weighted combination of all input components. The matrix tells you how to mix the inputs to get the outputs.

Key insight: Matrix ร— vector = linear combination. Each row of the matrix defines a "recipe" โ€” how much of each input component to use. This is very close to what a neural network layer does: take inputs, apply weighted combinations, produce outputs. The "weights" in a neural network are literally the entries of a matrix.

What Transformations Look Like

In 2D, we can visualize what matrices do to the entire plane. Here are the fundamental transformations:

Matrix Transformation What It Does
โ”Œ2 0โ”
โ”‚0 2โ”‚
โ”” โ”˜
Uniform scaling Stretches everything by ร—2
โ”Œ2 0โ”
โ”‚0 1โ”‚
โ”” โ”˜
Axis scaling Stretches x-axis only โ€” squares become rectangles
โ”Œ0 โˆ’1โ”
โ”‚1 0โ”‚
โ”” โ”˜
90ยฐ rotation Rotates every vector counterclockwise by 90ยฐ
โ”Œโˆ’1 0โ”
โ”‚0 โˆ’1โ”‚
โ”” โ”˜
Reflection Flips everything through the origin
โ”Œ1 1โ”
โ”‚0 1โ”‚
โ”” โ”˜
Shear Tilts the plane โ€” vertical lines slant
โ”Œ1 0โ”
โ”‚0 0โ”‚
โ”” โ”˜
Projection Collapses everything onto the x-axis
Every linear transformation is a matrix. Every matrix is a linear transformation. This equivalence โ€” that a grid of numbers and a geometric operation are the same thing โ€” is one of the most powerful ideas in applied mathematics. It means you can think geometrically (rotate, stretch, shear) but compute algebraically (multiply rows by columns).

Why This Matters for the Transformer

In the attention mechanism, every token vector x goes through three matrix transformations:

q = WQ ยท x     k = WK ยท x     v = WV ยท x

Each W is a learned matrix โ€” its numbers are discovered during training. WQ transforms x into a "query" (what am I looking for?), WK into a "key" (what do I contain?), and WV into a "value" (what information do I carry?).

Same vector, three different transformations, three different purposes. The matrices are the model's knowledge โ€” they encode what the model has learned about how to represent and relate information.

When people say a model has "175 billion parameters," they're counting matrix entries. GPT-3's 175B parameters are literally the numbers inside its matrices. The matrices are the model. Understanding matrices as transformations means understanding what those 175 billion numbers actually do.

Matrix Multiplication Preview

What happens when you apply one transformation, then another? You get a composition of transformations โ€” and that composition is itself a matrix. Matrix ร— matrix = composition.

If B rotates and A scales, then AยทB first rotates then scales โ€” in one matrix. This is why matrix multiplication matters so much: it's how we combine transformations. That's the next lesson.

Check Your Understanding

Q1: In the attention mechanism, the matrix WQ transforms a token vector x into a query vector q. What is WQ, fundamentally?

A. A lookup table that maps tokens to queries B. A learned linear transformation โ€” a grid of numbers discovered during training C. A fixed mathematical constant like ฯ€ D. A random number generator

Q2: A 2ร—2 matrix with zeros everywhere except a 3 in the top-left position transforms the vector [1, 1]. What is the result?

A. [1, 1] B. [3, 0] C. [3, 3] D. [0, 0]

Q3: If a model has "7 billion parameters," what are those parameters?

A. Hyperparameters like learning rate and batch size B. The entries of the model's matrices โ€” the numbers that define its transformations C. The number of tokens in the training data D. The number of different tasks the model can perform
๐Ÿ“บ Primary Source Watch: Linear transformations and matrices โ€” 3Blue1Brown (Essence of Linear Algebra, Ch. 3) The best 10-minute geometric intuition for matrices as transformations you will ever see.
๐Ÿ“– Recommended Reading Strang, Linear Algebra and Its Applications โ€” Sections 1.3โ€“1.4 (Matrix Multiplication as Composition)
If the connection between "matrix as grid of numbers" and "matrix as transformation of space" isn't fully clicking, ask me. I can walk through specific examples โ€” pick any 2ร—2 matrix and I'll show you exactly what it does to the plane.
โ† 0001: Vectors โ€” The Atom of AI Data Next: 0003 โ€” Matrix Multiplication: The Engine โ†’