引言:
在神经网络中,池化层是比较重要的,是提取信息的重要操作,可以去掉一些不重要的信息,从而减少计算量;
相关参数(MaxPool2d):
- kernel_size:表示做最大池化的窗口大小,可以是单个值,也可以是tuple元组;
- stride:步长,可以是单个值,也可以是tuple元组;
- padding:填充,可以是单个值,也可以是tuple元组;
- dilation:控制窗口中元素的步幅;
- return_indicex:布尔类型,返回最大值位置索引;
- ceil_mode:布尔类型,为True,用向上取整的方法,计算出输出形状;默认是向下取整;
代码展示:
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('data', train=False, download=True,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)
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))
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)
def forward(self, input):
output = self.maxpool1(input)
return output
tudui = Tudui()
writer = SummaryWriter('logs_maxpool')
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images('input', imgs, step)
output = tudui(imgs)
# output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images('output', output, step)
step += 1
writer.close()