In PyTorch, forward is the method that defines a module’s main computation: it receives inputs, applies the model’s operations, and returns outputs. Calling a module such as model(x) invokes __call__, which then runs forward and performs additional framework work. section 6.2.1 section 8.3
Use forward to define how data moves through a custom module.
- Subclass nn.Module and define a forward function that accepts the module’s inputs and returns its outputs. section 8.3
- Create layers and other submodules in __init__, assign them to self, and use them inside forward so their parameters persist with the module. section 8.3
- Call the module itself rather than calling forward directly; direct calls bypass hooks used during forward and backward passes. section 6.2.1
- Rely on autograd for the backward pass when the computation uses standard torch operations; nn.Module does not define a separate backward method. section 8.3
A convolutional network with an explicit forward pass
A custom Net can apply convolutions, activations, pooling, reshaping, and linear layers in a chosen sequence. Its forward method makes the data flow explicit and can manipulate intermediate outputs before returning predictions. section 8.3.1
# In[27]:
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
self.act1 = nn.Tanh()
self.pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(16, 8, kernel_size=3, padding=1)
self.act2 = nn.Tanh()
self.pool2 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(8 * 8 * 8, 32)
self.act3 = nn.Tanh()
self.fc2 = nn.Linear(32, 2)
def forward(self, x):
out = self.pool1(self.act1(self.conv1(x)))
out = self.pool2(self.act2(self.conv2(out)))
out = out.view(-1, 8 * 8 * 8) #1
out = self.act3(self.fc1(out))
out = self.fc2(out)
return out
Inspect a complete nn.Module subclass whose forward method chains convolutional, pooling, reshaping, activation, and linear operations. section 8.3.1

