最大池化层的作用:
(1)首要作用,下采样
(2)降维、去除冗余信息、对特征进行压缩、简化网络复杂度、减小计算量、减小内存消耗等
(3)实现非线性、
(4)扩大感知野。
(5)实现不变性,其中不变形性包括,平移不变性、旋转不变性和尺度不变性。
官方参考文档:
MAXPOOL2D
重要的 参数:
相关计算公式:
下面举一个例子:
通过对一个数组操作,获取其平均采样值:
import torch
from torch import nn
from torch.nn import MaxPool2d
input = torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1,]],dtype=torch.float32)
input = torch.reshape(input,(-1,1,5,5))
print(input.shape)
class Siri(nn.Module):
def __init__(self):
super(Siri, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=False)
def forward(self,input):
output = self.maxpool1(input)
return output
siri = Siri()
output = siri(input)
print(output)
ceil是向上取整,floor是向下取整,默认为ceil_mode=False
当ceil_mode=False时:
torch.Size([1, 1, 5, 5])
tensor([[[[2.]]]])
当ceil_mode=True时:
torch.Size([1, 1, 5, 5])
tensor([[[[2., 3.],
[5., 1.]]]])
下面是对CIFAR10数据集的操作:
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)
class Siri(nn.Module):
def __init__(self):
super(Siri, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool1(input)
return output
siri = Siri()
step = 0
writer = SummaryWriter("logs_maxpool")
运行结果: