ThaiThien
(Thai Thien)
June 4, 2017, 5:36pm
1
class MyModel(nn.Module):
def __init__(self, cuda, word_dim, tag_dim, mem_dim, criterion):
super(MyModel, self).__init__()
def forward(input):
.. # do something
return output
model = MyModel()
Is there any different if I called
model.forward(input)
rather than
model(input)
Because only when I call model.forward(input), IDE (in this case, Pycharm) suggest me argument for forward function.
12 Likes
fmassa
(Francisco Massa)
June 5, 2017, 5:18am
2
You should avoid calling Module.forward
.
The difference is that all the hooks are dispatched in the __call__
function, so if you call .forward
and have hooks in your model, the hooks won’t have any effect
18 Likes
But who/what exactly calls the .forward() function? is it the init ()? In my case I am being told in error that the .forward is not getting all the arguments it needs, but I am not able to figure out where in the control flow its getting lost.
2 Likes
As @fmassa said, forward
is called in the .__call__
function. Have a look at the line of code .
7 Likes
What so you mean by the “hooks”?
6 Likes
dngros
January 4, 2019, 7:45am
6
Just avoiding using forward directly doesn’t really solve the issue that it breaks IDE support. One potential solution could be this Solution to different forward for train and inference + IDE support for forward args . Here I just wrapped nn.Module to allow a forward method with both hooks and IDE support is retained.
ThaiThien
(Thai Thien)
July 19, 2020, 4:23am
7
I haven’t use “hooks” yet, but it is like event trigger.
Gillian
(Milani)
March 26, 2024, 1:12pm
8
Hello from 6 years later, here is a link targetting the commit instead of the main branch, showing the right line (as i suppose) pytorch/torch/nn/modules/module.py at d564ecb4a515e34184c976617f57b2eb01665660 · pytorch/pytorch · GitHub