学习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)

执行结果

***.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

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

执行结果

.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
\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)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值