池化层
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)
输出结果:
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)
输出结果:
|