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
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
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))
)
The results from nn.functional.max_pool1D
and nn.MaxPool1D
will be similar by value; though, the former output is of type torch.nn.modules.pooling.MaxPool1d
while the latter output is of type torch.Tensor
; this difference gives you different options as well; as a case in point, you can not call size/ shape on the output of the nn.MaxPool1D
while you can call either of these functions on nn.function.max_pool1D
.
I believe this should be similar in maxpool2D
as well.