Linear algebra for LLMsPart 2: Matrices and matrix multiplication

Matrices and shapes

5 min

A matrix is a grid of numbers

AA has 2 rows and 3 columns: an m×nm \times n matrix with m=2m = 2 and n=3n = 3. We write A∈R2×3A \in \mathbb{R}^{2 \times 3}.

Write

A∈R2×3A \in \mathbb{R}^{2 \times 3}: 2 rows, 3 columns.

Rows go across, columns go down

Row 1 (blue) is [1,2,3][1, 2, 3]. Column 2 (pink) is [2,5][2, 5]. They cross at A12=2A_{12} = 2 (green), row first, then column.

Write

Row 1 = [1,2,3][1, 2, 3]. Column 2 = [2,5][2, 5]. A12=2A_{12} = 2.

✏️ Your turn

Read the entries

Write A21A_{21} (math, counting from 1) and A[0, 2] (code, counting from 0).

Work it out on paper, then go to the next card to check.

Answer

A21=4A_{21} = 4 (row 2, column 1, in blue) and A[0, 2] = 3 (row 0, column 2 in code, in pink). Same grid, two counting conventions.

A stack of rows: one token per row

Read XX by rows and each row is one token’s vector. Row “cat” is everything the model holds about that token. Most layers act on each row separately.

Write

XX is T×dT \times d. Row ii = token ii’s vector.

A stack of columns: one feature per column

Read XX by columns and each column is one feature across all the tokens: here feature 3 is [2,−1,0][2, -1, 0]. Normalization layers and some interpretability tools look along this direction.

Shapes you'll meet in a transformer

Matrix Shape One row is
XX, the hidden states T×dT \times d one token
EE, the embedding table V×dV \times d one vocabulary entry
WQW_Q, a projection (code layout) d×dhd \times d_h one input feature’s weights
QK⊤QK^\top, the attention scores T×TT \times T one query against every key

Write

X ⁣:T×dX\!: T \times d. E ⁣:V×dE\!: V \times d. WQ ⁣:d×dhW_Q\!: d \times d_h. Scores: T×TT \times T.

In PyTorch

A = torch.tensor([[1, 2, 3], [4, 5, 6]])
A.shape      # torch.Size([2, 3])
A[0]         # first row: tensor([1, 2, 3])
A[:, 1]      # second column: tensor([2, 5])
A[1, 0]      # one entry: tensor(4)

: means “every index along this axis”.

📌 Recap

Matrices and shapes in one card

  • An m×nm \times n matrix has mm rows and nn columns; AijA_{ij} is row ii, column jj.
  • Math counts from 1 (A21A_{21}); code counts from 0 (A[1, 0]).
  • Hidden states XX are T×dT \times d: each row is a token, each column a feature.

Write

The three lines above.