Introduction: matrices do the work
Matrices aren't just tables of numbers. They are transformation machines. When a neural network receives an input and produces an output, it's a series of matrix operations doing the work.
Understanding these operations means understanding what actually happens in each layer of a model.
Addition and subtraction
The simplest operation. You add matrices element by element. The two matrices must have the same size.
If :
Concrete example:
In ML, addition appears everywhere: adding the bias to the output of a linear layer, adding residual connections in Transformers.
Scalar multiplication
You multiply every element by the same number:
When you multiply weights by a regularization factor, or when you apply a learning rate to the gradient, that's scalar multiplication.
The matrix product: the king of operations
This is the most important operation in deep learning. It's at the heart of every linear layer, every attention mechanism, every transformation.
Definition
To multiply by , the number of columns of A must equal the number of rows of B. The result is a matrix :
Each element is the dot product of the -th row of with the -th column of .

Example
Important properties
- Not commutative: in general. Order matters.
- Associative:
- Distributive:
Why it's fundamental in ML
A linear layer (or dense, or fully connected) in a neural network performs exactly:
Where:
- is the input vector
- is the weight matrix
- is the bias vector
- is the output vector
It's a matrix-vector product followed by an addition. The matrix encodes the learned transformation: it decides which combinations of inputs are relevant.
For a batch of examples, you stack the vectors into a matrix and compute . That's a single matrix product that processes the entire batch at once. That's why GPUs, optimized for massive matrix multiplication, are so important.
The attention mechanism
In a Transformer, attention is computed by:
It's a cascade of matrix products. computes the similarity scores between all pairs of tokens. Multiplication by produces the weighted output. Everything relies on matrix multiplication.
The transpose
Definition
The transpose of is the matrix obtained by swapping rows and columns:
Concretely, the first row of becomes the first column of .
Properties
- , note, the order reverses
Uses in ML
The transpose appears constantly:
- The dot product of two vectors is written
- In the attention formula, transposes the keys to compute similarities
- The gradient of the loss with respect to a linear layer's weights involves the transpose of the activations
The inverse of a matrix
Definition
For a square matrix , the inverse is the matrix such that:
It's the analog of division for matrices. If , then .
When does it exist?
The inverse doesn't always exist. A matrix is invertible (or nonsingular) if and only if:
- Its determinant is nonzero
- Its columns are linearly independent
- It has no zero eigenvalue
If the matrix is not invertible, it's called singular. Intuitively, it "crushes" information in one direction, it's impossible to go back.
In ML
You often avoid computing the inverse directly (it's expensive and numerically unstable). But the concept is central:
- Linear regression: the analytical solution is . In practice, more numerically stable methods are used.
- Second-order optimization: Newton's methods use the inverse of the Hessian matrix.
- Multivariate Gaussian distribution: involves the inverse of the covariance matrix.
The determinant
Intuitive definition
The determinant of a square matrix is a scalar that measures how the matrix deforms space.
For a matrix:
Geometric meaning
The determinant indicates:
- Magnitude: is the factor by which volumes are multiplied under the transformation . A determinant of 2 means areas double.
- Sign: a negative determinant means the transformation reverses orientation (like a mirror).
- Zero: if , the matrix crushes space into a lower dimension. Information is lost. The matrix is not invertible.

In ML
The determinant appears in:
- Normalizing flows: generative models that transform a simple distribution into a complex one. The log-determinant of the Jacobian is at the heart of the likelihood computation.
- Multivariate Gaussian distribution: the density involves , the determinant of the covariance matrix.
The linear layer: putting it all together
Let's recap. A linear layer in a neural network performs:
That's:
- A matrix product (), the learned transformation
- An addition (), the offset
- Followed by a nonlinear activation function
Stacking multiple layers means chaining matrix multiplications interspersed with nonlinearities. The entire network is a composition of matrix transformations.
That's why deep learning is fundamentally applied linear algebra. And that's why mastering these operations isn't optional, it means understanding what your model actually does.
Where this is used. Matrix multiplication and the transpose are literally the whole arithmetic of self-attention.