Skip to content
Math Foundations

Matrices

From vectors to matrices, organized collections of vectors that unlock linear layers, batch computation, and the backbone of every deep learning model.

A single vector is one input. But neural networks don't process one input at a time, but hundreds simultaneously. And a single linear layer doesn't apply one weight but many, transforming an entire space at once. Both of those ideas require matrices.

What Is a Matrix?

A matrix is a 2D array of numbers, organized in rows and columns. We write A∈Rm×n\mathbf{A} \in \mathbb{R}^{m \times n} to say A\mathbf{A} has mm rows and nn columns:

A=[a11a12a13a21a22a23]\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix}

This is a 2×32 \times 3 matrix. The entry aija_{ij} sits in row ii, column jj.

a₁₂1234562 × 3 matrix — aᵢⱼ is in row i, column j

There are two ways to read a matrix. Algebraically, it's a rectangle of numbers with a shape. Geometrically, it's a linear transformation, a function that takes a vector in one space and maps it to another. A 2×32 \times 3 matrix takes vectors in R3\mathbb{R}^3 and produces vectors in R2\mathbb{R}^2.

The geometric view is what builds intuition for why matrix multiplication works the way it does.

Basic Operations

Addition

Two matrices of the same shape add element-wise:

[1234]+[5678]=[681012]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} + \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ 10 & 12 \end{bmatrix}

Same rule as vector addition, since a matrix is just several vectors stacked together.

Scalar Multiplication

Multiplying by a scalar scales every entry:

2⋅[1234]=[2468]2 \cdot \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = \begin{bmatrix} 2 & 4 \\ 6 & 8 \end{bmatrix}

Transpose

The transpose AT\mathbf{A}^T flips the matrix along its diagonal, turning rows into columns and columns into rows:

A=[123456]  ⟹  AT=[142536]\mathbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \implies \mathbf{A}^T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}

If A\mathbf{A} is m×nm \times n, then AT\mathbf{A}^T is n×mn \times m. Transposing is cheap, just a reinterpretation of memory with no computation, and it appears constantly when aligning shapes for multiplication.

Matrix Multiplication

Multiplying A\mathbf{A} (m×km \times k) by B\mathbf{B} (k×nk \times n) gives C\mathbf{C} (m×nm \times n):

Cij=∑l=1kAil BljC_{ij} = \sum_{l=1}^{k} A_{il} \, B_{lj}

Each entry CijC_{ij} is the dot product of row ii of A\mathbf{A} with column jj of B\mathbf{B}.

Example:

[1234][5678]=[1⋅5+2⋅71⋅6+2⋅83⋅5+4⋅73⋅6+4⋅8]=[19224350]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 1{\cdot}5 + 2{\cdot}7 & 1{\cdot}6 + 2{\cdot}8 \\ 3{\cdot}5 + 4{\cdot}7 & 3{\cdot}6 + 4{\cdot}8 \end{bmatrix} = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}
1234×5678=19224350C₁₁ = 1·5 + 2·7 = 19 — each entry is a dot product (row · column)

For the multiplication to be valid, the inner dimensions must match: A\mathbf{A} is m×km \times k and B\mathbf{B} is k×nk \times n, so the kk must agree. The output has the outer dimensions: m×nm \times n.

One critical property: matrix multiplication is not commutative. AB≠BA\mathbf{AB} \neq \mathbf{BA} in general. Order matters.

Geometrically, AB\mathbf{AB} means "first apply transformation B\mathbf{B}, then apply transformation A\mathbf{A}." It's function composition, written right to left.

Matrix–Vector Multiplication

A case worth isolating: multiplying a matrix W\mathbf{W} (m×nm \times n) by a vector x\mathbf{x} (n×1n \times 1) gives a vector y\mathbf{y} (m×1m \times 1):

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

Each entry yiy_i is the dot product of row ii of W\mathbf{W} with x\mathbf{x}:

yi=∑j=1nWij xjy_i = \sum_{j=1}^{n} W_{ij} \, x_j

This is mm neurons computing their dot product simultaneously, replacing a loop over neurons with one matrix operation. That's the whole point.

2103×12=46y₁ = 2·1 + 1·2 = 4 — each output is one row dotted with x

Why This Is Everywhere in Deep Learning

A Linear Layer Is a Matrix Multiplication

A linear layer transforms an input x∈Rn\mathbf{x} \in \mathbb{R}^n into an output y∈Rm\mathbf{y} \in \mathbb{R}^m via:

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

W\mathbf{W} is the weight matrix (m×nm \times n), b\mathbf{b} is the bias vector (m×1m \times 1). Every learnable parameter in the layer lives in W\mathbf{W}. Row ii of W\mathbf{W} is the weight vector for output neuron ii, the direction in input space that neuron is sensitive to.

Training adjusts W\mathbf{W} so that the transformation maps inputs to useful representations. What changes during training is the matrix.

Batching: Processing Multiple Inputs at Once

Training on one example at a time is slow. In practice, a batch of BB examples passes through the network together. Instead of a single vector x∈Rn\mathbf{x} \in \mathbb{R}^n, we have a matrix X∈RB×n\mathbf{X} \in \mathbb{R}^{B \times n}, with one row per example.

The linear layer becomes:

Y=XWT+b\mathbf{Y} = \mathbf{X}\mathbf{W}^T + \mathbf{b}

Y\mathbf{Y} is B×mB \times m: one output row per input example, all computed in a single matrix multiplication. This is why GPUs dominate deep learning, as massively parallel matrix operations are exactly what they were designed for.

100111123×Wᵀ2103=210324B = 3 examples in parallel — row i of X maps to row i of Y

Weight Matrices Learn Representations

After training, the rows of W\mathbf{W} are directions in input space the layer has learned to respond to. A high dot product between input x\mathbf{x} and row ii means x\mathbf{x} strongly activates neuron ii: it contains the feature that row ii has specialized in. The matrix is a learned filter bank.

This idea extends far. The query, key, and value matrices in a transformer's attention mechanism are learned weight matrices. The token embedding matrix maps integer indices to dense vectors. Every parameter in a neural network lives in some matrix, and every forward pass is a chain of matrix multiplications.

In Code

import torch

# A weight matrix W of shape (2, 3): it maps 3-dimensional inputs to 2-dimensional outputs
W = torch.tensor([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0]])

x = torch.tensor([1.0, 0.0, -1.0])

# Matrix-vector product: each output is the dot product of one row of W with x
y = W @ x
print(y)  # tensor([-2., -2.])

# A batch of two examples, one per row: shape (2, 3)
X = torch.tensor([[1.0, 0.0, -1.0],
                  [2.0, 1.0,  0.0]])

# The whole batch goes through W in a single matrix product: shape (2, 2)
Y = X @ W.T
print(Y)  # tensor([[-2., -2.], [ 4., 13.]])

Vectors and matrices are the two primitives everything else is built from. Training a network means adjusting those matrices so the outputs improve, and that requires knowing how to measure change. In the next article, we cover derivatives, the mathematical tool that makes this precise.