15.土堆说卷积操作(stride、padding)

土堆说卷积操作(可选看)

卷积神经网络中Padding和Stride的概念,Padding用于解决图像边缘信息丢失问题,保持输出矩阵尺寸;Stride则影响卷积的步进,改变输出大小。通过调整这两者,可以控制卷积层的输出特性。

这节来讲解卷积层 :Convolution Layers

先进入pytorch官方网站地址:torch.nn --- PyTorch 1.8.1 documentation

主要讲解 nn.Conve2d ,pytorch官方网站地址:Conv2d --- PyTorch 1.8.1 documentation

torch.nn 和 torch.nn.functional 的区别:前者是后者的封装,更利于使用

点击 torch.nn.functional - Convolution functions - conv2d 查看参数

stride(步进)

可以是单个数,或元组(sH,sW) --- 控制横向步进和纵向步进

卷积操作

卷积操作介绍

当 stride = 2 时,横向和纵向都是2,输出是一个2×2的矩阵

卷积操作实战

要求输入的维度 & reshape函数

  • input:尺寸要求是batch,几个通道,高,宽(4个参数)
  • weight:尺寸要求是输出,in_channels(groups一般为1),高,宽(4个参数)

使用 torch.reshape 函数,将输入改变为要求输入的维度

实现上图代码

复制代码
import torch
import torch.nn.functional as F
 
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]])   #将二维矩阵转为tensor数据类型
# 卷积核kernel
kernel = torch.tensor([[1,2,1],
                       [0,1,0],
                       [2,1,0]])
 
# 尺寸只有高和宽,不符合要求
print(input.shape)  #5×5
print(kernel.shape)  #3×3
 
# 尺寸变换为四个数字
input = torch.reshape(input,(1,1,5,5))  #通道数为1,batch大小为1
kernel = torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(kernel.shape)
 
output = F.conv2d(input,kernel,stride=1)  # .conv2d(input:Tensor, weight:Tensor, stride)
print(output)

输出结果为:

当将步进 stride 改为 2 时:

复制代码
output2 = F.conv2d(input,kernel,stride=2)
print(output2)

padding(填充)

在输入图像左右两边进行填充,决定填充有多大。可以为一个数或一个元组(分别指定高和宽,即纵向和横向每次填充的大小)。默认情况下不进行填充

padding=1:将输入图像左右上下两边都拓展一个像素,空的地方默认为0

代码实现:

复制代码
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)

d(input,kernel,stride=1,padding=1)

print(output3)

复制代码
[外链图片转存中...(img-xxjUZlUQ-1724861517755)]
相关推荐
万少4 小时前
小龙虾(openclaw),轻松玩转自动发帖
前端·人工智能·后端
飞哥数智坊6 小时前
openclaw 重大更新,真的懂我啊
人工智能
KaneLogger6 小时前
AI 时代编程范式迁移的思考
人工智能·程序员·代码规范
飞哥数智坊6 小时前
养虾记第2期:从“人工智障”到“赛博分身”,你的龙虾还缺这两个灵魂
人工智能
飞哥数智坊6 小时前
龙虾虽香,小心扎手!官方点名后,我们该怎么“养虾”?
人工智能
yiyu07167 小时前
3分钟搞懂深度学习AI:实操篇:卷积层
人工智能·深度学习
字节架构前端8 小时前
Skill再回首—深度解读Anthropic官方最新Skill白皮书
人工智能·agent·ai编程
冬奇Lab9 小时前
OpenClaw 深度解析(八):Skill 系统——让 LLM 按需学习工作流
人工智能·开源·源码阅读
冬奇Lab9 小时前
一天一个开源项目(第45篇):OpenAI Agents SDK Python - 轻量级多 Agent 工作流框架,支持 100+ LLM 与实时语音
人工智能·开源·openai