• 学习pytorch10 神经网络-最大池化的作用


    B站小土堆学习视频
    https://www.bilibili.com/video/BV1hE411t7RN?p=19&spm_id_from=pageDriver&vd_source=9607a6d9d829b667f8f0ccaaaa142fcb

    官方文档

    https://pytorch.org/docs/stable/nn.html#pooling-layers

    doc–>pytorch–>torch.nn–>Pooling layers

    上采样是插值,下采样是抽样,可以通俗的理解为逆运算???
    在这里插入图片描述

    参数说明

    池化核可以理解为卷积核,跟卷积核的使用方式一致,计算公式不太一致
    池化和卷积不同 一是核的使用不同,二是步长不同【如果步长是1 就达不到降采样的目的了】
    在这里插入图片描述

    运算演示

    ceil允许有出界操作,floor不允许出界操作
    最大池化是对池化核覆盖的区域取最大值操作,核里面可以没有数值
    在这里插入图片描述

    公式

    在这里插入图片描述

    最大池化

    1. 最大池化是对池化核覆盖的区域取最大值操作,核里面可以没有数值
    2. 最大池化的目的是 保留输入图像的特征,并减少数据量的计算。【训练网络的计算参数减少,则计算更快】
    3. 卷积提取特征 池化压缩特征

    代码

    code 1

    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)  # RuntimeError: "max_pool2d" not implemented for 'Long'
    
    print(input.shape)
    input = torch.reshape(input, (-1,1,5,5))
    print(input.shape)
    print(input)
    
    class Tudui(nn.Module):
        def __init__(self):
            super(Tudui, self).__init__()
            self.maxpool2d1 = MaxPool2d(3, ceil_mode=True)
            self.maxpool2d2 = MaxPool2d(3, ceil_mode=False)
    
        def forward(self, input):
            output1 = self.maxpool2d1(input)
            output2 = self.maxpool2d2(input)
            return output1, output2
    
    tudui = Tudui()
    out1, out2 = tudui(input)
    print(out1)
    print(out2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    执行结果

    ***.conda\envs\pytorch\lib\site-packages\torch\nn\functional.py:780: UserWarning: Note that order of the arguments: ceil_mode and return_indices will changeto match the args list in nn.MaxPool2d in a future release.
      warnings.warn("Note that order of the arguments: ceil_mode and return_indices will change"
    torch.Size([5, 5])
    torch.Size([1, 1, 5, 5])
    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.]]]])
    tensor([[[[2., 3.],
              [5., 1.]]]])
    tensor([[[[2.]]]])
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    code2

    import torch
    from torch import nn
    import torchvision
    from torch.nn import MaxPool2d
    from torch.utils.data import DataLoader
    from torch.utils.tensorboard import SummaryWriter
    
    test_set = torchvision.datasets.CIFAR10('./dataset', train=False, transform=torchvision.transforms.ToTensor(), download=True)
    
    dataloader = DataLoader(test_set, batch_size=64)
    
    class Tudui(nn.Module):
        def __init__(self):
            super(Tudui, self).__init__()
            self.maxpool2d = MaxPool2d(3, ceil_mode=True)
    
        def forward(self, input):
            output = self.maxpool2d(input)
            return output
    
    writer = SummaryWriter('logs')
    step = 1
    tudui = Tudui()
    for data in dataloader:
        imgs, targets = data
        writer.add_images('input: ', imgs, step)
        output = tudui(imgs)
        writer.add_images('output: ', output, step)
        step += 1
    
    writer.close()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    执行结果

    .conda\envs\pytorch\lib\site-packages\torch\nn\functional.py:780: UserWarning: Note that order of the arguments: ceil_mode and return_indices will changeto match the args list in nn.MaxPool2d in a future release.
      warnings.warn("Note that order of the arguments: ceil_mode and return_indices will change"
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    \learn_pytorch> tensorboard --logdir=logs
    TensorFlow installation not found - running with reduced feature set.
    Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
    TensorBoard 2.14.0 at http://localhost:6006/ (Press CTRL+C to quit)
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

  • 相关阅读:
    Mac里有多个版本的Python,产生的奇奇怪怪的问题
    7、MySQL Workbench 导出导入数据库
    相对路径的使用
    RAW图像处理软件Capture One 23 Enterprise mac中文版功能特点
    Linux的基本命令
    亚马逊频繁封号,跨境电商卖家如何应对?
    J-Tech Talk|以型搜型:3D模型表征助力3D神经搜索!
    吊打面试官系列之:常见测试开发面试题汇总,在面试的路上,总要先下手为强。
    Apache HTTPD 多后缀解析漏洞
    详解LockSupport的使用
  • 原文地址:https://blog.csdn.net/weixin_42831564/article/details/132834721