小土堆-pytorch框架学习-P19-最大池化的使用

MaxPool官方文档

一般用MaxPool2d

参数👇

  • kernel_size (Union[int, Tuple[int, int]**]) – the size of the window to take a max over——卷积核,一般3$\times$3
  • stride (Union[int, Tuple[int, int]**]) – the stride of the window. Default value is kernel_size——步长一般与卷积核大小一致
  • padding (Union[int, Tuple[int, int]**]) – Implicit negative infinity padding to be added on both sides——是否在图像边缘进行填充
  • dilation (Union[int, Tuple[int, int]**]) – a parameter that controls the stride of elements in the window——是否在进行卷积时,隔一个取一个值?
  • return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
  • ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape——如果设置为true,则保留边缘的特征,否则不保留

image-20230705105527142

简单的使用代码示例👇

import torch
from torch import nn

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]])

class Tudui(nn.Module):
    def __init__(self):#这里忘记怎么写
        super(Tudui ,self).__init__()
        self.maxpool = nn.MaxPool2d(kernel_size = 3,
                                    ceil_mode =True)
        print(f"self.maxpool is {self.maxpool}")

    def forward(self , input):
        output = self.maxpool(input)
        return output

tudui = Tudui()
output = tudui(input)
print(f"output = {output}")
print(f"type of output = {type(output)}")
print("="*111)

直接运行会出错,运行结果👇

image-20230705112105805

问题:给定张量输入不对,需要3维/4维输入张量,但是现在我只给定了2维张量,需要对其重构。知道长宽和宽度,但不知道每次的minibatch1

解决方法有两种:

  1. 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]])
    print(F"the orignal shape of input is {input.shape}")
    # input = torch.reshape(input ,(-1 ,1,5,5) )#如果没有加这一行,会有报错
    print(F"the final shape of input is {input.shape}")
    # 增加一个维度,将2D张量转换为3D张量
    
  2. 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]])
    print(F"the orignal shape of input is {input.shape}")
    input= input.unsqueeze(0).unsqueeze(0).float()#增加维度
    print(F"the final shape of input is {input.shape}")
    

使用数据集在tensorboard中查看👇

test_data = torchvision.datasets.CIFAR10(root = "",
											train = False,
                                        transforms = torchvison.transforms.ToTensor())
data_loader = DataLoader(dataset =test_data ,  batch_size = 64)

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("tb_logs/maxpool_log")
step = 0
for data in data_loader:
    imgs , targets = data
    print(F"imgs = {imgs}")
    print(F"imgs.shape =  {imgs.shape}")
    print(F"imgs.type = {type(imgs)}")
    writer.add_images("maxpool_input" ,imgs, step )
    print(" "*111)
    output = tudui(imgs)
    print(F"output = {output}")
    print(F"output.shape = {output.shape}")
    print(F"output.type = {type(output)}")
    writer.add_images("maxpool_output",output, step)
    step+=1
    
writer.close() 

运行结果👇

image-20230705143710646

image-20230705144206439




  1. https://pytorch.org/docs/stable/generated/torch.nn.functional.conv2d.html#torch.nn.functional.conv2d ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelpFireCode

随缘惜缘不攀缘。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值