【PyTorch】教程:torch.nn.maxpool2d

torch.nn.MaxPool2d

CLASS torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)

2D 最大池化;
输入 ( N , C , H , W ) (N, C, H, W) (N,C,H,W), 输出 ( N , C , H o u t , W o u t ) (N, C, H_{out}, W_{out}) (N,C,Hout,Wout) kernel_size ( k H , k W ) (kH, kW) (kH,kW)

o u t ( N i , C j , h , w ) = max ⁡ m = 0 , … , k H − 1 max ⁡ n = 0 , … , k W − 1 input ( N i , C j , stride[0] × h + m , stride[1] × w + n ) \begin{aligned} out(N_i, C_j, h, w) ={} & \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1} \\ & \text{input}(N_i, C_j, \text{stride[0]} \times h + m, \text{stride[1]} \times w + n) \end{aligned} out(Ni,Cj,h,w)=m=0,,kH1maxn=0,,kW1maxinput(Ni,Cj,stride[0]×h+m,stride[1]×w+n)

参数

  • kernel_size (Union [ int, Tuple [int, int]] ) – 池化窗口大小
  • stride (Union [ int, Tuple [int, int]]) – 滑动步长,默认为 kernel_size
  • padding (Union [ int, Tuple [int, int]]) – 扩边
  • dilation (Union [ int, Tuple [int, int]]) – 控制窗口中元素的步长
  • return_indices ([bool]) - 如果为 True, 返回最大值和索引;
  • ceil_mode ([bool]) – 如果为 True, 使用 ceil 代替 floor 计算输出形状

形状

  • Input: ( N , C , H i n , W i n ) (N, C, H_{in}, W_{in}) (N,C,Hin,Win) or ( C , H i n , W i n ) (C, H_{in}, W_{in}) (C,Hin,Win)
  • Output: ( N , C , H o u t , W o u t ) (N, C, H_{out}, W_{out}) (N,C,Hout,Wout) or ( C , H o u t , W o u t ) (C, H_{out}, W_{out}) (C,Hout,Wout) 其中
    H o u t = ⌊ H i n + 2 ∗ padding[0] − dilation[0] × ( kernel_size[0] − 1 ) − 1 stride[0] + 1 ⌋ H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]} \times (\text{kernel\_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor Hout=stride[0]Hin+2padding[0]dilation[0]×(kernel_size[0]1)1+1
    W o u t = ⌊ W i n + 2 ∗ padding[1] − dilation[1] × ( kernel_size[1] − 1 ) − 1 stride[1] + 1 ⌋ W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]} \times (\text{kernel\_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor Wout=stride[1]Win+2padding[1]dilation[1]×(kernel_size[1]1)1+1

示例

# pool of square window of size=3, stride=2
m = nn.MaxPool2d(3, stride=2)
# pool of non-square window
m = nn.MaxPool2d((3, 2), stride=(2, 1))
input = torch.randn(20, 16, 50, 32)
output = m(input)
print(output.size()) # torch.Size([20, 16, 24, 31])

【参考】

MaxPool2d — PyTorch 1.13 documentation

猜你喜欢

转载自blog.csdn.net/zhoujinwang/article/details/129290314