池化层
MaxPool2d 
kernel_size :窗口的大小以达到最大值stride :窗口的步幅。默认值为kernel_size padding :要在两侧添加隐式零填充dilation : 控制窗口中元素步幅的参数return_indices :如果True ,将返回最大索引和输出。ceil_mode :当为 True 时,将使用ceil而不是floor来计算输出形状
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 Model(nn.Module):
def __init__(self):
super().__init__()
self.maxpool1 = MaxPool2d(kernel_size = 3, ceil_mode = True)
def forward(self, input):
output = self.maxpool1(input)
return output
model = Model()
output = model(input)
print(output)
输出结果: ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IjqDxYpe-1642952011381)(H:\codes\pytorch\note\池化.assets\image-20220107035218644.png)]](https://img-blog.csdnimg.cn/608bcce9735e44fbb9cc7c43980bbcf0.png)
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 Model(nn.Module):
def __init__(self):
super().__init__()
self.maxpool1 = MaxPool2d(kernel_size = 3, ceil_mode = False)
def forward(self, input):
output = self.maxpool1(input)
return output
model = Model()
output = model(input)
print(output)
输出结果:  
|