1D卷积类的调用参数解释
class torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
-in_channels(int) – 输出通道数量。在文本分类中,即为词向量的维度
-out_channels(int) – 通过卷积生成的通道数量。有多少个out_channels,就须要多少个1维卷积
-kernel_size(int or tuple) - 卷积核的尺寸,卷积核的大小为(k,),第二个维度是由in_channels来决定的,所以实际上卷积大小为kernel_size*in_channels
-stride(int or tuple, optional) - 卷积步长
-padding (int or tuple, optional)- 输出的每一条边补充0的层数
-dilation(int or tuple, optional) – 卷积核元素之间的间距(空洞卷积)
-groups(int, optional) – 从输出通道到输入通道的阻塞连接数
-bias(bool, optional) - 偏置量抉择,bias=True为增加举例代码
这里32为batch_size,64为句子最大长度,512为词向量。输出一维卷积的时候,须要将32x64x512变换为32x512x64,因为一维卷积是在最初维度上扫的,最初out的大小即为:32x128x(64-2+1)=32x128x63conv1 = nn.Conv1d(in_channels=512,out_channels=128,kernel_size=2)input = torch.randn(32,64,512)# 对矩阵进行转置:batch_size x text_len x embedding_size -> batch_size x embedding_size x text_leninput = input.permute(0,2,1)out = conv1(input)#print(out)#print(out.size())