What is "forward" and when must you define it?

Hello, I am a bit confused on when you need to have a “forward” method in a NN module or a custom transform class. Can someone provide some more information?

Who calls forward? When does it get called? And when do you need to have it? I have named some of my modules other than forward and everything works, can someone explain?

forwards plays the same role as __call__ does for a regular python class.
Basically when you run model(input) this calls internally forward + some extra code around this function to add functionalities.
This way they pytorch ensures you don’t rewrite __call__ but an undefined method: forward.

Any custom module that you write (anything which inherits from a nn.Module) needs to define this function. Official layers already define it.

1 Like