Synaplume

Linear Algebra

Matrices & Matrix Multiplication

50 min

Two ways to see a matrix

A matrix is a rectangular grid of numbers, like a spreadsheet:

A=[2013]A = \begin{bmatrix} 2 & 0 \\ 1 & 3 \end{bmatrix}

This one has 2 rows and 2 columns (a "2×2 matrix"). There are two equally important ways to think about matrices:

1. A matrix is a table of data. Each row is one example (a house), each column one feature (size, age). A dataset of 10,000 houses with 20 features is a 10,000 × 20 matrix. When ML engineers say "design matrix", they mean this.

2. A matrix is a function that transforms vectors. Multiplying a vector by a matrix rotates, stretches, flips or squashes it. Every layer of a neural network is a matrix waiting to transform whatever vector arrives.

Matrix–vector multiplication

To compute AxA\mathbf{x}, take the dot product of each row of AA with x\mathbf{x}:

[2013][12]=[21+0211+32]=[27]\begin{bmatrix} 2 & 0 \\ 1 & 3 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \end{bmatrix} = \begin{bmatrix} 2\cdot1 + 0\cdot2 \\ 1\cdot1 + 3\cdot2 \end{bmatrix} = \begin{bmatrix} 2 \\ 7 \end{bmatrix}

A vector went in, a (transformed) vector came out — a matrix is a function. A neural network layer computes exactly

y=Wx+b\mathbf{y} = W\mathbf{x} + \mathbf{b}

where WW holds the layer's weights and b\mathbf{b} its biases. Notice this is the multi-dimensional version of y=wx+by = wx + b from lesson one. The pattern never changes; only the dimensions grow.

Matrix–matrix multiplication

Multiplying matrix AA (m×n) by matrix BB (n×p) gives an m×p matrix: entry (i,j)(i,j) is the dot product of row ii of AA with column jj of BB. Two crucial properties:

  • Inner dimensions must match — (m×n)(n×p) works; the n's must agree.
  • Order mattersABBAAB \neq BA in general. Transformations applied in a different order give different results (rotate-then-stretch ≠ stretch-then-rotate).

Composing two transformations = multiplying their matrices. A 50-layer network is (roughly) 50 matrix multiplications chained together. Training GPT-class models is, computationally, trillions of these operations — which is why the entire AI industry runs on GPUs, chips originally built to multiply matrices for 3-D graphics.

Special matrices worth knowing

  • Identity II: ones on the diagonal, zeros elsewhere; Ix=xI\mathbf{x} = \mathbf{x} (does nothing).
  • Transpose ATA^T: flip rows and columns.
  • Inverse A1A^{-1}: the "undo" matrix, A1A=IA^{-1}A = I (when it exists).

Key takeaways

  • Matrix = data table and vector transformer; both views matter.
  • A neural layer is Wx+bW\mathbf{x} + \mathbf{b} — matrices are the weights of AI.
  • Matrix multiplication composes transformations; order matters.

Check your understanding

0/3 answered
  1. 1.A neural network layer computes y=Wx+b\mathbf{y} = W\mathbf{x} + \mathbf{b}. What is WW?

  2. 2.Is matrix multiplication commutative (AB=BAAB = BA)?

  3. 3.Why do GPUs dominate AI computing?

Share:

0% of Mathematics for AI completed

Up nextEigenvalues & Why Dimensions Get Reduced