NeuroGenesys.
The Geometry of LearningAcademy • Foundations • Lesson 2 of 3

Matrices as maps: layers, rank and composition

7 min read • By Will Schulz • NeuroGenesys AI Academy

A neural network layer is, before its nonlinearity, a matrix multiplying a vector. Reading that matrix as a map that moves, stretches and collapses space explains why depth needs nonlinearities, why some information is irrecoverably lost inside a network, and why fine-tuning a large model can need only a small fraction of its parameters.

A matrix is a function

An m×nm \times n matrix WW takes a vector x∈Rnx \in \mathbb R^n to Wx∈RmWx \in \mathbb R^m. Each output component is a dot product of one row of WW with xx:

(Wx)i=∑j=1nWij xj.(Wx)_i = \sum_{j=1}^{n} W_{ij}\,x_j.

The map is linear: W(ax+by)=a Wx+b WyW(ax + by) = a\,Wx + b\,Wy. Linearity means the whole map is determined by what it does to the basis vectors e1,…,ene_1, \dots, e_n, and WejWe_j is simply the jj-th column of WW. So the columns are where the axes go, and WxWx is the combination of those columns weighted by the entries of xx:

Wx=x1 (col1)+x2 (col2)+⋯+xn (coln).Wx = x_1\,(\text{col}_1) + x_2\,(\text{col}_2) + \cdots + x_n\,(\text{col}_n).

This column picture is the most useful single idea in applied linear algebra.[1]

Worked example: three maps of the plane

MatrixEffect on e1=(1,0)e_1 = (1,0), e2=(0,1)e_2 = (0,1)Geometry
(2001/2)\begin{pmatrix} 2 & 0 \\ 0 & 1/2 \end{pmatrix}(2,0)(2,0), (0,12)(0,\tfrac12)stretch xx, squash yy
(0−110)\begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}(0,1)(0,1), (−1,0)(-1,0)rotate 90°
(1224)\begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix}(1,2)(1,2), (2,4)(2,4)flatten onto a line

The third matrix sends both axes onto the same line (the second column is twice the first), so every input lands somewhere on the line through (1,2)(1,2). For example, (2,−1)↦(2−2,4−4)=(0,0)(2, -1) \mapsto (2 - 2, 4 - 4) = (0, 0). A whole direction of inputs is mapped to zero and cannot be recovered from the output.

A layer is an affine map plus a nonlinearity

A fully connected layer computes h=σ(Wx+b)h = \sigma(Wx + b): a linear map, a shift by the bias bb, and an elementwise nonlinearity σ\sigma such as ReLU. A transformer is dominated by such maps: the query, key, value and output projections in attention and the two large matrices of each feed-forward block.[2] Most of a large model's parameters and most of its arithmetic live in these matrices.

Composition is multiplication

Applying W1W_1 and then W2W_2 is the single map W2W1W_2 W_1: matrix multiplication is defined so that (W2W1)x=W2(W1x)(W_2 W_1)x = W_2(W_1 x). Shapes must chain: an m×km \times k matrix times a k×nk \times n matrix gives m×nm \times n.

This has an immediate consequence. Stack any number of linear layers without nonlinearities and the result is a single matrix:

WL⋯W2W1=Weff.W_L \cdots W_2 W_1 = W_{\text{eff}}.

Depth then adds nothing to what the network can represent; it can still only compute linear functions. The nonlinearity between layers is what breaks the collapse. With it, even a single hidden layer of sigmoidal units can approximate any continuous function on a bounded region to arbitrary accuracy given enough units, as Cybenko proved in 1989.[3]

Order matters in two ways. Matrix multiplication is not commutative (W2W1≠W1W2W_2 W_1 \ne W_1 W_2 in general: rotate-then-stretch differs from stretch-then-rotate). And although (AB)x=A(Bx)(AB)x = A(Bx), the costs differ: forming ABAB first and then multiplying by xx can take far more arithmetic than two matrix–vector products. The adapter method below depends on choosing the cheap order.

Check your understanding

A network has two linear layers with no activation function: W1W_1 is 512×784512 \times 784 and W2W_2 is 10×51210 \times 512. What single matrix computes the same function, and what is its shape?

Show answer +

W2W1W_2 W_1, which is 10×78410 \times 784. The 512-unit hidden layer adds parameters but no expressive power; only a nonlinearity between the layers would change that.

Rank: how much of the space survives

The column space of WW is the set of all possible outputs WxWx, all combinations of its columns. Its dimension is the rank of WW. The null space is the set of inputs mapped to zero. For an m×nm \times n matrix, the rank–nullity theorem says

rank⁡(W)+dim⁡(null space)=n.\operatorname{rank}(W) + \dim(\text{null space}) = n.

Every input dimension either survives into the output or is annihilated.[1] In the worked example above, the flattening matrix has rank 1 and a one-dimensional null space (the line through (2,−1)(2,-1)).

