Lesson 7: method of ConvLayer

In the definition of object ConvNet2, there are something I can’t understand:

def forward(self, x):
    for l in self.layers: x = l(x)

according to the definition of self.layers, self.layers is the object ConvLayer, I am confused by why x = l(x) rather than x=l.forward(x) can directly calls the method forward of ConvLayer?

That works because ConvLayer is a subclass of Pytorch’s nn.Module, whose __call__ method delegates to forward. You can check out the full implementation by doing nn.Module.__call__?? in a notebook.

Thank you very much, I’ve checked it out and I understand it now.

1 Like