A matrix product is a grid of dot products, legal only when inner dimensions agree, and fast because hardware runs those dot products in parallel.
Shapes
- A matrix is a grid of numbers with shape
(rows × columns). A Bis defined only when inner dimensions match:(m × n)times(n × p)gives(m × p).- Shared
nis consumed; outermandpsurvive. Mismatched inner dims = no product. - Tracking shapes at every step catches the most common ML bug.
(m × n) · (n × p) → (m × p)
\____/
must match
Definition
- Entry
(i, j)is the dot product of rowiofAwith columnjofB:
(AB)[i][j] = sum over k = 1..n of A[i][k] * B[k][j]
- A dot product multiplies two equal-length vectors element by element and sums to one number.
- Matrix-vector product
A xreturns a new vector; a linear layer computesy = W x + b, and training learns the entries ofW.
Properties
| Property | Rule | Note |
|---|---|---|
| Not commutative | A B ≠ B A | Order matters; B A may not exist |
| Associative | (A B) C = A (B C) | Regroup to pick cheapest order |
| Distributive | A (B + C) = A B + A C | |
| Identity | A I = I A = A | I = ones on diagonal, zeros elsewhere |
| Transpose | (A B)^T = B^T A^T | Product reverses |
Cost
- Naive cost:
O(m·n·p)multiply-adds (m·poutput cells, each a length-ndot product). - For square
n × nmatrices:O(n³). - Strassen and successors lower the exponent but are rarely used in practice.
- Real speedups come from parallel hardware (GPUs, TPUs), which is why ML work is cast as large matrix products.
Broadcasting (not multiplication)
- Broadcasting virtually stretches a smaller array to match a larger one along size-1 dimensions.
- Adding a bias
(p,)to outputs(m × p)hits every row with no explicit loop. - It is elementwise, not matrix multiplication. Confusing the two gives wrong shapes or plausible-looking wrong answers.