Search notes:

Inherit from torch.nn.Module

The following simple script defines a class, FWD, which inherits from torch.nn.Module. The class implements one method only: forward.
Instances of classes that inherit from nn.Module (such as fwd in this script) are callable. When they're called (fwd(…)), under the hood, the forward method gets invoked.
import torch

class FWD(torch.nn.Module):

  def forward(self, x):
      print(f'forward, x = {x}')
      return x*2


fwd = FWD()

tns = torch.tensor([
           [ 1, 3 ],
           [ 2, 4 ]
      ])

#
#  Verify existence of __call__ member (which makes an object callable):
#
print(fwd.__call__)

#
#  call object:
#
print(fwd(tns))

See also

torch.nn.Module:wa

Index