Course contentsShow
← Machine Learning and Deep Learning
Lesson 803 of 3,53819. PyTorch FundamentalsFree lesson

The Forward Method: Defining Model Computation

Understand how the forward method defines the computation graph and why you call the module itself, not forward directly.

The Forward Method: Defining Model Computation

What you'll learn: How the forward method acts as the blueprint for your neural network's computation and why PyTorch calls it automatically.

The Core Idea

When you create a custom nn.Module, the forward method is where you define exactly what happens when data flows through your model. Think of it like writing a recipe: the ingredients are your layers (defined in __init__), and the forward method is the step-by-step cooking instructions.

Here's the interesting part: you never call forward() directly. Instead, you call the module itself like a function, and PyTorch automatically invokes forward behind the scenes.

Why This Design Matters

class SimpleNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)
    
    def forward(self, x):
        # Define the computation: what happens to input x?
        return self.linear(x)

model = SimpleNet()
output = model(input_tensor)  # Calls forward() automatically!
# DON'T do: model.forward(input_tensor)  # Works, but bypasses hooks

When you call model(input_tensor), PyTorch's __call__ method runs first, which:

  1. Handles autograd graph construction
  2. Executes any registered hooks
  3. Calls your forward method
  4. Returns the result

Calling forward directly skips these important steps and breaks functionality you'll rely on later.

The Forward Method as Your Computation Graph

Every operation inside forward creates nodes in the computational graph (which enables autograd to compute gradients). The order of operations matters—this is where your architectural choices come to life.

Key Takeaway: The forward method defines your model's computation flow. Always call the module itself (model(x)), not forward directly, to ensure PyTorch's machinery works correctly.