Two facts follow for networks:

  • Rank never exceeds the narrowest dimension. rank⁡(W)≤min⁡(m,n)\operatorname{rank}(W) \le \min(m, n), and for a product, rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(AB) \le \min(\operatorname{rank} A, \operatorname{rank} B). A composition of linear maps is at most as expressive as its narrowest bottleneck.
  • Attention compares in a low-dimensional subspace. In the original transformer, dmodel=512d_{\text{model}} = 512 and each of 8 heads projects queries and keys down to dk=64d_k = 64 dimensions.[2] The score q⋅k=xi⊤WQWK⊤xjq \cdot k = x_i^\top W_Q W_K^\top x_j is a bilinear form whose 512×512512 \times 512 matrix WQWK⊤W_Q W_K^\top has rank at most 64. Each head can only measure similarity along 64 directions of the residual stream, which is one reason models use many heads.

Low-rank updates: LoRA

A rank-rr matrix of shape d×kd \times k can always be written as a product BABA with BB of shape d×rd \times r and AA of shape r×kr \times k. Storing BB and AA takes r(d+k)r(d + k) numbers instead of dkdk.

Hu and colleagues' LoRA (low-rank adaptation) uses exactly this to fine-tune large models cheaply. The pretrained weight W0W_0 is frozen, and the update is constrained to low rank:

W=W0+BA,B∈Rd×r,  A∈Rr×k,  r≪min⁡(d,k).W = W_0 + BA, \qquad B \in \mathbb R^{d \times r},\; A \in \mathbb R^{r \times k},\; r \ll \min(d, k).

BB starts at zero, so training begins from the pretrained model. The authors' hypothesis is that the weight change needed for adaptation has low intrinsic rank. They report that, compared with full fine-tuning of GPT-3 175B with Adam, LoRA reduced trainable parameters by a factor of 10,000 and GPU memory by a factor of 3, with quality on par or better on the models they tested.[4] Because BABA is just a matrix, it can be added into W0W_0 after training, so the adapted model runs with no extra inference latency.

Worked example: counting parameters

Take one 4096×40964096 \times 4096 projection matrix. Full fine-tuning updates 40962=16,777,2164096^2 = 16{,}777{,}216 numbers. A rank-8 LoRA update trains 8×(4096+4096)=65,5368 \times (4096 + 4096) = 65{,}536, a factor of 256 fewer. During training the adapter is applied as B(Ax)B(Ax): first an 8×40968 \times 4096 product, then a 4096×84096 \times 8 one, about 2×8×4096=65,5362 \times 8 \times 4096 = 65{,}536 multiply-adds per input vector, rather than forming the full 4096×40964096 \times 4096 matrix BABA.

Check your understanding

A LoRA adapter of rank r=16r = 16 is attached to a 1024×40961024 \times 4096 weight matrix. How many trainable parameters does it add, and what fraction of the full matrix is that?

Show answer +

16×(1024+4096)=81,92016 \times (1024 + 4096) = 81{,}920 parameters, versus 1024×4096=4,194,3041024 \times 4096 = 4{,}194{,}304 for the full matrix, about 1.95%1.95\%.

Which low-rank directions to choose, and why so few suffice, is a question about the singular value decomposition, the subject of the next lesson.

Key takeaways

  • A matrix is a linear map; its columns are the images of the basis vectors, and WxWx is a weighted combination of those columns.
  • Composing maps is multiplying matrices. Without nonlinearities, any stack of linear layers collapses to one matrix, so depth adds no expressive power.
  • Rank is the dimension of what survives the map; it is capped by the narrowest dimension, and rank–nullity accounts for every lost direction.
  • Attention heads compare tokens through low-rank bilinear forms, and LoRA fine-tunes by adding a low-rank product BABA to frozen weights.

Sources

  1. G. Strang (2023). Introduction to Linear Algebra, 6th ed. Wellesley-Cambridge Press. https://math.mit.edu/~gs/linearalgebra/ila6/indexila6.html ↩ ↩2

  2. A. Vaswani et al. (2017). "Attention Is All You Need." NeurIPS 2017. arXiv:1706.03762. https://arxiv.org/abs/1706.03762 ↩ ↩2

  3. G. Cybenko (1989). "Approximation by Superpositions of a Sigmoidal Function." Mathematics of Control, Signals and Systems 2(4): 303–314. https://doi.org/10.1007/BF02551274 ↩

  4. E. J. Hu, Y. Shen, P. Wallis, Z. Allen-Zhu, Y. Li, S. Wang, L. Wang, W. Chen (2021). "LoRA: Low-Rank Adaptation of Large Language Models." arXiv:2106.09685. https://arxiv.org/abs/2106.09685 ↩

Leadership

A word from the founder — on why the brain is the blueprint, and why the next decade of intelligence will be built here.

"The brain is the universe's only proof that general intelligence is possible. We treat it as the blueprint."

From cortical interfaces to photonic silicon, every program at NeuroGenesys exists to answer one question: how does intelligence arise — and how do we build it safely.

Will Schulz

Will Schulz

Founder, NeuroGenesys • AW3 Technology