3. Backpropagation & Mathematical Foundations
The Feedforward Mechanism
The feedforward process is the deterministic transformation of input data through successive layers of affine transformations followed by non-linear activations. Each layer calculates $z = W x + b$, where W is the weight matrix and b is the bias vector, followed by a non-linear activation function σ(z). This process constructs a complex, hierarchical feature representation of the input data.
Mathematically, the output for a network with L layers can be represented as a composite function: $f(x) = \sigma_L(W_L(...\sigma_1(W_1 x + b_1)...) + b_L)$. Understanding the chain rule application during this forward pass is crucial, as the intermediate activations cache the states necessary for the subsequent backpropagation step. In vectorized form, this is implemented using optimized matrix multiplication, which exploits hardware acceleration via GPUs.
import torch # Initialize input and weights for a single layer x = torch.randn(1, 10) W = torch.randn(10, 5) b = torch.randn(1, 5) # Perform affine transformation z = torch.matmul(x, W) + b # Apply ReLU activation activation = torch.relu(z) print(f'Layer output shape: {activation.shape}')
The Chain Rule of Calculus
Backpropagation is essentially the efficient application of the chain rule to compute partial derivatives of the cost function with respect to every parameter in the network. If y = f(g(x)), then dy/dx = (dy/dg) * (dg/dx). In a neural network, this allows us to compute the gradient of the loss L with respect to any weight w by traversing backward from the final output layer to the input.
For a parameter w_ij in layer l, the gradient is given by $\frac{\partial L}{\partial w_{ij}} = \frac{\partial L}{\partial z_l} \cdot \frac{\partial z_l}{\partial w_{ij}}$. Since z_l is a linear combination of previous activations, dz_l/dw_ij corresponds simply to the activation of the preceding neuron. By iteratively applying this, we compute the sensitivity of the global error to local weight changes, which is the foundational mechanism of network learning.
import torch # Require gradients for tracking w = torch.randn(5, 5, requires_grad=True) x = torch.randn(1, 5) y = torch.matmul(x, w) loss = y.sum() # Calculate gradients via Autograd (Backprop) loss.backward() # Access gradients for weight matrix print(f'Weight gradient shape: {w.grad.shape}')
Gradient Descent & Parameter Updates
Once the partial derivatives are calculated via backpropagation, we update the parameters using a gradient descent variant: $\theta = \theta - \eta \cdot \nabla L(\theta)$, where η is the learning rate. This step ensures that we move in the direction of the steepest descent in the loss landscape, effectively minimizing the discrepancy between predictions and ground truth labels.
In deep networks, this process must be carefully managed to avoid vanishing or exploding gradients. Standard techniques like Xavier or Kaiming initialization help ensure that activations and gradients maintain a healthy variance across layers during the initial phase of training. Proper hyperparameter tuning, specifically regarding the learning rate, is the final determinant of whether the network converges to a global minimum or gets trapped in suboptimal local basins.
import torch.optim as optim # Model parameters with optimizer model_params = [torch.randn(10, 5, requires_grad=True)] optimizer = optim.SGD(model_params, lr=0.01) # Gradient step optimizer.zero_grad() loss = (model_params[0].sum() - 1.0)**2 loss.backward() optimizer.step()
Knowledge Check
Ready to test your understanding of 3. Backpropagation & Mathematical Foundations?