Deep-dives on transformers, autograd, and LLM math — read the blog

← Blogs

Linear Algebra for Machine Learning: The Series That Made It Click

A complete intuitive introduction to vectors, matrices, dot products, determinants, eigenvectors, basis changes, and how linear algebra powers machine learning.

AI from Scratch Part 1 of 15
View as Markdown

I used to treat linear algebra like a checklist: learn the definition, memorize the formula, move on. It worked well enough for passing quizzes, but when I started reading ML papers and implementing models, the notation still felt disconnected from anything real.

That changed after I watched 3Blue1Brown’s Essence of Linear Algebra.

What is linear algebra, really?

Linear algebra is the math of lists of numbers and machines that update those lists in a predictable way. A vector can be an arrow on a plane, but in machine learning it is more often a description of something: a house might be $x=[1200,3]$ for square feet and bedrooms; a token might be a list of 768 learned features. A matrix is the machine: feed it a vector, get another vector out. Grant Sanderson’s series made that picture stick for me — a matrix is not a grid of symbols, it is a transformation of space that stretches, rotates, or shears every input direction. Once I held that image, eigenvalues, determinants, and change of basis stopped feeling like magic notation and started feeling like consequences of the same idea.

flowchart LR
  V["Input vector x"] --> M["Matrix A"]
  M --> O["Output vector Ax"]
  O --> D["Dot products measure alignment"]
  O --> E["Eigenvectors = directions that only scale"]

The rest of this post walks one small $2\times2$ example from start to finish — vectors, matrix multiply, dot product, determinant, eigenvalue — with every arithmetic step written out. I will come back to where these ideas show up in models like the ones I am building in AL-1.

What is a vector?

A vector is an ordered list of numbers. In two dimensions I write

$$ x = \begin{bmatrix} 4 \\ 5 \end{bmatrix}. $$

The first entry is how far I move along the horizontal axis; the second is how far along the vertical axis. I can read the same object as an arrow from the origin to the point $(4,5)$, or as a list of features. Both views are the same data; geometry gives me intuition, lists give me code.

How does a matrix transform a vector?

A matrix packages several output directions at once. I will use the same matrix throughout this post:

$$ A = \begin{bmatrix} 2 & 1 \\ 0 & 3 \end{bmatrix}. $$

Row 1 says: “to build the first output coordinate, take $2$ times the first input plus $1$ times the second.” Row 2 says: “take $0$ times the first input plus $3$ times the second.” Applying $A$ to $x=[4,5]^T$ means computing each output entry separately.

First output coordinate:

$$ (Ax)_1 = 2 \cdot 4 + 1 \cdot 5 $$
$$ (Ax)_1 = 8 + 5 $$
$$ (Ax)_1 = 13 $$

Second output coordinate:

$$ (Ax)_2 = 0 \cdot 4 + 3 \cdot 5 $$
$$ (Ax)_2 = 0 + 15 $$
$$ (Ax)_2 = 15 $$

So the transformed vector is

$$ Ax = \begin{bmatrix} 13 \\ 15 \end{bmatrix}. $$

Geometrically, the point $(4,5)$ lands at $(13,15)$. I can also read the same calculation in column language: the columns of $A$ are where the basis vectors $\hat{\imath}=[1,0]^T$ and $\hat{\jmath}=[0,1]^T$ get sent. The input $[4,5]^T$ says “take 4 copies of the first column plus 5 copies of the second”:

$$ Ax = 4 \begin{bmatrix} 2 \\ 0 \end{bmatrix} + 5 \begin{bmatrix} 1 \\ 3 \end{bmatrix}. $$

Neural-network layers usually think in the row view (weighted sums into each neuron); animations usually think in the column view (where basis arrows go). Same multiplication, two pictures.

One complete example: vectors through eigenvalues

I want one thread of numbers I can follow without switching examples. I keep $A=\begin{bmatrix}2&1\0&3\end{bmatrix}$ and $x=\begin{bmatrix}4\5\end{bmatrix}$ from above, then ask what else this same matrix tells me.

Matrix multiply (recap)

$$ x = \begin{bmatrix} 4 \\ 5 \end{bmatrix}, \qquad A = \begin{bmatrix} 2 & 1 \\ 0 & 3 \end{bmatrix} $$
$$ (Ax)_1 = 2(4) + 1(5) = 13, \qquad (Ax)_2 = 0(4) + 3(5) = 15 $$
$$ Ax = \begin{bmatrix} 13 \\ 15 \end{bmatrix} $$

Dot product on two directions from the same setup

Take the first column of $A$ as vector $a$ and a separate direction $b$:

$$ a = \begin{bmatrix} 2 \\ 0 \end{bmatrix}, \qquad b = \begin{bmatrix} 3 \\ 4 \end{bmatrix} $$

The dot product multiplies matching entries and adds:

$$ a \cdot b = 2 \cdot 3 + 0 \cdot 4 $$
$$ a \cdot b = 6 + 0 $$
$$ a \cdot b = 6 $$

