最大池化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()
相关推荐
文心快码BaiduComate10 分钟前
百度云与光本位签署战略合作:用AI Agent 重构芯片研发流程
前端·人工智能·架构
风象南1 小时前
Claude Code这个隐藏技能,让我告别PPT焦虑
人工智能·后端
曲幽1 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
Mintopia2 小时前
OpenClaw 对软件行业产生的影响
人工智能
陈广亮2 小时前
构建具有长期记忆的 AI Agent:从设计模式到生产实践
人工智能
会写代码的柯基犬2 小时前
DeepSeek vs Kimi vs Qwen —— AI 生成俄罗斯方块代码效果横评
人工智能·llm
Mintopia3 小时前
OpenClaw 是什么?为什么节后热度如此之高?
人工智能
爱可生开源社区3 小时前
DBA 的未来?八位行业先锋的年度圆桌讨论
人工智能·dba
叁两6 小时前
用opencode打造全自动公众号写作流水线,AI 代笔太香了!
前端·人工智能·agent
敏编程6 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python