Multilayer Perceptrons › Batch Processing with Matrix Operations
Language AI HandbookAll chapters Multilayer Perceptrons › Batch Processing with Matrix OperationsOne section · Sections in this chapter5 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The forward pass we implemented processes one example at a time, but this is inefficient in practice. Modern hardware, especially GPUs, is designed for parallel computation. By processing multiple examples simultaneously, we can achieve dramatic speedups.
For a batch of examples, the input becomes a matrix of shape . The forward pass equations generalize elegantly:
where:
- : the activation matrix from the previous layer, with shape
- : the weight matrix with shape
- : the pre-activation matrix with shape
- : the bias vector with shape , broadcast across all columns
- : the batch size
The matrix multiplication computes the linear transformation for all examples at once. The bias vector is broadcast (replicated) across all columns.
def forward_pass_batch(
X, weights, biases, hidden_activation=relu, output_activation=sigmoid
):
"""
Compute forward pass for a batch of inputs.
Args:
X: Input matrix of shape (n_features, batch_size)
weights: List of weight matrices
biases: List of bias vectors
Returns:
activations: List of activation matrices for each layer
pre_activations: List of pre-activation matrices for each layer
"""
A = X if X.ndim == 2 else X.reshape(-1, 1)
activations = [A]
pre_activations = []
for l in range(len(weights)):
Z = weights[l] @ A + biases[l]
pre_activations.append(Z)
if l == len(weights) - 1:
A = output_activation(Z)
else:
A = hidden_activation(Z)
activations.append(A)
return activations, pre_activations
# Create a batch of 5 examples
X_batch = np.random.randn(2, 5)
activations_batch, _ = forward_pass_batch(X_batch, weights, biases)Batch input shape: (2, 5) (2 features, 5 examples) Batch output shape: (1, 5) (1 output, 5 examples) Outputs for each example: Example 1: 0.5000 Example 2: 0.5000 Example 3: 0.5000 Example 4: 0.5528 Example 5: 0.5000
All five examples are processed in a single matrix operation, producing five outputs simultaneously. Batch processing also provides more stable gradient estimates during training, as we will see in the Backpropagation chapter.