Length of $a$:

$$ \|a\| = \sqrt{2^2 + 0^2} $$
$$ \|a\| = \sqrt{4} $$
$$ \|a\| = 2 $$

Length of $b$:

$$ \|b\| = \sqrt{3^2 + 4^2} $$
$$ \|b\| = \sqrt{9 + 16} $$
$$ \|b\| = \sqrt{25} $$
$$ \|b\| = 5 $$

The geometric formula $a\cdot b = |a||b|\cos\theta$ gives:

$$ 6 = 2 \cdot 5 \cdot \cos\theta $$
$$ \cos\theta = \frac{6}{10} $$
$$ \cos\theta = 0.6 $$

A large positive dot product means the vectors point roughly the same way; zero would mean perpendicular; negative would mean they face opposite directions. That is why dot products score similarity in search, recommendations, embeddings, and transformer attention.

Determinant: how much area scales

Before inverses and eigenvalues, I check whether $A$ squashes space. For a $2\times2$ matrix,

$$ \det(A) = (2)(3) - (1)(0) $$
$$ \det(A) = 6 - 0 $$
$$ \det(A) = 6 $$

A unit square becomes a parallelogram with six times the area. If $\det(A)=0$, some direction collapses to a line or point and information is lost forever — no inverse can undo that.

Eigenvalues: which directions only get stretched

An eigenvector $v$ is a direction $A$ does not rotate — it only scales by some number $\lambda$:

$$ Av = \lambda v. $$

I find $\lambda$ by solving $\det(A - \lambda I)=0$.

$$ A - \lambda I = \begin{bmatrix} 2-\lambda & 1 \\ 0 & 3-\lambda \end{bmatrix} $$
$$ \det(A - \lambda I) = (2-\lambda)(3-\lambda) - (1)(0) $$
$$ \det(A - \lambda I) = (2-\lambda)(3-\lambda) $$

Set the determinant to zero:

$$ (2-\lambda)(3-\lambda) = 0 $$

So one eigenvalue is

$$ \lambda_1 = 2 $$

and the other is

$$ \lambda_2 = 3 $$

Eigenvector for $\lambda_1 = 2$. Solve $(A - 2I)v = 0$:

$$ A - 2I = \begin{bmatrix} 0 & 1 \\ 0 & 1 \end{bmatrix} $$
$$ \begin{bmatrix} 0 & 1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} $$

The first row gives

$$ 0 \cdot v_1 + 1 \cdot v_2 = 0 $$
$$ v_2 = 0 $$

Pick $v_1 = 1$:

$$ v = \begin{bmatrix} 1 \\ 0 \end{bmatrix} $$

Check:

$$ Av = \begin{bmatrix} 2 \cdot 1 + 1 \cdot 0 \\ 0 \cdot 1 + 3 \cdot 0 \end{bmatrix} $$
$$ Av = \begin{bmatrix} 2 \\ 0 \end{bmatrix} $$
$$ Av = 2 \begin{bmatrix} 1 \\ 0 \end{bmatrix} $$
$$ Av = \lambda_1 v \quad \checkmark $$

Eigenvector for $\lambda_2 = 3$. Solve $(A - 3I)v = 0$:

$$ A - 3I = \begin{bmatrix} -1 & 1 \\ 0 & 0 \end{bmatrix} $$
$$ \begin{bmatrix} -1 & 1 \\ 0 & 0 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} $$

The first row gives

$$ -1 \cdot v_1 + 1 \cdot v_2 = 0 $$
$$ v_2 = v_1 $$

Pick $v_1 = 1$:

$$ v = \begin{bmatrix} 1 \\ 1 \end{bmatrix} $$

Check:

$$ Av = \begin{bmatrix} 2 \cdot 1 + 1 \cdot 1 \\ 0 \cdot 1 + 3 \cdot 1 \end{bmatrix} $$
$$ Av = \begin{bmatrix} 3 \\ 3 \end{bmatrix} $$
$$ Av = 3 \begin{bmatrix} 1 \\ 1 \end{bmatrix} $$
$$ Av = \lambda_2 v \quad \checkmark $$

That is the full chain on one matrix: input vector, multiply, dot product for alignment, determinant for area scaling, eigenpairs for preserved directions. When I apply $A$ many times, an eigenvector direction grows or shrinks by $\lambda^k$ — which is why eigenvalues matter for stability, PCA, and worrying about exploding or vanishing signals in deep networks.

How do dot products measure alignment?

The numeric thread above already computed $a\cdot b=6$. The extra idea is projection: how much of one vector lies along another. For $a=[2,1]$ and $b=[3,4]$ (a different pair, same mechanics),

$$ a \cdot b = 2 \cdot 3 + 1 \cdot 4 $$
$$ a \cdot b = 6 + 4 $$
$$ a \cdot b = 10 $$
$$ a \cdot a = 2^2 + 1^2 $$
$$ a \cdot a = 4 + 1 $$
$$ a \cdot a = 5 $$

Projection of $b$ onto $a$:

