Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between nn.MaxPool2d vs.nn.functional.max_pool2d?

Whats the difference between: nn.MaxPool2d(kernel_size, stride) and nn.functional.max_pool2d(t, kernel_size, stride)?

The first one I define in the module and the second in the forward function?

Thanks

like image 464
gab Avatar asked Oct 23 '19 01:10

gab


1 Answers

They are essentially the same. The difference is that torch.nn.MaxPool2d is an explicit nn.Module that calls through to torch.nn.functional.max_pool2d() it its own forward() method.

You can look at the source for torch.nn.MaxPool2d here and see the call for yourself: https://pytorch.org/docs/stable/_modules/torch/nn/modules/pooling.html#MaxPool2d

Reproduced below:

def forward(self, input):
        return F.max_pool2d(input, self.kernel_size, self.stride,
                            self.padding, self.dilation, self.ceil_mode,
                            self.return_indices)

Why have two approaches for the same task? I suppose it's to suit the coding style of the many people who might use PyTorch. Some prefer a stateful approach while others prefer a more functional approach.

For example having torch.nn.MaxPool2d means that we could very easily drop it into a nn.Sequential block.

model = nn.Sequential(
          nn.Conv2d(1,3,3),
          nn.ReLU(),
          nn.MaxPool2d((2, 2))
        )
like image 80
JoshVarty Avatar answered Oct 08 '22 09:10

JoshVarty