关于机器学习:1D卷积详解

2次阅读

共计 883 个字符,预计需要花费 3 分钟才能阅读完成。

  • 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)=32x128x63

    conv1 = 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_len
    input = input.permute(0,2,1)
    out = conv1(input)
    #print(out)
    #print(out.size())
正文完
 0