In our last lesson, we established that the core computational step within a neural network layer is an affine transformation, g(Wx + b). This combination of a linear transformation (the weight matrix W) and a translation (the bias vector b), followed by a non-linear activation g, is the fundamental building block of deep learning.
This raises a crucial question: why do we express this process using matrix algebra? Is it just for tidy notation, or is there a deeper, more practical reason? This lesson will answer that question directly. We will explore how casting data operations as matrix multiplications is the key that unlocks the massive computational speed required to train and run modern machine learning models on entire datasets, not just one sample at a time.
From One to Many: Batch Processing with Matrices
So far, we have considered applying a layer's transformation to a single input vector x. In practice, we almost never do this. Instead, we process a batch of many input samples simultaneously.
A naive approach would be to use a for loop: calculate the output for the first input, then the second, then the third, and so on. This is computationally slow and inefficient. A much better way is to group our input vectors into a single matrix X and process the entire batch with a single matrix multiplication.
The following article breaks down this crucial transition, from computing the output for a single neuron to processing an entire batch of data for a full layer in one go.
Basic matrix maths for neural networks: the theory
This article from Giles' blog provides an excellent step-by-step construction of how matrix multiplication is used to scale up neural network calculations.
First, read the section Matrix maths for one neuron. Notice how the weighted sum for a single neuron is simply a dot product, which can be expressed as a matrix-vector multiplication. Next, read Multiple neurons. This shows how by stacking the weight vectors for each neuron into the rows of a single weight matrix W, we can calculate the pre-activations for all neurons in the layer simultaneously. Finally, read the section on Batches. This is the key step. See how by arranging multiple input vectors as columns in an input matrix X, we can process the entire batch with the single, compact equation Z = WX, where Z is the matrix of resulting pre-activations.
The final equation, Z = WX, is the heart of the matter. If our weight matrix W has dimensions (number of neurons, number of input features) and our input batch X has dimensions (number of input features, number of samples in batch), the resulting matrix Z will have dimensions (number of neurons, number of samples in batch). Each column of Z is the pre-activation vector for the corresponding input sample in the batch. All of these calculations happen within one single, highly optimized operation.
The image below provides a great visual summary of this process for a simple network. The input X (a column vector in this case, representing one sample), the weights W_H and W_O, and the biases are all represented as matrices. The forward pass is a sequence of matrix multiplications and additions.

The Magic of Parallelism: Why Matrix Operations are Fast
Why is one large matrix multiplication so much faster than many small, sequential ones? The answer lies in the architecture of modern computer hardware, particularly Graphics Processing Units (GPUs). This principle is often called vectorization.
Instead of operating on one number at a time, vectorized operations process entire arrays—or vectors—of data at once. Modern CPUs and especially GPUs have multiple processing units (cores) that can perform calculations in parallel. Matrix multiplication is an operation that is "embarrassingly parallel," meaning it can be easily broken down into many independent smaller calculations (dot products) that can be executed simultaneously across these cores.
The following videos give a fantastic intuition for why this is so powerful.
Why do we use matrices for neural networks?
This video from Leximus Deep Learning provides a clear, visual comparison between a sequential and a parallel approach to calculating a neural network layer's output.
Watch the segment from this section. Pay close attention to the visual breakdown of the steps required. The sequential "for loop" approach requires 18 steps, whereas the parallel approach, by performing all independent multiplications and then additions simultaneously, reduces this to just 4 steps—a 4.5x speedup in this small example.
The next video demonstrates this with actual Python code, showing the dramatic performance difference between a traditional for loop and a vectorized operation using the NumPy library.
Vectorization in Python : Data Science Code
This video from ritvikmath explains the concept of vectorization and demonstrates its incredible impact on performance.
First, watch the conceptual explanation of vectorization from the beginning. The key idea is that independent operations can be split across multiple processor cores and executed simultaneously. Now, see it in action. Skip to the code demonstration for element-wise addition starting at this point. A for loop takes nearly 10 seconds to add two lists of 10 million numbers, while the vectorized NumPy operation x + y takes less than 0.1 seconds—a speedup of over 100x. Finally, watch the matrix multiplication example from here. The difference is even more staggering: a naive implementation with nested loops takes over 7 seconds for a 200x200 matrix, while NumPy's np.dot() function is over 7,500 times faster.
This massive speedup is why libraries like NumPy, TensorFlow, and PyTorch are central to machine learning. They are built on top of highly optimized, low-level libraries (like BLAS, CuBLAS) that are specifically designed to execute these matrix operations on parallel hardware like GPUs. Without this, training a model like AlphaFold or a large language model, which involves trillions of calculations, would be simply impossible.
The core idea is further explained in this excerpt on batched operations.
Matrix Multiplication: The Engine of Deep Learning | TensorTonic
This section from TensorTonic explicitly discusses batch matrix multiplication and its connection to GPU parallelism.
Please read the section Batched Operations. It formally shows the dimensions for batch matrix multiplication (B x M x N) * (B x N x P) = (B x M x P), where B is the batch size. The crucial takeaway is the sentence under "GPU Parallelism": "All [B] matrix multiplications execute simultaneously on GPU. This is why batch size matters for training throughput."
Efficiency at Scale: Distributed and Fault-Tolerant Computation
The principle of parallelization doesn't stop at a single GPU. For the largest models in existence, computations are distributed across hundreds or thousands of machines in a data center. Even in this scenario, the fundamental operation remains matrix multiplication.
This introduces new challenges, such as handling the failure of one of the "worker" machines. Researchers have developed clever strategies to make these large-scale computations fault-tolerant.

In the diagram above, you can see two approaches:
- (a) Replicated Task: The simplest way to handle failure is to assign the same sub-problem (e.g.,
A₁X) to multiple workers. If one fails, the master can get the result from the other. This is robust but inefficient. - (b) Coded Task: This is a much more sophisticated approach. The master sends carefully constructed combinations of the data (
A₁X,A₂X,(A₁+A₂)X) to the workers. With this "coded" information, the master can reconstruct the full resultAXeven if one of the workers fails, without needing to perform a full redundant computation. This touches on advanced topics like erasure codes, but it demonstrates how the core ideas of linear algebra extend to building robust, large-scale AI systems—a key concern in real-world research and deployment.
Conclusion
In this lesson, we uncovered the practical motivation behind using matrix algebra in machine learning. It's not just about mathematical elegance; it's about computational feasibility.
Key Takeaways:
- Batching: By stacking multiple input samples into a single matrix
X, we can process an entire batch through a neural network layer using one matrix multiplication,WX. - Vectorization: This is the principle of applying operations to entire arrays at once, rather than element by element in a loop.
- Parallelism: Matrix multiplication is highly parallelizable. This allows modern hardware like GPUs, with their thousands of cores, to execute the vast number of calculations required for deep learning with immense speed.
- Efficiency: The speedup from vectorization is not minor—it can be orders of magnitude (100x to 10,000x or more), making the difference between a model that trains in hours and one that would take years.
You now understand not just what a neural network computes (g(Wx+b)), but also how that computation is performed efficiently at scale.
In the next module, we will begin our journey into the "other half" of machine learning. So far, we've focused on the forward pass—how a network with given weights produces an output. Next, we will start exploring the inverse problem: how do we find the right weights in the first place? This will lead us into the geometry of linear systems, the concept of a solution space, and eventually, to the optimization algorithms that "teach" a model by adjusting its weights. We'll start by examining linear combinations and what it means for a set of vectors to be linearly independent.