Pytorch学习12_最大池化的使用

输入图像

复制代码
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]
                    ])
input=torch.reshape(input,(-1,1,5,5))#二维张量转换为一个四维张量。(batch_size, channels, height, width)

print(input.shape)

ceil_mode=True

复制代码
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)
input=torch.reshape(input,(-1,1,5,5))#二维张量转换为一个四维张量。(batch_size, channels, height, width)

# print(input.shape)

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

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

xuexu=Xuexu()
output=xuexu(input)
print(output)

ceil_mode=False

复制代码
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)
input=torch.reshape(input,(-1,1,5,5))#二维张量转换为一个四维张量。(batch_size, channels, height, width)

# print(input.shape)

class Xuexu(nn.Module):
    def __init__(self):
        super().__init__()
        self.maxpool=MaxPool2d(kernel_size=3,ceil_mode=False)

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

xuexu=Xuexu()
output=xuexu(input)
print(output)

输出查看

复制代码
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import dataset, DataLoader
from torch.utils.tensorboard import SummaryWriter

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

dataloader=DataLoader(dataset,batch_size=64)

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

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

xuexu=Xuexu()
writer=SummaryWriter("./logs_maxpool")
step=0
for data in dataloader:
    imgs,targets=data
    writer.add_images("input",imgs,step)
    output=xuexu(imgs)
    writer.add_images("output",output,step)
    step=step+1

writer.close()

终端运行

tensotboard --logdir="logs_maxpool"

点击蓝色的链接

可以看到最大池化的效果,类似于马赛克。

最大池化是一种常用的池化操作,它的主要作用包括:

  1. 特征降维: 最大池化通过在局部区域内选择最大值来减小图像或特征图的空间尺寸。这有助于减少模型的计算复杂性和参数数量,从而加速训练和降低过拟合的风险。

  2. 平移不变性: 最大池化在一定程度上提供平移不变性,即对于输入的轻微平移,最大池化仍然能够提取相同的关键特征。这对于图像识别等任务是有益的,因为图像中的目标可能出现在不同的位置。

  3. 特征提取: 最大池化有助于保留图像或特征图中的重要特征,因为在每个池化窗口中,只选择最显著的特征值。这有助于模型更专注于图像中最重要的信息。

总的来说,最大池化是一种常见的操作,用于在深度学习中对输入数据进行下采样和特征提取,以改善模型的计算效率和泛化能力。

参考

【PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】】 https://www.bilibili.com/video/BV1hE411t7RN/?p=19\&share_source=copy_web\&vd_source=be33b1553b08cc7b94afdd6c8a50dc5a

相关推荐
立志成为大牛的小牛几秒前
数据结构——二十九、图的广度优先遍历(BFS)(王道408)
数据结构·数据库·学习·程序人生·考研·算法·宽度优先
taxunjishu几秒前
Ethernet/ip 转 Modbus RTU 驱动,罗克韦尔 PLC 与华为逆变器打造光伏电站智能监控典范
人工智能·物联网·自动化·区块链
坚持就完事了2 分钟前
正则表达式与Python的re模块
python·正则表达式
Alex艾力的IT数字空间2 分钟前
基于PyTorch和CuPy的GPU并行化遗传算法实现
数据结构·人工智能·pytorch·python·深度学习·算法·机器学习
keerduoba10 分钟前
EWCCTF2025 Tacticool Bin wp
python
屁股割了还要学13 分钟前
【Linux入门】常用工具:yum、vim
linux·运维·服务器·c语言·c++·学习·考研
却道天凉_好个秋17 分钟前
OpenCV(十三):通道的分离与合并
人工智能·opencv·计算机视觉
七宝大爷23 分钟前
NVIDIA Blackwell Ultra GB300深度解析:AI芯片性能的新巅峰
人工智能·gpu·gb300
鲸鱼在dn24 分钟前
大语言模型的后训练与“灾难性遗忘”问题——李宏毅2025大模型第六讲笔记
人工智能·笔记·语言模型
a20063801237 分钟前
ply(python版本的flex/bison or Lex/Yacc)
python