torch.nn.Conv1d及一维卷积详解

近日在搞wavenet,期间遇到了一维卷积,在这里对一维卷积以及其pytorch中的API进行总结,方便下次使用

之前对二维卷积是比较熟悉的,在初次接触一维卷积的时候,我以为是一个一维的卷积核在一条线上做卷积,但是这种理解是错的,一维卷积不代表卷积核只有一维,也不代表被卷积的feature也是一维。一维的意思是说卷积的方向是一维的。

下边首先看一个简单的一维卷积的例子(batchsize是1,也只有一个kernel):

输入:

一个长度为35的序列,序列中的每个元素有256维特征,故输入可以看作(35,256)
卷积核: size = (k,) , (k = 2)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aUj1VA0m-1573028650763)(https://ranchofromxgd.github.io/_posts/assets/2019-11-06-16-16-37.png)]

这幅图只说明了只有一个数据的情况,如果将数据打包成batch,可以用代码表示如下:

    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())

输出:

torch.Size([32, 100, 34])

在分析这个结果之前先来看一下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函数,这样就可以实现一维卷积。

  • 114
    点赞
  • 282
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
torch.nn.Conv1d is a class in the PyTorch library that represents a 1-dimensional convolutional layer. The Conv1d layer applies a 1D convolution operation on the input tensor. It is commonly used in deep learning models for processing one-dimensional sequential data such as time series, audio signals, or text data. The Conv1d layer takes as input a 3D tensor with dimensions (batch_size, input_channels, input_length) and applies a convolution operation using a set of learnable filters. The filters slide over the input tensor along one dimension to produce a set of output channels. The output tensor has dimensions (batch_size, output_channels, output_length), where output_length depends on the padding and stride parameters. The Conv1d layer has several parameters that can be set, including the number of input and output channels, the size of the convolutional kernel, the stride, padding, and dilation rates. These parameters allow the Conv1d layer to be customized for different applications. Example usage: ``` import torch # Define a Conv1d layer with 16 input channels, 32 output channels, and a kernel size of 3 conv1d_layer = torch.nn.Conv1d(in_channels=16, out_channels=32, kernel_size=3) # Define an input tensor with dimensions (batch_size=4, input_channels=16, input_length=100) input_tensor = torch.randn(4, 16, 100) # Apply the Conv1d layer to the input tensor output_tensor = conv1d_layer(input_tensor) # The output tensor has dimensions (batch_size=4, output_channels=32, output_length=98) print(output_tensor.shape) ```
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值