Confusion regarding use of forward function while developing a CNN model

when we define a class for a NN or CNN, in the init function we define the model and in the forward function we pass the images.
But when we need the outputs while training why do we write model(images) instead of model.forward(images)?

model here is the object of CNN/NN class.

Hi,

Actually, it is about python programming language. forward() function overrides __call__() function so when you call your class like CNNClass() it calls __call__() function which in pytorch case it is forward() function. So


model = CNNClass()
out = model(image)

Is same as:

model = CNNClass()
out = model.forward(image)

But the first one is the conventional method.

By the way, it would be great if you paste codes as text not image. :wink:

Good luck

Thank you so much! And yeah, I’ll paste the code next time! :sweat_smile:

This section of the docs talks about it:
https://pytorch.org/docs/stable/nn.html?highlight=forward#torch.nn.Module.forward