Regarding Conv1d

Hi I couldn’t grammatically quite understand the following example

m = nn.Conv1d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 50)
output = m(input)

I think the type of nn.Conv1d is class, and accordingly m should be an instance of the class. Why in the third line, m(input) is used like a function?

Actually, it seems that >>> output = m(input) executes the function forward(self, input) defined in the class nn.Conv1d. So, to be grammatically correct, I think >>> output = m(input) should be changed into >>> output = m.forward(input) . But >>> output = m(input) indeed works.

Could anybody explain a bit about this? What grammatical rule does it follow?

The source code for Conv1d is at the following URL

Thank you very much!

nn.Module (which nn.Conv1d and all the nn. modules extend), implements the __call__ magic method so that it can be used like a function as in your example. The implementation can be found here. Using __call__ has some extra processing for forward/backward hooks, but, as you have found, its main job is to call forward()

Thank you so much for your explanation! Truly helpful!