$$ \operatorname{proj}_a(b) = \frac{a \cdot b}{a \cdot a}\, a $$
$$ \operatorname{proj}_a(b) = \frac{10}{5} \begin{bmatrix} 2 \\ 1 \end{bmatrix} $$
$$ \operatorname{proj}_a(b) = 2 \begin{bmatrix} 2 \\ 1 \end{bmatrix} $$
$$ \operatorname{proj}_a(b) = \begin{bmatrix} 4 \\ 2 \end{bmatrix} $$

Least-squares regression is the same picture in higher dimensions: when no exact solution exists, project the target onto the space of predictions the model can actually produce.

How does matrix multiplication compose transformations?

If matrix $B$ acts on $x$ first and matrix $A$ acts on the result, the combined effect is $ABx$ — rightmost matrix hits $x$ first. Order matters: rotate then stretch is not generally the same as stretch then rotate, so $AB \neq BA$ in most cases.

Shapes encode the contract. If $B$ is $3\times2$ and $x$ is $2\times1$, then

$$ Bx \text{ has shape } 3 \times 1. $$

Matrix $A$ multiplying that result must have three columns. Dimension matching is not bureaucratic bookkeeping; it says the output space of one map must equal the input space of the next.

As a tiny composition with the same $A$, let

$$ B = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} $$

(swap the two coordinates). Entry $(AB)_{11}$:

$$ (AB)_{11} = 0 \cdot 2 + 1 \cdot 0 $$
$$ (AB)_{11} = 0 $$

Entry $(AB)_{12}$:

$$ (AB)_{12} = 0 \cdot 1 + 1 \cdot 3 $$
$$ (AB)_{12} = 3 $$

Entry $(AB)_{21}$:

$$ (AB)_{21} = 1 \cdot 2 + 0 \cdot 0 $$
$$ (AB)_{21} = 2 $$

Entry $(AB)_{22}$:

$$ (AB)_{22} = 1 \cdot 1 + 0 \cdot 3 $$
$$ (AB)_{22} = 1 $$

So

$$ AB = \begin{bmatrix} 0 & 3 \\ 2 & 1 \end{bmatrix}. $$

Applying to $x=[4,5]^T$ means swap first ($Bx=[5,4]^T$), then apply $A$ — one pipeline, one matrix product.

What do determinants and inverses tell you?

I already found $\det(A)=6$. When $\det(A)\neq 0$, an inverse exists that undoes the transformation. For this $A$,

$$ A^{-1} = \frac{1}{\det(A)} \begin{bmatrix} 3 & -1 \\ 0 & 2 \end{bmatrix} $$
$$ A^{-1} = \frac{1}{6} \begin{bmatrix} 3 & -1 \\ 0 & 2 \end{bmatrix} $$

Solving $Ax=b$ is $x = A^{-1}b$ in principle. In real code I use a numerical solver instead of forming the inverse explicitly — faster and more stable — but the picture is “run the transformation backward.”

What is a change of basis?

A basis is the coordinate language I use to describe vectors. The physical arrow does not move when I change basis; only the numbers describing it change. If the columns of $P$ are new basis vectors expressed in the old coordinates, then

$$ x = P[x]_{\text{new}} $$

and

$$ [x]_{\text{new}} = P^{-1}x. $$

Fourier transforms pick a frequency basis; PCA picks variance-aligned axes; token embeddings learn a coordinate system where semantic relationships become geometry later layers can exploit.

Where does machine learning use linear algebra?

Dense layers are $y = Wx + b$: each row of $W$ is a dot product against the input, exactly the row view of matrix multiply. Embedding tables store one vector per token ID. Attention scores query–key alignment with dot products and computes all pairs at once with matrix multiplication. Convolutions apply the same small linear map to every local patch. Regression and PCA lean on projection and eigenvectors of covariance matrices. Backpropagation threads Jacobians — local linear approximations of how small input changes affect outputs — through the graph; that is why I sanity-check gradients with numerical checking before trusting a new backward() implementation.

The AL-1 build path I am following assumes all of this is muscle memory: autogradgradient checkerBPEembeddings → positional encoding → attention. Linear algebra is the floor under every step.

Who should watch the 3Blue1Brown series?

If you are heading into machine learning, computer vision, or anything drowning in matrix notation, and symbols still feel like alphabet soup, the playlist is worth the time. You do not need a PhD — just patience and willingness to pause when an animation does not land on the first pass.

I still rewatch individual chapters when a course throws something abstract at me (SVD, change of basis, dual spaces). It is faster than rereading a textbook from zero. The animations gave me a place to put $Ax=b$ and weight matrices in PyTorch; the pencil work — one $2\times2$ example at a time — made the symbols trustworthy. If you are in the same boat I was in, watch the full Essence of Linear Algebra playlist, work through a tiny matrix by hand, and then go build something that actually multiplies matrices. That combination is what finally made it click for me.

Support the writing

If this post helped, a coffee keeps the deep dives, paper picks, and news digests coming.

Buy Me a Coffee