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 matrix takes a vector to . Each output component is a dot product of one row of with :
The map is linear: . Linearity means the whole map is determined by what it does to the basis vectors , and is simply the -th column of . So the columns are where the axes go, and is the combination of those columns weighted by the entries of :
This column picture is the most useful single idea in applied linear algebra.[1]
Worked example: three maps of the plane
| Matrix | Effect on , | Geometry |
|---|---|---|
| , | stretch , squash | |
| , | rotate 90° | |
| , | 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 . For example, . 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 : a linear map, a shift by the bias , and an elementwise nonlinearity 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 and then is the single map : matrix multiplication is defined so that . Shapes must chain: an matrix times a matrix gives .
This has an immediate consequence. Stack any number of linear layers without nonlinearities and the result is a single matrix:
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 ( in general: rotate-then-stretch differs from stretch-then-rotate). And although , the costs differ: forming first and then multiplying by can take far more arithmetic than two matrix–vector products. The adapter method below depends on choosing the cheap order.
A network has two linear layers with no activation function: is and is . What single matrix computes the same function, and what is its shape?
Show answer +Hide answer −
, which is . 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 is the set of all possible outputs , all combinations of its columns. Its dimension is the rank of . The null space is the set of inputs mapped to zero. For an matrix, the rank–nullity theorem says
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 ).
Two facts follow for networks:
- Rank never exceeds the narrowest dimension. , and for a product, . 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, and each of 8 heads projects queries and keys down to dimensions.[2] The score is a bilinear form whose matrix 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- matrix of shape can always be written as a product with of shape and of shape . Storing and takes numbers instead of .
Hu and colleagues' LoRA (low-rank adaptation) uses exactly this to fine-tune large models cheaply. The pretrained weight is frozen, and the update is constrained to low rank:
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 is just a matrix, it can be added into after training, so the adapted model runs with no extra inference latency.
Worked example: counting parameters
Take one projection matrix. Full fine-tuning updates numbers. A rank-8 LoRA update trains , a factor of 256 fewer. During training the adapter is applied as : first an product, then a one, about multiply-adds per input vector, rather than forming the full matrix .
A LoRA adapter of rank is attached to a weight matrix. How many trainable parameters does it add, and what fraction of the full matrix is that?
Show answer +Hide answer −
parameters, versus for the full matrix, about .
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 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 to frozen weights.
Sources
-
G. Strang (2023). Introduction to Linear Algebra, 6th ed. Wellesley-Cambridge Press. https://math.mit.edu/~gs/linearalgebra/ila6/indexila6.html ↩ ↩2
-
A. Vaswani et al. (2017). "Attention Is All You Need." NeurIPS 2017. arXiv:1706.03762. https://arxiv.org/abs/1706.03762 ↩ ↩2
-
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 ↩
-
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 ↩
