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))
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With