About the 'nn.Module.forward'

I am new to pytorch. I read the code about how to define a network and call the forward function to generate output.

I am confused why “out=net(input)” , instead of “out=net.forward(input)” just like the standard form of calling a function defined in a class?
Thanks

4 Likes

When you call the model directly, the internal __call__ function is used. Have a look at the code.
This function manages all registered hooks and calls forward afterwards.
That’s also the reason you should call the model directly, because otherwise your hooks might not work etc.

11 Likes

Thank you. Understand.

To others, the question/code is related to this guide:
https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#define-the-network

2 Likes