Linear algebra for LLMsPart 3: Matrices as transformations

Projections

7 min

Projecting onto an axis

xproj

Drop x=[2,3]x = [2, 3] straight down onto the horizontal axis and you land at [2,0][2, 0]. You kept the part of xx along e1e_1 and threw away the rest.

Projecting onto any unit vector

xu
proj⁡ux=(x⋅u) u\displaystyle \operatorname{proj}_u x = (x \cdot u)\,u

With ∣u∣=1|u| = 1: x⋅ux \cdot u is the shadow length (Part 1), and multiplying by uu points it the right way. Here x⋅u=1.2+0.8=2x \cdot u = 1.2 + 0.8 = 2, so the projection is 2u=[1.2,1.6]2u = [1.2, 1.6].

Write

proj⁡ux=(x⋅u) u\operatorname{proj}_u x = (x \cdot u)\,u for unit uu.

Removing a direction

x−(x⋅u) u=[2,1]−[1.2,1.6]\displaystyle x - (x \cdot u)\,u = [2, 1] - [1.2, 1.6]
=[0.8,−0.6]\displaystyle = [0.8, -0.6]
[0.8,−0.6]⋅u=0.48−0.48=0\displaystyle [0.8, -0.6] \cdot u = 0.48 - 0.48 = 0

Subtract the projection and what’s left has nothing along uu: it’s orthogonal to uu. Every vector splits into a part along uu plus a part at 90° to it.

Write

x=(x⋅u)u⏟along u+x−(x⋅u)u⏟⊥ux = \underbrace{(x \cdot u)u}_{\text{along } u} + \underbrace{x - (x \cdot u)u}_{\perp u}

The projection matrix

P=uu⊤=P = uu^\top =

(x⋅u)u=u(u⊤x)=(uu⊤)x(x \cdot u)u = u(u^\top x) = (uu^\top)x, so projecting is multiplying by P=uu⊤P = uu^\top: an outer product, rank 1. Projecting twice changes nothing more: P2=PP^2 = P. Removing the direction is I−PI - P.

Write

P=uu⊤P = uu^\top, P2=PP^2 = P. Remove uu: (I−uu⊤)x(I - uu^\top)x.

✏️ Your turn

Project and remove

x=[5,0],u=[0.6,0.8]x = [5, 0], \quad u = [0.6, 0.8]

Write proj⁡ux\operatorname{proj}_u x and x−proj⁡uxx - \operatorname{proj}_u x, then check the second is orthogonal to uu.

Work it out on paper, then go to the next card to check.

Answer

x⋅u=3\displaystyle x \cdot u = 3
proj⁡ux=[1.8,2.4]\displaystyle \operatorname{proj}_u x = [1.8, 2.4]
x−proj⁡ux=[3.2,−2.4]\displaystyle x - \operatorname{proj}_u x = [3.2, -2.4]
3.2(0.6)−2.4(0.8)=0\displaystyle 3.2(0.6) - 2.4(0.8) = 0

Onto a whole subspace

P=UU⊤(U⊤U=I)\displaystyle P = UU^\top \quad (U^\top U = I)

Stack kk orthonormal directions as the columns of UU (d×kd \times k). Then UU⊤xUU^\top x keeps exactly the part of xx inside their span. With k=1k = 1 it’s the uu⊤uu^\top you just used.

Projections inside and around LLMs

In a transformer

  • Ablation: x−(x⋅u)ux - (x \cdot u)u deletes whatever a model stores along direction uu in the residual stream, to test whether it matters.
  • Steering: x+αux + \alpha u pushes the model along a concept direction instead.
  • Linear probes read a concept as x⋅wx \cdot w: a projection onto ww.
  • LayerNorm’s centering subtracts the mean, which is projecting out the all-ones direction 1/d\mathbf{1}/\sqrt{d}: (x⋅1d)1d=xˉ 1(x \cdot \tfrac{\mathbf{1}}{\sqrt d})\tfrac{\mathbf{1}}{\sqrt d} = \bar{x}\,\mathbf{1}.

📌 Recap

Projections in one card

  • proj⁡ux=(x⋅u)u=uu⊤x\operatorname{proj}_u x = (x \cdot u)u = uu^\top x for unit uu.
  • x−(x⋅u)ux - (x \cdot u)u is orthogonal to uu: the direction removed.
  • P=uu⊤P = uu^\top (or UU⊤UU^\top) has P2=PP^2 = P.
  • Ablation, steering, probes and mean-centering are all projections.

Write

The four lines above.