深度学习(八)——神经网络:卷积层,如何应用?

摘要:# 一、卷积层Convolution Layers函数简介 > 官网网址:[torch.nn.functional — PyTorch 2.0 documentation](https:pytorch.orgdo
一、卷积层Convolution Layers函数简介 官网网址:torch.nn.functional — PyTorch 2.0 documentation 由于是图像处理,所以主要介绍Conv2d。 class 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): 输入图像的通道数,彩色图像一般为3(RGB三通道) out_channel(int): 通过卷积后,产生的输出的通道数 kernel_size(int or tuple): 一个数或者元组,定义卷积大小。如kernel_size=3,即定义了一个大小为3×3的卷积核;kernel_size=(1,2),即定义了一个大小为1×2的卷积核。 stride(int or tuple,可选): 默认为1,卷积核横向、纵向的步幅大小 padding(int or tuple,可选): 默认为0,对图像边缘进行填充的范围 padding_mode(string,可选): 默认为zeros,对图像周围进行padding时,采取什么样的填充方式。可选参数有:'zeros','reflect','replicate'or'circular'。 dilation(int or tuple,可选): 默认为1,定义在卷积过程中,它的核之间的距离。这个我们称之为空洞卷积,但不常用。 groups(int or tuple,可选): 默认为1。分组卷积,一般都设置为1,很少有改动 bias(bool,可选): 默认为True。偏置,常年设置为True。代表卷积后的结果是否加减一个常数。 关于卷积操作,官方文档的解释如下: In the simplest case, the output value of the layer with input size\((N,C_{in}​,H,W)\)and output\((N,C_{out}​,H_{out}​,W_{out}​)\)can be precisely described as: \[out(N_i​,Cou_{tj}​​)=bias(C_{out_j}​​)+∑_{k=0}^{C_{in}​−1}​weight(C_{out_j}​​,k)⋆input(N_i​,k) \] where⋆is the valid 2Dcross-correlationoperator,\(N\)is a batch size,\(C\)denotes a number of channels,\(H\)is a height of input planes in pixels, and\(W\)is width in pixels. (1)参数kernel_size的说明 kernel_size主要是用来设置卷积核大小尺寸的,给定模型一个kernel_size,模型就可以据此生成相应尺寸的卷积核。 卷积核中的参数从图像数据分布中采样计算得到的。 卷积核中的参数会通过训练不断进行调整。
阅读全文