The forward method defines how data moves through a neural network. It tells the model what to do with input to get output.
forward method in PyTorch
def forward(self, x): # define how input x passes through layers x = self.layer1(x) x = self.layer2(x) return x
The forward method is inside a class that inherits from torch.nn.Module.
You do not call forward directly; instead, call the model instance with input.
forward method applies this layer to input x.class SimpleModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(10, 5) def forward(self, x): return self.linear(x)
forward.def forward(self, x): x = self.conv1(x) x = torch.relu(x) x = self.pool(x) return x
This program defines a small model with two linear layers and a ReLU activation in between. It shows how forward controls data flow. The output is the model's prediction for the input tensor.
import torch import torch.nn as nn class MyModel(nn.Module): def __init__(self): super().__init__() self.linear1 = nn.Linear(4, 3) self.relu = nn.ReLU() self.linear2 = nn.Linear(3, 2) def forward(self, x): x = self.linear1(x) x = self.relu(x) x = self.linear2(x) return x # Create model instance model = MyModel() # Sample input tensor (batch size 1, 4 features) input_tensor = torch.tensor([[1.0, 2.0, 3.0, 4.0]]) # Get model output by calling the model output = model(input_tensor) print("Output:", output)
Always call the model instance (e.g., model(input)) instead of calling forward directly.
The forward method can include any Python code to process data step-by-step.
Use self to access layers defined in __init__.
The forward method defines how input data moves through your model.
It is required when creating custom PyTorch models by subclassing nn.Module.
Call the model instance with input to run forward and get output.