nn.MaxPool2d
时间: 2023-09-11 12:04:34 浏览: 47
nn.MaxPool2d is a function in PyTorch's nn module that performs max pooling operation on 2D inputs (images). Max pooling is a type of pooling operation in convolutional neural networks (CNNs) that reduces the spatial dimensions of the input while preserving the most important features.
The function takes several arguments, including the kernel size of the pooling window, stride, padding, and dilation. The input tensor is divided into non-overlapping rectangular blocks of size kernel_size, and the maximum value in each block is taken as the output. The stride parameter controls the spacing between the blocks, while padding adds extra values around the input tensor to ensure that the kernel can be applied to the borders. Dilation controls the spacing between the values within the kernel.
Here's an example of how to use nn.MaxPool2d:
```
import torch.nn as nn
maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
input_tensor = torch.randn(1, 3, 28, 28) # batch size, number of channels, height, width
output_tensor = maxpool(input_tensor)
```
In this example, we create a max pooling layer with a kernel size of 2 and a stride of 2. We then apply this layer to an input tensor of size (1, 3, 28, 28), which represents a batch of 1 RGB image with a height and width of 28 pixels. The output tensor will have a size of (1, 3, 14, 14), with the height and width reduced by half due to the max pooling operation.