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 to say has rows and columns:
This is a matrix. The entry sits in row , column .
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 matrix takes vectors in and produces vectors in .
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:
Same rule as vector addition, since a matrix is just several vectors stacked together.
Scalar Multiplication
Multiplying by a scalar scales every entry:
Transpose
The transpose flips the matrix along its diagonal, turning rows into columns and columns into rows:
If is , then is . Transposing is cheap, just a reinterpretation of memory with no computation, and it appears constantly when aligning shapes for multiplication.
Matrix Multiplication
Multiplying () by () gives ():
Each entry is the dot product of row of with column of .
Example:
For the multiplication to be valid, the inner dimensions must match: is and is , so the must agree. The output has the outer dimensions: .
One critical property: matrix multiplication is not commutative. in general. Order matters.
Geometrically, means "first apply transformation , then apply transformation ." It's function composition, written right to left.
Matrix–Vector Multiplication
A case worth isolating: multiplying a matrix () by a vector () gives a vector ():
Each entry is the dot product of row of with :
This is neurons computing their dot product simultaneously, replacing a loop over neurons with one matrix operation. That's the whole point.
Why This Is Everywhere in Deep Learning
A Linear Layer Is a Matrix Multiplication
A linear layer transforms an input into an output via:
is the weight matrix (), is the bias vector (). Every learnable parameter in the layer lives in . Row of is the weight vector for output neuron , the direction in input space that neuron is sensitive to.
Training adjusts 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 examples passes through the network together. Instead of a single vector , we have a matrix , with one row per example.
The linear layer becomes:
is : 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.
Weight Matrices Learn Representations
After training, the rows of are directions in input space the layer has learned to respond to. A high dot product between input and row means strongly activates neuron : it contains the feature that row 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.