Is model(input) same as model.__call__(input)

Let’s say I defined the a model class. Now I want to implement a train function inside the class for which i need to call the model. How should i do that.

Which one is the right way to do this :
def train(self,x):


def train(self,x):

Yes model(input) and model.__call__(input) are the same.
It is the python convention to have the __call__ method being called when you use foo().
In this case, you can do self.__call__(x) (or self(x) if that works but I’m not sure it does).

@shagunsodhani No this is not the right way !! You should not call .forward() directly ! If you do this, many of the nn.Module functionality won’t work (hooks for example).

@albanD you are right. I confused it with something else :slight_smile:

1 Like