MaxPool2d
- API
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
- 参数
kernel_size (Union[int, Tuple[int, int]]) – 要占用最大值的窗口大小
stride (Union[int, Tuple[int, int]]) – 窗口的步幅。默认值为 `kernel_size`
padding (Union[int, Tuple[int, int]]) – 要在两边添加的隐式负无穷大填充
dilation (Union[int, Tuple[int, int]]) – 控制窗口中元素步幅的参数
return_indices (bool) – 如果 `True` ,将返回最大索引和输出。对`torch.nn.MaxUnpool2d` 以后有用
ceil_mode (bool) – 当为 True 时,将使用 ceil (向上取整)而不是 floor(向下取整) 来计算输出形状,即即使最后剩余数据不够stride也要进行计算
- 代码示例
'''
tensor([[[[2., 3.],
[5., 1.]]]])
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
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)
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
input = torch.reshape(input,(-1,1,5,5))
class maxpool_test(nn.Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.maxpool1 = MaxPool2d(kernel_size=3,stride=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool1(input)
return output
test = maxpool_test()
output = test(input)
print(output)
- 作用:减少数据信息但是不减少特征