DL Notes
Understanding forward propagation — how neural networks compute predictions by passing inputs through layers of weights, biases, and activation functions.
Forward propagation is how a neural network makes predictions. Data flows from input to output, layer by layer, through a sequence of linear transformations and non-linear activations. Every prediction your network makes — whether classifying an image or generating text — happens through forward propagation.
The Core Idea
Given an input, forward propagation computes the output by applying each layer's transformation sequentially:
Input → Layer 1 → Activation → Layer 2 → Activation → ... → Output
At each layer:
z = W·a + b (linear transformation)
a = σ(z) (non-linear activation)
Mathematical Formulation
For a network with L layers:
Layer l computation:
$$\mathbf{z}^{[l]} = \mathbf{W}^{[l]} \mathbf{a}^{[l-1]} + \mathbf{b}^{[l]}$$
$$\mathbf{a}^{[l]} = g^{[l]}(\mathbf{z}^{[l]})$$
Where:
- $\mathbf{a}^{[0]} = \mathbf{x}$ (the input)
- $\mathbf{W}^{[l]}$ is the weight matrix for layer l
- $\mathbf{b}^{[l]}$ is the bias vector for layer l
- $g^{[l]}$ is the activation function for layer l
- $\mathbf{a}^{[L]}$ = $\hat{\mathbf{y}}$ (the final prediction)
Step-by-Step Example
Let's trace through a concrete 2-layer network classifying a 3-feature input into 2 classes:
Vectorized Forward Pass (Batch Processing)
In practice, we process entire batches simultaneously using matrix operations:
Forward Propagation in PyTorch
PyTorch handles forward propagation through the forward() method and autograd tracks operations for backpropagation automatically:
Computational Complexity
For a layer with n_in inputs and n_out outputs, processing a batch of m samples:
- Matrix multiply: O(m × n_in × n_out)
- Bias addition: O(m × n_out)
- Activation: O(m × n_out)
For a full network with layers [784, 256, 128, 10] and batch size 32:
- Layer 1: 32 × 784 × 256 = 6,422,528 multiply-adds
- Layer 2: 32 × 256 × 128 = 1,048,576 multiply-adds
- Layer 3: 32 × 128 × 10 = 40,960 multiply-adds
Total: ~7.5 million operations per forward pass. GPUs execute these in parallel.
Forward Pass vs Inference
| Aspect | Training Forward Pass | Inference |
|---|---|---|
| Gradient tracking | Yes (autograd) | No (torch.no_grad()) |
| Dropout | Active (drops neurons) | Inactive |
| Batch Norm | Uses batch statistics | Uses running averages |
| Memory usage | High (stores intermediates) | Low |
| Speed | Slower | Faster |
# During inference, disable gradient computation for efficiency
model.eval() # Switch to inference mode
with torch.no_grad(): # Don't track gradients
predictions = model(test_input)Key Takeaways
- Forward propagation is a sequence of matrix multiplications followed by non-linear activations
- Each layer transforms the representation — early layers detect simple features, later layers detect complex ones
- Batch processing (vectorization) makes forward propagation efficient on GPUs
- During training, intermediate values are cached for backpropagation
- During inference, we disable gradient tracking and dropout for speed and correctness
- The computational cost is dominated by matrix multiplications — this is why GPUs matter
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Forward Propagation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Deep Learning topic.
Search Terms
deep-learning, deep learning, deep, learning, neural, network, fundamentals, forward
Related Deep Learning Topics