All courses
Courses › How LLMs really work · foundation

A matrix as a function

See a matrix as a linear map, XW as the same map applied to every row of X, and the identity matrix.

Builds on: Matrix multiplication

▶ Watch this lesson Start the whole course

What the animation says

A matrix is a machine

You can already multiply matrices and predict their shapes. Now a different question: what does multiplying by W actually do? Stop reading W as a table of numbers. Read it as a machine. A row of two numbers goes in. A row of three numbers comes out. Later, a model pushes every word's vector through the same machine at once. So let's open it up.

Rows are ingredients

Inside are the rows of W. Each row is three numbers long. Think of them as ingredients. The input is the recipe. Its first number says how much of row one to use, its second how much of row two. Take the input two, one. Two times row one gives two, zero, six. One times row two gives zero, two, one. Add them: two, two, seven. Now feed in zero, three. What comes out?

That's what linear means

None of row one, three of row two: zero, six, three. Now double an input. Two, one becomes four, two. The output doubles too: four, four, fourteen. Add two inputs. Two, one plus zero, three is two, four. And the outputs add: two, two, seven plus zero, six, three gives two, eight, ten. Scaling and adding pass straight through the machine. That is what linear means.

One map, every row

Now feed the machine a stack. X has three rows, so X times W has three rows. Row one, two, one, goes through and becomes two, two, seven. Row two, zero, three, goes through on its own and becomes zero, six, three. Same W, one row at a time. No row ever looks at its neighbours. Your turn: tap every row of X that output row three depends on, then check.

The machine that changes nothing

Output row three came only from row three of X: one, one became one, two, four. One special machine: the identity. It's square, with ones on the diagonal and zeros elsewhere. Its rows are one, zero and zero, one. So two, one gives two of the first row plus one of the second: two, one again. Nothing changes. Remember: a matrix is a linear function. It adds up its rows, weighted by the input, and applies that to every row on its own.

What does multiplying by a matrix do?

You can multiply matrices and predict shapes. But what does $xW$ actually **do** to $x$? Later, a model will push every word's vector through the same matrix at once. To reason about that, stop seeing $W$ as a table of numbers. See it as a machine: a vector goes in and a vector comes out. What kind of machine is it? And what happens when you feed it a whole stack of rows?

Rows of W are ingredients, x is the recipe

Let $W$ have shape $d_{in}\times d_{out}$. Each of its $d_{in}$ rows is an "ingredient" vector of length $d_{out}$. An input row $x$ says how much of each ingredient to use: $x_1$ of row 1, $x_2$ of row 2, and so on. The output $xW$ is that weighted sum. Double $x$ and the output doubles. Add two inputs and their outputs add. That is what **linear** means.

The matrix as a linear map

For a row vector $x$ of length $d_{in}$: $$xW=\sum_{i=1}^{d_{in}} x_i\,W_{i,:}$$ Here $W_{i,:}$ is row $i$ of $W$. Linear: for rows $a,b$, $(a+b)W=aW+bW$, and $(cx)W=c\,(xW)$ for any number $c$. For $X$ of shape $T\times d_{in}$, row $t$ of $XW$ equals (row $t$ of $X$)$W$. The same map is applied to each row, separately. Identity $I$: the square $d_{in}\times d_{in}$ matrix with 1s on the diagonal and 0s elsewhere. Its rows are the unit rows, so $xI=x$.

One map, applied row by row

$W=\begin{bmatrix}1&0&3\\0&2&1\end{bmatrix}$ maps 2 numbers to 3. Row $[2,1]$: $2\cdot[1,0,3]+1\cdot[0,2,1]=[2,2,7]$. Row $[0,3]$: $0\cdot[1,0,3]+3\cdot[0,2,1]=[0,6,3]$. So $X=\begin{bmatrix}2&1\\0&3\end{bmatrix}$ gives $XW=\begin{bmatrix}2&2&7\\0&6&3\end{bmatrix}$. Each row was transformed on its own. Identity: the $2\times 2$ $I$ has rows $[1,0]$ and $[0,1]$, so $[2,1]I=2[1,0]+1[0,1]=[2,1]$, unchanged.

Remember this

A $d_{in}\times d_{out}$ matrix $W$ is a linear function from length-$d_{in}$ rows to length-$d_{out}$ rows. The output is the rows of $W$, weighted by the input's entries. $XW$ applies that one function to every row of $X$ independently. The identity $I$ ($d_{in}\times d_{in}$) is the function that changes nothing.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.