Start here

Run it

The 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

1The zero

Run it

From A matrix is a transformation

Run the cell. It prints the shape of ink and draws the zero.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

Two rows, across and up, and one column for each of the 35 inked squares.

2A matrix from two arrows

Your turn

From 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
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 turn

From Matrix multiplication

A 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
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 turn

From Matrix multiplication

inputs 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
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 turn

From 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
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 turn

From 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
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 own

From 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.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
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 .