0
0
PyTorchml~5 mins

forward method in PyTorch

Choose your learning style9 modes available
Introduction

The forward method defines how data moves through a neural network. It tells the model what to do with input to get output.

When building a custom neural network model in PyTorch.
When you want to specify how layers connect and process data.
When you need to control the flow of data for training or prediction.
When experimenting with new model architectures.
When debugging or understanding how your model works internally.
Syntax
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.

Examples
A simple model with one linear layer. The forward method applies this layer to input x.
PyTorch
class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)
    def forward(self, x):
        return self.linear(x)
Example showing multiple steps: convolution, activation, and pooling inside forward.
PyTorch
def forward(self, x):
    x = self.conv1(x)
    x = torch.relu(x)
    x = self.pool(x)
    return x
Sample Model

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.

PyTorch
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)
OutputSuccess
Important Notes

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__.

Summary

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.