WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Basic · question 13 of 100

What is the role of the forward() method in a custom PyTorch module?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

The ‘forward()‘ method is a crucial part of a PyTorch module, as it defines the computation that is performed when the module is applied to some input data.

In PyTorch, a neural network is represented as a series of connected layers or modules (such as ‘nn.Linear‘, ‘nn.Conv2d‘, ‘nn.LSTM‘, etc). Each of these modules has a ‘forward()‘ method that defines the computations performed by that module. When a module is applied to some input data, the ‘forward()‘ method is called, and it applies the computation defined by that module to the input data.

For example, consider a simple neural network with two linear layers:

import torch.nn as nn

class MyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(10, 20)
        self.fc2 = nn.Linear(20, 5)

    def forward(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        return x

In this example, we define a custom module ‘MyNet‘ that has two layers of ‘nn.Linear‘ modules. In the ‘forward()‘ method, we first pass the input ‘x‘ through ‘fc1‘. We then pass the output of ‘fc1‘ through ‘fc2‘, and return the final output.

When we apply this module to some input data, the ‘forward()‘ method is called and applies the computation defined by the module to the input data:

net = MyNet()
input_data = torch.randn(1, 10)
output = net(input_data)

Here, ‘net(input_data)‘ calls the ‘forward()‘ method of ‘MyNet‘, which applies the linear transformations defined by ‘fc1‘ and ‘fc2‘ to the input data ‘input_data‘. The result is the output of the module, which is returned as ‘output‘.

Therefore, the ‘forward()‘ method is essential for defining the computation that is performed by a PyTorch module, and provides the building blocks for constructing complex neural networks.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics