最大池化pytorch

**前置知识:

1、

复制代码
self.maxpool_2=MaxPool2d(kernel_size=3,ceil_mode=True)
复制代码
output=self.maxpool_2(input)

输入:张量的形状是(N,C,H,W)或(C,H,W)

  • Input: (N,C,Hin,Win)or (C,Hin,Win)

  • Output: (N,C,Hout,Wout)or (C,Hout,Wout)

参数:

  • 池化核(池化窗口)大小:kernel_size (Union[ int, Tuple[ int, int] ]) -- the size of the window to take a max over

  • 步长: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

  • 取整方式:ceil_mode (bool) -- when True, will use ceil instead of floor to compute the output shape(True:向上取整,保留不足的部分;False:向下取整,去除不足一份的部分)

  • 空洞卷积dilation (Union[ int, Tuple[ int, int] ]) -- a parameter that controls the stride of elements in the window

2、池化的作用:

从特征图中提取最有代表性的特征;防止过拟合,实现降维;保持平移不变性。

(即保留重要特征,同时减少数据量,使模型训练得更快 eg: 1080P高清------>720P高清)

**代码:

1、对单一二维矩阵进行最大池化:

input 单一二维矩阵reshape(变成3D或4D)------>nn 创建神经元------>output 计算并输出

python 复制代码
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)) #-1是占位符,后续自动计算batch_size的大小
print(input.shape)

#神经元
class Xigua(nn.Module):
    def __init__(self):
        super().__init__()
        self.maxpool_2=MaxPool2d(kernel_size=3,ceil_mode=True) #保留不足的部分,也把它算进去

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

xigua1=Xigua()
output=xigua1(input)
print(output)

2、对RGB图像进行池化:

input 导入并加载RGB图片数据集------>nn 创建神经元------>output 计算并记录

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

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

dataloader=DataLoader(test_set,batch_size=64)

#神经元
class Xigua(nn.Module):
    def __init__(self):
        super().__init__()
        self.maxpool_2=MaxPool2d(kernel_size=3,ceil_mode=True) #保留不足的部分,也把它算进去

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

xigua1=Xigua()
writer=SummaryWriter("logs2")
step=1
for imgs,targets in dataloader:
    print(imgs.shape)
    writer.add_images("input",imgs,step)
    imgs=xigua1(imgs)
    print(imgs.shape)
    writer.add_images("output",imgs,step)
    step=step+1
    if step>=3:
        break
writer.close()
相关推荐
@心都21 分钟前
机器学习数学基础:44.多元线性回归
人工智能·机器学习·线性回归
说私域22 分钟前
基于开源AI大模型的精准零售模式创新——融合AI智能名片与S2B2C商城小程序源码的“人工智能 + 线下零售”路径探索
人工智能·搜索引擎·小程序·开源·零售
蜡笔小新星25 分钟前
Flask项目框架
开发语言·前端·经验分享·后端·python·学习·flask
熊文豪25 分钟前
Windows本地部署OpenManus并接入Mistral模型的实践记录
人工智能·llm·mistral·manus·openmanus·openmanus开源替代方案·本地llm部署实践
cliff,26 分钟前
【python爬虫】酷狗音乐爬取
笔记·爬虫·python·学习
IT猿手26 分钟前
2025最新群智能优化算法:海市蜃楼搜索优化(Mirage Search Optimization, MSO)算法求解23个经典函数测试集,MATLAB
开发语言·人工智能·算法·机器学习·matlab·机器人
IT猿手2 小时前
2025最新群智能优化算法:山羊优化算法(Goat Optimization Algorithm, GOA)求解23个经典函数测试集,MATLAB
人工智能·python·算法·数学建模·matlab·智能优化算法
萧鼎3 小时前
深入解析 Umi-OCR:高效的免费开源 OCR 文字识别工具
python·ocr·umi-ocr
Jet45053 小时前
玩转ChatGPT:GPT 深入研究功能
人工智能·gpt·chatgpt·deep research·深入研究
毕加锁3 小时前
chatgpt完成python提取PDF简历指定内容的案例
人工智能·chatgpt