input = torch.Tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,1],
[5,3,4,2,1],
[2,1,0,1,1]])
# kernel = torch.Tensor([[0,1,1],
# [0,1,0],
# [1,0,1]])
# print(input.shape)
input =torch.reshape(input,(1,1,5,5))
# torch.reshape(kernel,(1,1,3,3))
# print(input.shape)
pooling = nn.MaxPool2d(2)
output = pooling(input)
print(output)
pooling = nn.MaxPool2d(2)表示每2*2的一块,取最大值变成一个
因此必须reshape成(1,1,5,5)的格式
示例2
import torch
from torch import nn
# 重写类,快捷键Alt+insert
class module(nn.Module):
def __init__(self):
super().__init__()
self.maxpooling = nn.MaxPool2d(kernel_size=2,ceil_mode=True)
def forward(self, input):
output = self.maxpooling(input)
return output
input = torch.Tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,1],
[5,3,4,2,1],
[2,1,0,1,1]])
input =torch.reshape(input,(1,1,5,5))
# 新建对象
module = module()
output =module(input)
print(output)