Mathematics for AI / Linear Algebra
The core operation of every neural network.
Reviewed by Yuvaraj
If you have ever written a line of deep-learning code, you have already multiplied matrices thousands of times per second, every dense layer, every attention head, every embedding lookup is matrix multiplication underneath. It is the single most important numerical operation in modern AI, and also the one most people fake their way through. This lesson makes it concrete: what a matrix is, what it does when you multiply, and exactly how the arithmetic works, entry by entry.
A matrix is a rectangular grid of numbers arranged in rows and columns. We describe its shape as , and we name the entry in row , column as . For example,
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
is a matrix, and (row 2, column 1).
But a matrix is more than storage. It is also a function: it takes a vector in and returns a new vector, stretching, rotating, and shearing space in a straight-line ("linear") way. Multiplying a vector by a matrix applies that transformation. Holding both pictures, the static grid and the active transformation, at once is what makes this concept hard, so let us look at it from three angles.
For a curious beginner
Picture the grid as a machine that transforms space. Feed it the arrows that point along your axes and it sends them to new places; every other point rides along, so squares turn into parallelograms. Multiplying a vector by the matrix just asks: where does this point land after the transformation?
How it is actually used
In code a matrix is a 2-D array, and matrix multiplication is a batch of
dot products. A linear (dense) layer stores its weights as a matrix W;
running the layer on inputs X is literally X @ W. Each output number is
the dot product of one input row with one weight column, which is why GPUs,
built to do billions of multiply-adds in parallel, are the hardware of AI.
The underlying mechanism
For , the entry is the dot product of row of with column of : . The sum runs over the shared inner index , which is precisely why that dimension must match.
You can multiply by only when the number of columns of equals the number of rows of . Line the shapes up side by side:
The two inner numbers (both ) must match; they cancel, and the two outer numbers ( and ) become the shape of the result. So a times a gives a , and a times a gives a . If the inner numbers disagree, the product simply does not exist.
Take
Here is and is ; the inner dimensions (both ) match, so is . Each output entry is one row of dotted with :
That is exactly what a two-neuron linear layer with no bias computes on a single input.
Now multiply two matrices:
Both are , so is . Compute each entry as (row of ) dotted with (column of ):
Collecting the four results:
One rule, everywhere
Matrix–vector multiplication is just the special case . Every product above is the same recipe, sum of products along the inner dimension, so once you can do one entry by hand, you can do a layer.
Common mistakes
A * B multiplies entry-by-entry and needs identical shapes; the real matrix product is A @ B (or np.matmul).matmul reference, how the operation and the @ operator behave in real code.