Lesson 7 of 117 min
Rank and low-rank matrices
The rank of a matrix is how many directions it leaves after its move, and a matrix of low rank is a few columns times rows, stored in few numbers.
After this lesson you can say how many independent directions a matrix really has.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
A column times a row: `np.outer`
The video built the nine numbers as a times table: each one is a number from the column times a number from the row. NumPy has one call for it, np.outer. Unless one of the two lists is all zeros, the matrix it makes has rank 1.
import numpy as np
column = np.array([1, 2, 3])
row = np.array([4, 5, 7])
M = np.outer(column, row)
print(M)
print(np.linalg.matrix_rank(M))[[ 4 5 7] [ 8 10 14] [12 15 21]] 1
By rows or by columns, the same count
The video counted directions from the columns. You can count them from the rows instead, and you always get the same number. M.T turns the rows into columns, and its rank is still 1: every row of M is [4, 5, 7] times 1, 2 or 3.
print(np.linalg.matrix_rank(M.T))1
The points that land on the start
When a matrix squashes the plane flat, a whole line of points lands on [0, 0]. For the puzzle's matrix it is the line through [2, -1]: two of the arrow across, minus one of the arrow up, cancel out. Any point on that line, however far along, lands on the start.
S = np.array([[1, 2], [2, 4]])
for t in [1, 3, -0.5]:
p = t * np.array([2, -1])
print(p, S @ p)[ 2 -1] [0 0] [ 6 -3] [0 0] [-1. 0.5] [0. 0.]
Real numbers are almost never exactly low rank
Add a little noise, as every measurement has, and the nine numbers have rank 3: no column is exactly a multiple of another any more. matrix_rank takes a tol: a direction smaller than it is not counted. With a tol above the noise, the rank is 1 again.
rng = np.random.default_rng(0)
noisy = M + rng.normal(scale=0.001, size=M.shape)
print(noisy.round(3))
print(np.linalg.matrix_rank(noisy))
print(np.linalg.matrix_rank(noisy, tol=0.01))[[ 4. 5. 7.001] [ 8. 9.999 14. ] [12.001 15.001 20.999]] 3 1
LoRA's change starts at nothing
In Hugging Face's PEFT library, lora_A is the eight rows and lora_B the eight columns. lora_B starts as zeros, so before training the change is all zeros and the model is as it was. The change is also scaled by lora_alpha / r; here lora_alpha is 16 and r is 8.
r, alpha = 8, 16
lora_A = rng.normal(size=(r, 4096))
lora_B = np.zeros((4096, r))
change = (alpha / r) * (lora_B @ lora_A)
print(change.shape, np.count_nonzero(change))(4096, 4096) 0
Run on Python 3.13, NumPy 2.5.5 and scikit-learn 1.9.1. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: A matrix is a transformation
A move sends the arrow across, [1, 0], to [2, 1], and the arrow up, [0, 1], to [-1, 1]. Where does the point [1, 2] land?
2 of 3
The matrix [[2, 6], [3, 9]] has four different numbers. What is its rank?
3 of 3
A layer is a 1,000 by 1,000 matrix. A change of rank 4 is stored as 4 columns and 4 rows, of 1,000 numbers each. How many numbers does the change store?
The ideas in this lesson
- Rank
- How many directions a matrix leaves after its move: the number of its columns that are independent.
- Low rank
- Rank 1 is one column times one row; low rank is a few of those added, stored in few numbers.
It leans on
- LoRA
Transcript
What the video says, part by part. Press a time to watch from there.
The puzzle
Here's a puzzle. This is a handwritten zero on a grid. And here's a matrix that moves it. Its columns say where the two arrows land. The arrow across lands on one, two. The arrow up lands on two, four. So what will the zero look like after the move?
Squashed flat
Hold on to your guess.
A matrix moves every point of the plane, and its columns say where the arrows across and up land. Here, the arrow across lands on one, two. The arrow up lands on two, four. Let's move it.
The zero goes flat... onto one line. Why? Look at the two arrows. The arrow up, two, four, is twice as long as the arrow across, one, two... and it points the same way. Every point is a mix of the two arrows, so every point lands on that one line.
If you guessed a bigger zero that leans: the arrows do get longer. But they point the same way.
The rank
So how do you measure how flat a move leaves things?
Count the directions that are left. Before the move, the plane had two: across and up.
After it, just one. That count... is called the rank. This matrix has rank one.
You can also count from the columns, one at a time. Each column that points a new way adds one direction.
One, two is new: that's one. Two, four points the same way as one, two, so it adds nothing.
A matrix that makes the zero twice as wide keeps both directions. Rank two.
In space
And in space? A three by three matrix has three columns: where the arrows across, back and up land. In this one, the first two columns are the arrows across and back, as they were, flat on the floor. The third column is one, zero, zero... the same as the first. So the arrow up lands on the floor too, right on top of the arrow across. The whole box goes flat onto the floor. Two directions left: rank two.
Many numbers?
Can you see the rank in the numbers themselves?
More different numbers... more directions?
Still rank one
A matrix's rank counts the directions it leaves. Here's a three by three matrix with nine different numbers. What's its rank?
Look at the columns. The first column, four, eight, twelve, is just one, two, three, times four. The second is one, two, three, times five. The third, times seven. They all point the same way. Rank one.
In NumPy, one line counts it for you, and it says one too.
A column times a row
So the whole matrix is one column... times one row.
One, two, three down the side. Along the top, the numbers each column was multiplied by: four, five and seven. Each number inside is one from the column times one from the row, like a times table in school. A matrix of rank one... is a column times a row.
Nine numbers, made from six.
And a thousand by a thousand: a million numbers, made from two thousand. The matrix from the puzzle is one too: one, two, times one, two.
A few pieces
Now add two of these pieces. If their columns point different ways, you get rank two. Add eight, and you get rank eight at most. Two of them might point the same way. When the rank is small compared to the size, we say the matrix has a low rank. You can store it as its few columns and rows. That's far fewer numbers than the matrix holds.
Changing a big model
So why does this matter for AI?
Take a large AI model that writes text. One of its layers is a square matrix, with four thousand and ninety-six numbers on each side. That's almost seventeen million numbers.
To teach the model a new job, a method called LoRA leaves that matrix as it is.
It adds a change made of just eight column-times-row pieces. So it stores eight columns and eight rows, of four thousand and ninety-six numbers each. How many numbers is that, compared to seventeen million?
About sixty-five thousand. Less than half of one percent of the layer.
In NumPy: LoRA's change
Let's build that change in NumPy. Here are eight columns of four thousand and ninety-six numbers, and eight rows. When LoRA teaches a model a new job, these are the numbers it learns.
Here, they're random. The at sign multiplies them, and adds up the eight column-times-row pieces. The change has the layer's full size: four thousand and ninety-six on each side. NumPy can count its rank, as it did for the nine numbers. What will it print?
Eight... not four thousand and ninety-six.
Every column of the change is a mix of the same eight columns. So almost seventeen million numbers... and only eight directions. That's why LoRA keeps only the columns and the rows. The change they make holds two hundred and fifty-six times as many numbers.
What you saw
So here's what you saw. The rank is how many directions a matrix leaves. You count it from the columns: each one that points a new way adds one. A matrix of rank one is a column times a row. And a matrix of low rank is a few of those, added up. So a few numbers can hold it.
The lab
That's the end of this unit. In its lab, you'll use these three lessons to build a small layer, and to change it cheaply, the way LoRA does.
The directions left
So the rank is how many directions a matrix leaves. Low rank means a few numbers can hold it. Now try it on this lesson's page, and take the quiz. Make the zero go flat onto a line. Use two columns that point opposite ways.
You use this lesson in Lab 2: A small layer, and a cheap change to it, which comes next.
Facts last checked on .
