neural networks
forward pass
backward pass
deep learning
machine learning

What are forward and backward passes in neural networks?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Forward and backward passes are the two core computations in neural network training. The forward pass turns inputs into predictions, and the backward pass computes gradients that tell the optimizer how to update parameters. Understanding both passes is essential for debugging unstable training, shape errors, and poor convergence. Most training instability can be traced to misunderstandings in one of these two phases rather than optimizer choice alone.

Forward Pass: From Input to Prediction

In the forward pass, each layer applies transformations to produce the next representation. For a dense layer, this is a matrix multiplication plus bias, followed by an activation function.

python
1import torch
2import torch.nn as nn
3
4x = torch.tensor([[0.2, -1.0, 0.5]], dtype=torch.float32)
5layer1 = nn.Linear(3, 4)
6layer2 = nn.Linear(4, 2)
7activation = nn.ReLU()
8
9h = activation(layer1(x))
10logits = layer2(h)
11print("logits:", logits)

No learning happens yet. The network only computes outputs using current parameter values.

Loss Connects Prediction to Target

Training needs a scalar objective to measure prediction quality.

python
1target = torch.tensor([1])
2loss_fn = nn.CrossEntropyLoss()
3loss = loss_fn(logits, target)
4print("loss:", float(loss.item()))

The loss value is the bridge between forward and backward passes.

Backward Pass: Gradient Computation via Chain Rule

The backward pass computes partial derivatives of loss with respect to each trainable parameter. Frameworks such as PyTorch build a computation graph during forward pass and then traverse it backward.

python
1loss.backward()
2
3for name, param in list(layer1.named_parameters()) + list(layer2.named_parameters()):
4    grad_norm = param.grad.norm().item()
5    print(name, "grad norm:", grad_norm)

Gradients quantify how much each parameter influences loss. Optimizers use these gradients to adjust weights.

Parameter Update Step

After gradients are computed, optimizer updates parameters and gradients are cleared.

python
1optimizer = torch.optim.SGD(
2    list(layer1.parameters()) + list(layer2.parameters()),
3    lr=0.05
4)
5
6optimizer.step()
7optimizer.zero_grad()

Without zero_grad, gradients accumulate across iterations by default, which can produce unintended updates.

Full Training Loop Structure

A typical supervised loop repeats this sequence for mini-batches.

python
1model = nn.Sequential(
2    nn.Linear(3, 8),
3    nn.ReLU(),
4    nn.Linear(8, 2),
5)
6optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
7loss_fn = nn.CrossEntropyLoss()
8
9for step in range(100):
10    batch_x = torch.randn(16, 3)
11    batch_y = torch.randint(0, 2, (16,))
12
13    preds = model(batch_x)         # forward
14    loss = loss_fn(preds, batch_y) # loss
15
16    optimizer.zero_grad()
17    loss.backward()                # backward
18    optimizer.step()               # update

This loop is the core of most deep learning training scripts.

Why Backward Pass Uses More Memory

Backward computation needs intermediate activations from forward pass. Deep models or large batch sizes can run out of memory quickly because activations are stored until gradients are computed.

Common mitigation options:

  • reduce batch size
  • mixed precision training
  • gradient checkpointing
  • smaller model depth during experiments

Memory planning is often as important as model architecture.

Forward vs Inference Mode

Inference uses forward pass only. No gradient graph is needed, so memory and compute can drop significantly.

python
1model.eval()
2with torch.no_grad():
3    out = model(torch.randn(4, 3))
4    print(out.shape)

Using evaluation mode and disabled gradients is important for stable and efficient production prediction.

Diagnostics That Map to Each Pass

Useful troubleshooting signals:

  • forward issues: invalid tensor shapes, exploding activations, poor feature scaling
  • backward issues: zero or NaN gradients, disconnected graphs, unsupported operations
  • update issues: too high learning rate, optimizer misconfiguration, missing gradient reset

Reading logs by pass stage helps isolate root causes quickly.

Common Pitfalls

A common mistake is assuming forward pass alone changes model weights. Parameter updates occur only after backward pass and optimizer step.

Another mistake is forgetting gradient reset, causing accumulation that distorts updates.

A third mistake is accidentally detaching tensors in the forward path, which blocks gradient flow.

Teams also mix training and evaluation modes incorrectly, causing unstable metrics due to dropout and batch normalization behavior.

Summary

  • Forward pass computes predictions from inputs using current parameters.
  • Backward pass computes gradients by applying chain rule over the computation graph.
  • Loss value links prediction quality to parameter updates.
  • Training loops require ordered forward, loss, backward, and optimizer steps.
  • Clear pass-level diagnostics make neural network debugging much faster.

Course illustration
Course illustration

All Rights Reserved.