pytorch之torch.nn.Conv2d()函数详解
文章目录
一、官方文档介绍
nn.Conv2d
:对由多个输入平面(多通道)组成的输入信号进行二维卷积
二、torch.nn.Conv2d()函数详解
参数详解
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
参数 | 参数类型 | ||
---|---|---|---|
in_channels |
int | Number of channels in the input image | 输入图像通道数 |
out_channels |
int | Number of channels produced by the convolution | 卷积产生的通道数 |
kernel_size |
(int or tuple) | Size of the convolving kernel | 卷积核尺寸,可以设为1个int型数或者一个(int, int)型的元组。例如(2,3)是高2宽3卷积核 |
stride |
(int or tuple, optional) | Stride of the convolution. Default: 1 | 卷积步长,默认为1。可以设为1个int型数或者一个(int, int)型的元组。 |
padding |
(int or tuple, optional) | Zero-padding added to both sides of the input. Default: 0 | 填充操作,控制padding_mode 的数目。 |
padding_mode |
(string, optional) | ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’ | padding 模式,默认为Zero-padding 。 |
dilation |
(int or tuple, optional) | Spacing between kernel elements. Default: 1 | 扩张操作:控制kernel点(卷积核点)的间距,默认值:1。 |
groups |
(int, optional) | Number of blocked connections from input channels to output channels. Default: 1 | group参数的作用是控制分组卷积,默认不分组,为1组。 |
bias |
(bool, optional) | If True, adds a learnable bias to the output. Default: True | 为真,则在输出中添加一个可学习的偏差。默认:True。 |
参数dilation——扩张卷积(也叫空洞卷积)
dilation操作动图演示如下:
Dilated Convolution with a 3 x 3 kernel and dilation rate 2
扩张卷积核为3×3,扩张率为2
参数groups——分组卷积
Group Convolution顾名思义,则是对输入feature map进行分组,然后每组分别卷积。假设输入feature map的尺寸仍为C ∗ H ∗ W ,输出feature map的数量为N个,如果设定要分成G个groups,则每组的输入feature map数量为 C G \frac{C}{G}GC ,每组的输出feature map数量为N G \frac{N}{G}GN,每个卷积核的尺寸为C G ∗ K ∗ K \frac{C}{G}∗K∗KGC∗K∗K,卷积核的总数仍为N个,每组的卷积核数量为N G \frac{N}{G}GN ,卷积核只与其同组的输入map进行卷积,卷积核的总参数量为N ∗ C G ∗ K ∗ K N∗\frac{C}{G}∗K∗KN∗GC∗K∗K,可见,总参数量减少为原来的1 G \frac{1}{G}G1,其连接方式如下图右所示,group1输出map数为2,有2个卷积核,每个卷积核的channel数为4,与group1的输入map的channel数相同,卷积核只与同组的输入map卷积,而不与其他组的输入map卷积。
三、代码实例
import torch x = torch.randn(3,1,5,4) print(x) conv = torch.nn.Conv2d(1,4,(2,3)) res = conv(x) print(res.shape) # torch.Size([3, 4, 4, 2])
输入:x[ batch_size, channels, height_1, width_1 ] batch_size,一个batch中样本的个数 3 channels,通道数,也就是当前层的深度 1 height_1, 图片的高 5 width_1, 图片的宽 4 卷积操作:Conv2d[ channels, output, height_2, width_2 ] channels,通道数,和上面保持一致,也就是当前层的深度 1 output ,输出的深度 4【需要4个filter】 height_2,卷积核的高 2 width_2,卷积核的宽 3 输出:res[ batch_size,output, height_3, width_3 ] batch_size,,一个batch中样例的个数,同上 3 output, 输出的深度 4 height_3, 卷积结果的高度 4 width_3,卷积结果的宽度 2 一个样本卷积示例:
亲测有效!!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~