一、声明
- 本文根据个人理解写的,如有纰漏望指正,谢谢!
- 本帖持续更新中
二、理解
2.1 调用格式
torch.nn.Conv1d(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:输入通道数
- out_channels = b 2 b_2 b2:这意味着我们将使用 b 2 b_2 b2个不同的卷积核来处理输入数据。每个卷积核将能够学习输入数据的不同特征,从而输出 b 2 b_2 b2个特征映射(或称为通道)。这增加了模型从输入数据中提取特征的能力。
- kernel_size:卷积核“长度”,其另一维度值与输入的通道数相同,所以卷积核尺寸为kernel_size*in_channels
- stride:滑动步长,默认为1
- padding:补充0的个数,默认为0。例:当padding=1,则在输入的每一通道的数列的左右两端各补一个0
- dilation:核的元素之间的间隔。 默认为1。(1应该是指元素间距,1即紧邻)
- groups:分组数。
例如,(1)当groups=1,则所有输入都卷积到输出;(2)groups=2时,这个操作就相当于有两个conv层并排,每个conv层作用于一半的输入通道,产生一半的输出通道,然后两者串联起来;(3)groups= in_channels时,每个输入通道都用它自己的一组滤波器进行卷积,这些滤波器的大小为⌊in_channels/out_channels⌋
2.2 运算示意图
输入数据、卷积核、输出数据三者各维度大小的关系
注:关于 c 3 c_3 c3的大小其实是个很复杂的公式,默认情况下可以用下图中的公式计算。ppt
图1:数据之间各维度大小关系 |
---|
![]() |
示例
假设输入数据 A A A是一个3x2x4的张量,定义卷积核时定义为nn.Conv1d(2, 4, 2, stride=1),也就是说,卷积核4个是2x2大小的张量,最终输出数据 O O O则为3x4x3。
图2:数据之间各维度大小关系 |
---|
![]() |
代码
conv1d参与操作的主要有:输入数据、weight、bias。参考
import torch.nn as nn
import torch
# input data
input = torch.randn(3, 2, 4) # (batch_size,channels,length)
print("input data:\n",input)
# define conv1d
m = nn.Conv1d(2, 4, 2, stride=1) # (input_channels, output_channels, kernel_size, stride)
# weight and bias
print("weight:\n",m.weight)
print("bias:\n", m.bias)
# perform conv1d
output = m(input)
print("output:\n",output)
注:如何更改weight和bias呢?
nn.init.constant_(conv1.weight, 1) # 将核的元素值初始化为全1
nn.init.constant_(conv1.bias, 0) # 偏置值为0
三、注意点
conv1d有两个,一个是torch.nn模块中,另一个是torch.nn.functional模块中。
第二种: torch.nn.functional.conv1d
调用格式
torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
- 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
- stride (int or tuple, optional) – Stride of the convolution. Default: 1
- padding (int, tuple or str, optional) – Padding added to both sides of the input. Default: 0
- padding_mode (string, optional) – ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’
- dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
- groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
- bias (bool, optional) – If True, adds a learnable bias to the output. Default: True