Module 5 — Matrices & Transformations
A single data point is a vector; a complete dataset is a two-dimensional array of numbers — a matrix. A matrix also represents a transformation: multiplying a vector by a matrix transforms it — rotating, scaling, or combining its features. Every layer of a neural network is precisely one such transformation. This module develops matrix multiplication concretely and establishes why it constitutes the neural-network layer.
A matrix as data and as a transformation
A matrix is a rectangular array of numbers organized into rows and columns. It admits two interpretations:
- As data: each row is one example, each column one feature. Five houses with three features each is a 5×3 matrix.
- As a transformation: a matrix acts on a vector to produce a new vector — stretching, rotating, or projecting it. This is the interpretation central to machine-learning models.
Matrix–vector multiplication: a collection of dot products
To multiply a matrix by a vector, compute the dot product of each row with the vector (the operation introduced in Module 4). Each row yields one component; together they form the output vector:
As a worked example, let \( A=\begin{bmatrix} 2 & -1 \\ 1 & 3 \end{bmatrix} \) and \( \mathbf{x}=\begin{bmatrix} 3 \\ 2 \end{bmatrix} \). The dot product of row 1 with the vector gives the first component, and that of row 2 the second:
Step through it yourself below — each result entry lights up the matrix row and the vector it is the dot product of.
This activity needs JavaScript. Each output entry is the dot product of one row of the matrix and the vector: row 1 gives \( (2)(3)+(-1)(2)=4 \), row 2 gives \( (1)(3)+(3)(2)=9 \).
The visualizer below allows you to specify the matrix and observe its effect on a vector and on an entire grid of points. The rotation, scale, and shear presets demonstrate the corresponding transformations geometrically.
This activity needs JavaScript. The lesson below still covers everything.
Matrix × matrix: chaining transformations
Multiplying two matrices means applying one transformation after another. The entry in row \( i \), column \( j \) of the result is the dot product of row \( i \) of the first with column \( j \) of the second. The inner dimensions must match — an \( m\times n \) times an \( n\times p \) gives an \( m\times p \). Build one entry at a time below and watch the row-meets-column pattern.
This activity needs JavaScript.
Two special matrices: the identity and the transpose
The identity matrix \( I \) is the square matrix with \( 1 \)s on its main diagonal and \( 0 \)s everywhere else. In two dimensions,
It is the multiplicative identity of matrix multiplication, playing exactly the role the number \( 1 \) plays for the real numbers. Just as \( 1 \cdot x = x \) for every real number \( x \), we have \( I\mathbf{x} = \mathbf{x} \) for every vector \( \mathbf{x} \), and \( IA = AI = A \) for every compatible matrix \( A \). Multiplying by \( I \) leaves its argument unchanged.
The transpose \( A^{\top} \) is formed by reflecting a matrix across its main diagonal: row \( i \) of \( A \) becomes column \( i \) of \( A^{\top} \), so the entries satisfy \( (A^{\top})_{ij} = A_{ji} \). An \( m \times n \) matrix becomes \( n \times m \). For example,
The transpose appears constantly when aligning dimensions so that a product is defined. Watch each row swing into the matching column below.
This activity needs JavaScript. The transpose reflects a matrix across its main diagonal — row \( i \) becomes column \( i \), so a \( 2\times 3 \) matrix becomes \( 3\times 2 \).
Step through one layer — multiply by \( W \), add the bias, then apply the activation — and watch \( \mathbf{x} \) become \( \mathbf{h} \):
This activity needs JavaScript. One layer computes \( \mathbf{h} = f(W\mathbf{x} + \mathbf{b}) \): matrix-multiply the input, add the bias, then apply the activation \( f \).
Check your understanding
Determine the entries and dimensions of the given matrix products.
This activity needs JavaScript.