卷积中out_channels实际上就是卷积核的个数,或者叫_num_filters,也就是滤波器的个数
可以用代码表示如下:
from torch.autograd import Variable
conv1 = nn.Conv1d(in_channels=256,out_channels = 100, kernel_size = 2)
input = torch.randn(32, 35, 256)
# batch_size x text_len x embedding_size -> batch_size x embedding_size x text_len
input = input.permute(0, 2, 1)
input = Variable(input)
out = conv1(input)
print(out.size())
输入的三个维度分别是:batch_size, in_channels也就是输入通道数=embedding维度,序列长度
输出:
torch.Size([32, 100, 34])
1
在分析这个结果之前先来看一下nn.Conv1d的官方文档
// 可以理解为特征的维度
in_channels – Number of channels in the input image
//输出的通道数,可以理解为卷积核的数量
out_channels – Number of channels produced by the convolution
// 卷积核的大小,只需要指定卷积方向的大小(因为是一维的)
kernel_size – Size of the convolving kernel
stride – Stride of the convolution
padding – Zero-padding added to both sides of the input
dilation – Spacing between kernel elements
groups – Number of blocked connections from input channels to output channels
bias – If True, adds a learnable bias to the output
再来看输出:torch.Size([32, 100, 34])
输入数据第一维表示batchsize,后边两维和前边的例子一样,不同的是输出,长度变为了34(卷积核大小为2),由于有100个卷积核,故生成了100个feature map
可能还会有一个疑惑,就是感觉100和34位置反过来了,这是因为nn.Conv1d对输入数据的最后一维进行一维卷积,为了将卷积方向设置正确,我们需要将输入序列长度这一维放到最后,即使用permute函数,这样就可以实现一维卷积。
一维卷积的作用
用1x1卷积核,实现降维和升维的操作其实就是channel间信息的线性组合变化,3x3,64channels的卷积核后面添加一个1x1,28channels的卷积核,就变成了3x3,28channels的卷积核,原来的64个channels就可以理解为跨通道线性组合变成了28channels,这就是通道间的信息交互。
注意:只是在channel维度上做线性组合,W和H上是共享权值的sliding window

一维卷积详解

171

被折叠的 条评论
为什么被折叠?



