A lot of the PyTorch tutorials I've been viewing do something like this.
Define model:
class Network(nn.Module):
def __init__():
super().__init__()
self.conv1 = ..
...
def forward(x)
...
...
Once the Network has been instantiated (net = Network()), the people in the tutorials write net(input_data) instead of net.forward(input_data).
I tried net.forward() and it gives the same results as net().
Why is this a common practice, and also why does this work?
You should avoid calling Module.forward.
The difference is that all the hooks are dispatched in the __call__ function see this, so if you call .forward and have hooks in your model, the hooks won’t have any effect.
Inshort when you call Module.forward, pytorch hooks wont have any effect
Detailed answer can be found in this post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With