Why the **forward** function is never be called

As the example below, I’ve never seen forward function called, but when the Network class is initialized, forward function still run?
Can someone help me to explain this?

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        
    def forward(self, x):
        return x
3 Likes

The forward function gets called when you call it. “Network class is initialized?” You mean you instantiate your class “Network”. This will only call your constructor. You think that will lead to an async process? Which is not happen. I’m not sure if I understand you correctly. You might want to share more code.

@anhco989 When the Network class is instantiated (Ex: net = Network()), all the statements within __init__ are executed (the constructor). Later, when you run your network on some batch of data, you write output = net(x), which invokes the __call__ method. Your Network class simply inherits the __call__ method of the nn.Module class. It is here where the forward method is called.

7 Likes

It really makes sense. Thank you