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:
- Handles autograd graph construction
- Executes any registered hooks
- Calls your
forwardmethod - 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.