Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Deep Learning Notes
Topic #249

Forward Propagation

Forward propagation is the process of pushing an input through every layer of a network, in order, to produce a final prediction. It's called "forward" to distinguish it from backpropagation, which runs in the opposite direction.

The General Formula, Layer by Layer

\[ \mathbf{a}^{(0)} = \mathbf{x}, \qquad \mathbf{z}^{(l)} = \mathbf{W}^{(l)}\mathbf{a}^{(l-1)} + \mathbf{b}^{(l)}, \qquad \mathbf{a}^{(l)} = \phi^{(l)}(\mathbf{z}^{(l)}) \]

Starting from the input \(\mathbf{a}^{(0)}=\mathbf{x}\), each layer \(l\) computes its pre-activation \(\mathbf{z}^{(l)}\) (a matrix multiplication plus bias) and then its activation \(\mathbf{a}^{(l)}\) (applying the layer's non-linearity). The final layer's activation is the network's prediction, \(\hat{\mathbf{y}} = \mathbf{a}^{(L)}\) for an \(L\)-layer network.

Full Numerical Walkthrough — A Tiny 2-Layer Network

Input \(\mathbf{x}=[1, 2]\). Layer 1: \(\mathbf{W}^{(1)} = \begin{bmatrix}0.1 & 0.2\\0.3 & 0.4\end{bmatrix}\), \(\mathbf{b}^{(1)}=[0.1, 0.1]\), ReLU activation.

\[ \mathbf{z}^{(1)} = \begin{bmatrix}0.1(1)+0.2(2)+0.1\\0.3(1)+0.4(2)+0.1\end{bmatrix} = \begin{bmatrix}0.6\\1.2\end{bmatrix}, \qquad \mathbf{a}^{(1)} = \text{ReLU}(\mathbf{z}^{(1)}) = \begin{bmatrix}0.6\\1.2\end{bmatrix} \]

Layer 2 (output): \(\mathbf{W}^{(2)}=[0.5, -0.5]\), \(b^{(2)}=0\), sigmoid activation.

\[ z^{(2)} = 0.5(0.6) + (-0.5)(1.2) + 0 = 0.3 - 0.6 = -0.3 \] \[ \hat{y} = \sigma(-0.3) = \frac{1}{1+e^{0.3}} \approx 0.426 \]

The network's final prediction for this input is approximately 0.426.

Code — The Same Computation in PyTorch

import torch
import torch.nn as nn

class TinyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Linear(2, 2)
        self.layer2 = nn.Linear(2, 1)

    def forward(self, x):
        a1 = torch.relu(self.layer1(x))    # layer 1: weighted sum + ReLU
        y_hat = torch.sigmoid(self.layer2(a1))   # layer 2: weighted sum + sigmoid
        return y_hat

model = TinyNet()
x = torch.tensor([[1.0, 2.0]])
prediction = model(x)   # this single call performs the entire forward pass
print(prediction)

Note that model(x) (equivalently, model.forward(x)) is exactly this note's formula, executed automatically — every "forward pass" you'll write for the rest of this hub follows this same layer-by-layer pattern, however many layers or however specialized (convolutional, attention-based) they are.

Common Mistakes

  • Applying an activation function to the very last output when the loss function already expects raw logits — e.g. applying sigmoid before nn.BCEWithLogitsLoss, which applies sigmoid internally for numerical stability.
  • Forgetting that forward propagation, by itself, does not involve any learning — it only computes a prediction from the network's current weights; learning happens afterward, in the backward pass and weight update.

Interview Relevance

Q: "Walk through what happens during a neural network's forward pass." The input is passed through each layer in sequence: at each layer, the previous layer's output is multiplied by that layer's weight matrix, a bias is added, and a non-linear activation function is applied — the result becomes the input to the next layer. This repeats until the final (output) layer, whose activation is the network's prediction.

Practice Question

For a 2-layer network with \(\mathbf{W}^{(1)}=[[1,0],[0,1]]\), \(\mathbf{b}^{(1)}=[0,0]\), ReLU, then \(\mathbf{W}^{(2)}=[1,1]\), \(b^{(2)}=0\), no output activation — compute the forward pass output for input \(\mathbf{x}=[-1, 3]\).

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →