Pytorch 神经网络模块之 Pooling layers
1. torch.nn.MaxPool2d
nn.MaxPool2d 是二维池化的方法,主要是针对图像,函数原型如下:
""" kernel_size - 表示做最大池化的窗口大小,可以是单个值,也可以是 tuple 元组 stride - 步长,可以是单个值,也可以是 tuple 元组 padding - 填充,可以是单个值,也可以是 tuple 元组 dilation - 控制窗口滑动步幅 return_indices - ceil_mode - """ class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
它的输入张量的形状必须是 $(batchSize, channel, height, width)$,举个例子:
import torch
input = [3,4,6,5,
2,4,6,8,
1,6,7,8,
9,7,4,6]
# (batchSize, channel, height, width)
input = torch.Tensor(input).view(1, 1, 4, 4)
# 创建一个 2x2 的最大池化层
maxpooling_layer = torch.nn.MaxPool2d(kernel_size=2)
output = maxpooling_layer(input)
print(output)
"""
tensor([[[[4., 8.],
[9., 8.]]]])
"""
浙公网安备 33010602011771号