Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling the forward method in PyTorch vs. calling the model instance

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?

like image 856
Shrey Joshi Avatar asked Nov 22 '25 02:11

Shrey Joshi


1 Answers

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

like image 58
Prajot Kuvalekar Avatar answered Nov 24 '25 16:11

Prajot Kuvalekar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!