Pytorch学习(二)Conv2d卷积

函数功能为在由多个输入平面组成的输入信号上应用二维卷积。
https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d

函数原型

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)

参数说明

in_channels :int类型,输入图像通道数
out_channels :int类型 ,卷积产生的通道数
kernel_size: int or tuple类型,卷积核尺寸,可以设为1个int型数或者一个(int, int)型的元组。例如(2,3)是高2宽3卷积核
stride:int or tuple类型,卷积步长,默认为1。可以设为1个int型数或者一个(int, int)型的元组。
padding:int or tuple类型,填充操作,控制padding_mode的数目。
padding_mode :string,类型‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’ padding模式,默认为Zero-padding 。
dilation:int or tuple类型 ,扩张操作:控制kernel点(卷积核点)的间距,默认值:1。
groups,int类型,group参数的作用是控制分组卷积,默认不分组,为1组。
bias :bool类型 :为真,则在输出中添加一个可学习的偏差。默认:True。

代码测试

import torch

x = torch.randn(2, 1, 5, 5)
print(x)

conv = torch.nn.Conv2d(1,3,(2,2))
res = conv(x)

print(res.shape)
print(res)

输入:x[ batch_size, channels, height_1, width_1 ]
batch_size,一个batch中样本的个数 2
channels,通道数,也就是当前层的深度 1
height_1, 图片的高 5
width_1, 图片的宽 5

卷积操作:Conv2d[ channels, output, height_2, width_2 ]
channels,通道数,和上面保持一致,也就是当前层的深度 1
output ,输出的深度 3【需要3个filter】
height_2,卷积核的高 2
width_2,卷积核的宽 2

输出:res[ batch_size,output, height_3, width_3 ]
batch_size,,一个batch中样例的个数,同上 2
output, 输出的深度 3
height_3, 卷积结果的高度 4
width_3,卷积结果的宽度 4

打印输出:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44901043/article/details/123744373