Custom model layers
Create a reusable neural-network component whose parameters and child layers are registered by PyTorch.
PyTorch · Function
torch.nn.Module.forward defines the computation performed whenever a module is called. Subclasses override it to transform inputs into outputs, while calling the module instance preserves registered hook behavior.
Override forward in an nn.Module to describe the computation performed for each call. Invoke the module itself rather than calling forward directly, so PyTorch can run registered hooks around the computation.
forward(*input)| Name | Type | Description |
|---|---|---|
input | Any | Input values supplied to the module; the concrete types, count, and shapes depend on the subclass implementation. |
The output produced by the subclass's computation; its type and structure depend on the implementation.
import torch
from torch import nn
class Affine(nn.Module):
def __init__(self, features):
super().__init__()
self.weight = nn.Parameter(torch.randn(features, features))
self.bias = nn.Parameter(torch.zeros(features))
def forward(self, input):
return input @ self.weight + self.bias
model = Affine(3)
output = model(torch.ones(2, 3))
print(output.shape)Defines a module computation in forward and invokes it through the module instance.
Create a reusable neural-network component whose parameters and child layers are registered by PyTorch.
Express a tensor transformation for a training or inference pipeline.
Compose several operations or submodules into one callable model component.
Override forward to define the computation performed at every module call. The method can accept the module's inputs and return its output.
Follow-up: Why call the module instead of forward directly? The module call path runs registered hooks, while directly invoking forward silently ignores them.
A module may perform arbitrary computation involving any number of inputs and outputs. Its forward pass applies that computation to the supplied inputs.
Follow-up: How would you make the module participate in training? Use learnable nn.Parameter objects or child modules so their parameters are registered and exposed to the optimizer.
Define forward as an instance method and return the result of the desired tensor operations. Then invoke the module instance, which dispatches to forward and preserves hook behavior.
Follow-up: What should be avoided when hooks matter? Calling module.forward directly, because that route does not execute registered hooks.
torch.nn.Moduletorch.nn.Module.__call__torch.nn.Parametertorch.nn.modules.module.register_module_forward_pre_hook