liveBook · Manning

Forward

As explained in Deep Learning with PyTorch, Second Edition

The standard module-computation entry point in PyTorch, invoked by __call__ and used for the main forward pass.

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

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains method definition, submodule use, invocation mechanics, hooks, and autograd’s implicit backward pass.
Examples
Demonstrates nn.Linear invocation and a convolutional Net whose forward method reshapes intermediate tensors.
What it assumes
Requires nn.Module subclassing and super().__init__() before assigning submodules or their parameters.
What you can do after
Build custom nn.Module subclasses with explicit data-flow logic and invoke them safely through the module call interface.
How it connects
Connects nn.Module, __call__, hooks, autograd, nn.Sequential, convolutional layers, pooling, and linear layers.

Also explained in

Compare how 2 books explain forward →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.