p19神经网络-最大池化的作用

以pytorch官网文档为学习教案

在卷积层stride默认为1,在池化层stride默认为kernal_size大小
对于Docs > torch.nn > MaxPool2d中的
ceil_mode – when True, will use ceil instead of floor to compute the output shape参数
当为True时,保留,为False舍去
在这里插入图片描述

根据其需要的input与output格式,进行reshape

在这里插入图片描述

import torch

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]])
input = torch.reshape(input, [-1, 1, 5, 5])
print(input.shape)
D:\anaconda\python.exe C:/Users/ASUS/Desktop/tudui/nn_maxpool.py
torch.Size([1, 1, 5, 5])

Process finished with exit code 0

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)  # 因为MaxPool2d要求long浮点输入
input = torch.reshape(input, [-1, 1, 5, 5])
print(input.shape)

class Lixinyu(nn.Module):
    def __init__(self):
        super(Lixinyu,self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)

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

lixinyu = Lixinyu()
output = lixinyu(input)
print(output)
D:\anaconda\python.exe C:/Users/ASUS/Desktop/tudui/nn_maxpool.py
D:\anaconda\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([1, 1, 5, 5])
tensor([[[[2., 3.],
          [5., 1.]]]])

Process finished with exit code 0

最终

import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)

dataloader = DataLoader(dataset, batch_size=64)

class Lixinyu(nn.Module):
    def __init__(self):
        super(Lixinyu, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)

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

lixinyu = Lixinyu()

writer = SummaryWriter("p19")
step = 0

for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    output = lixinyu(imgs)
    writer.add_images("output", output, step)
    step += 1

writer.close()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

橙黄橘绿时_Eden

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值