Lab 2 of 4about 40 minutes
A small layer, and a cheap change to it
It uses what you learned in A matrix is a transformation, Matrix multiplication and Rank and low-rank matrices.
Start here
Run itThe handwritten zero from the videos, as points: each inked square of the 8 by 8 picture is one column of ink, across then up, with the start in the middle. draw puts pictures side by side on one plane.
1The zero
Run itFrom A matrix is a transformation
Run the cell. It prints the shape of ink and draws the zero.
Two rows, across and up, and one column for each of the 35 inked squares.
2A matrix from two arrows
Your turnFrom A matrix is a transformation
Make the zero lean to the right. The arrow across, [1, 0], stays where it is. The arrow up, [0, 1], lands on [0.5, 1]. Write the matrix lean, row by row as NumPy wants it, so that its columns are those two landing places.
Hint
NumPy writes a matrix row by row. The first row holds the first number of each column, the second row the second number.
One way to do it
lean = np.array([[1, 0.5], [0, 1]])
practice = lean @ ink
draw(ink, practice)3A layer, by hand
Your turnA layer is a matrix, and a number added to each output. Write the three outputs by hand: each row's dot product with x, plus its added number. Then let @ do it.
Two lines go wrong. matrix * x gives a grid of products with no sums. matrix @ np.array([1, 2, 3]) stops: ValueError: … (size 3 is different from 2). Each row has two numbers, so the input needs two.
Hint
The second output uses the second row and the second added number, and so on. With @, the whole layer is one line.
One way to do it
matrix = np.array([[2, 1], [-1, 2], [-2, -1]])
added = np.array([1, -1, 0.5])
x = np.array([2, 1])
by_hand = np.array([matrix[0] @ x + added[0], matrix[1] @ x + added[1], matrix[2] @ x + added[2]])
out = matrix @ x + added
print(by_hand, out)4Many inputs at once
Your turninputs holds four inputs, one in each row. Give all four to the layer in one line, as the video's board did: .T stands the matrix's rows up as columns. Keep the result in outputs.
Hint
Each input is a row now, so the matrix has to stand its rows up as columns: inputs @ matrix.T, then add.
One way to do it
inputs = np.arange(8).reshape(4, 2)
outputs = inputs @ matrix.T + added
print(outputs)5Two moves, one matrix
Your turnFrom A matrix is a transformation and Matrix multiplication
turn turns the zero on its side, and wide makes it twice as wide. Join them into one matrix each way: turn_then_wide turns first, wide_then_turn stretches first. Then draw both.
Hint
Read from the right: in wide @ turn @ ink, turn touches the points first.
One way to do it
turn = np.array([[0, -1], [1, 0]])
wide = np.array([[2, 0], [0, 1]])
turn_then_wide = wide @ turn
wide_then_turn = turn @ wide
draw(turn_then_wide @ ink, wide_then_turn @ ink)6A change of rank 1
Your turnFrom Rank and low-rank matrices and Matrix multiplication
To teach a layer something new, LoRA leaves its matrix as it is and adds a change made of a column times a row. Make that change for matrix: columns is one column of 3 numbers, rows one row of 2. Then read its rank, and count the numbers it stores.
Hint
A column times a row is matrix times matrix: @. np.linalg.matrix_rank counts the directions.
One way to do it
columns = np.array([[1], [0], [2]])
rows = np.array([[0.5, -1]])
change = columns @ rows
rank = np.linalg.matrix_rank(change)
stored = columns.size + rows.size
print(change, rank, stored, "against", change.size)7A change for a real layer
On your ownFrom Rank and low-rank matrices
On your own now. On a 3 by 2 layer, a change of rank 1 saves just one number. Make a change of rank 8 for a layer of 512 by 512: columns of shape (512, 8) and rows of shape (8, 512), filled by rng.normal. Keep its rank in rank and the share of the layer's numbers it stores in share.
Hint
rng = np.random.default_rng(0) first. The share is what you store, columns.size + rows.size, divided by change.size.
One way to do it
rng = np.random.default_rng(0)
columns = rng.normal(size=(512, 8))
rows = rng.normal(size=(8, 512))
change = columns @ rows
rank = np.linalg.matrix_rank(change)
share = (columns.size + rows.size) / change.size
print(change.shape, rank, share)Run on Pyodide 314, Python 3.14, NumPy 2.4, scikit-learn 1.8 and matplotlib 3.10. Checked on .
