官方文档:https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html?highlight=nn%20conv1d#torch.nn.Conv1d

作用:对由多个输入平面组成的输入信号应用一维卷积。

在最简单的情况下,输入大小为 (N,Cin,L)(N, C_{\text{in}}, L)(N,Cin,L) 和输出(N,Cout,Lout)(N, C_{\text{out}}, L_{\text{out}})(N,Cout,Lout)可以精确地描述为:
out(Ni,Coutj)=bias(Coutj)+∑k=0Cin−1weight(Coutj,k)⋆input(Ni,k)\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \sum_{k = 0}^{C_{in} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k) out(Ni,Coutj)=bias(Coutj)+k=0Cin1weight(Coutj,k)input(Ni,k)
其中⋆\star是互相关算子(计算卷积的方式),NNN是batch_size,CCC表示通道数, LLL表示序列的长度。

参数:

  • 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,控制应用于输入的填充量。它可以是一个字符串 {‘valid’, ‘same’} 或一个整数元组,给出在两边应用的隐式填充量。

  • padding_mode (string, optional) – ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’

  • dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1,控制内核点之间的间距;也称为 à trous 算法。很难描述,但是这个链接很好地可视化了膨胀的作用。

  • groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1,控制输入​​和输出之间的连接。 in_channels 和 out_channels 都必须能被组整除。通常我们用不到。

  • bias (bool, optional) – If True, adds a learnable bias to the output. Default: True

输入、输出形状:

input: (N,Cin,Lin)(N, C_{in}, L_{in})(N,Cin,Lin)或者(Cin,Lin)(C_{in}, L_{in})(Cin,Lin)
output: (N,Cout,Lout)(N, C_{out}, L_{out})(N,Cout,Lout)或者(Cout,Lout)(C_{out}, L_{out})(Cout,Lout)
Lout=⌊Lin+2×padding−dilation×(kernel_size−1)−1stride+1⌋L_{out} = \left\lfloor\frac{L_{in} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel\_size} - 1) - 1}{\text{stride}} + 1\right\rfloor Lout=strideLin+2×paddingdilation×(kernel_size1)1+1

变量:

  • ~Conv1d.weight (Tensor) – the learnable weights of the module of shape (out_channels,in_channelsgroups,kernel_size)(\text{out\_channels}, \frac{\text{in\_channels}}{\text{groups}}, \text{kernel\_size})(out_channels,groupsin_channels,kernel_size) The values of these weights are sampled from U(−k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k})U(k

    ,k

    )
    where k=groupsCin∗kernel_sizek = \frac{groups}{C_\text{in} * \text{kernel\_size}}k=Cinkernel_sizegroups.
  • ~Conv1d.bias (Tensor) – the learnable bias of the module of shape (out_channels). If bias is True, then the values of these weights are sampled from U(−k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k})U(k ,k

    )
    where k=groupsCin∗kernel_sizek = \frac{groups}{C_\text{in} * \text{kernel\_size}}k=Cinkernel_sizegroups.

我使用比较多的实在textcnn模型中,使用cnn模型进行特征提取,以及在IDCNN模型中进行NER任务。以textcnn为例进行介绍,batch_size=N, 批次文本的长度为seq_len(批次数据如果长度不一致时,会使用pad的方式对数据进行对齐),设定的token(textcnn的文本处理单元,可以是字也可以是词)的Embedding维度为embedding_dim,embedding_dim也是conv1d的通道数。根据这个定义输入的数据可以如下形式:

以batch_size中的一条数据处理为例,进行卷积计算时需要有一个滑动的kernel,以及每次滑动的步幅stride,其他参数先保持默认。kernel的选择与N-gram相似,我们可以选择[2,3,4],stride通常也是设置为1的,也就是逐单元滑动,以kernel=2为例,输出通道output_channels=2,计算如下:

经过Conv1d之后,输出的就是2×32\times32×3的结果,其中2就是output_channels决定的,与其相等,3是根据seq_len以及stride共同决定的,那么预期的结果size就是N×2×3N\times2\times3N×2×3。假如in_channels=100,out_channels=2,kernel_size=2,batch_size=4,seq_len=10,stride=1那么输入该如何处理?输出又是什么样子呢?程序实现如下:

in_channels = 100
out_channels = 2
kernel_size = 2
batch_size = 4
seq_len = 10
conv1 = torch.nn.Conv1d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size)# 文本处理形式
input_data = torch.randn(batch_size, seq_len, in_channels)
# conv1d输入形式, (batch_size, channels, seq_len)
input_data = input_data.permute(0, 2, 1)   # torch.Size([4, 100, 10])
print(input_data.size())
conv_out = conv1(input_data)
print(conv_out.size())  # torch.Size([4, 2, 9])

输出结果的形状中4,2都比较好理解,9则是根据如下公式计算出的:
Lout=⌊Lin+2×padding−dilation×(kernel_size−1)−1stride+1⌋L_{out} = \left\lfloor\frac{L_{in} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel\_size} - 1) - 1}{\text{stride}} + 1\right\rfloor Lout=strideLin+2×paddingdilation×(kernel_size1)1+1
对应的参数带入即可计算出。在本例中padding=0,dilation=1,kernel_size=1,stride=1.

【Pytorch】torch.nn.Conv1d()理解与使用相关推荐

  1. Pytorch之nn.Conv1d学习个人见解

    Pytorch之nn.Conv1d学习个人见解 一.官方文档(务必先耐心阅读) 官方文档:点击打开<CONV1D> 二.Conv1d个人见解 Conv1d类构成 class torch.n ...

  2. 【pytorch】nn.conv1d的使用

    官方文档在这里. conv1d具体不做介绍了,本篇只做pytorch的API使用介绍. torch.nn.Conv1d(in_channels, out_channels, kernel_size, ...

  3. NLP中的卷积操作详解(torch.nn.Conv1d)

    NLP领域中,由于自然文本是一维的,通常使用一维卷积即可达到要求. 在实际应用中,经embedding层处理后的数据格式一般为(batch_size, word_embeddings_dim, max ...

  4. PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx

    PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现在 torch.nn.xxx 和 torch.nn.funct ...

  5. torch.nn.Embedding理解

    Pytorch官网的解释是:一个保存了固定字典和大小的简单查找表.这个模块常用来保存词嵌入和用下标检索它们.模块的输入是一个下标的列表,输出是对应的词嵌入. torch.nn.Embedding(nu ...

  6. [Pytorch]torch.nn.functional.conv2d与深度可分离卷积和标准卷积

    torch.nn.functional.conv2d与深度可分离卷积和标准卷积 前言 F.conv2d与nn.Conv2d F.conv2d 标准卷积考虑Batch的影响 深度可分离卷积 深度可分离卷 ...

  7. pytorch TORCH.NN 到底是什么?

    PyTorch 提供了设计精美的模块和类torch.nn. torch.optim. Dataset和DataLoader 来帮助创建和训练神经网络.为了充分利用它们的力量并针对需求灵活的定制它们,需 ...

  8. pytorch torch.nn.Sequential(* args)(嘎哈用的?构建神经网络用的?)

    class torch.nn.Sequential(* args) 一个时序容器.Modules 会以他们传入的顺序被添加到容器中.当然,也可以传入一个OrderedDict. 为了更容易的理解如何使 ...

  9. pytorch torch.nn.MSELoss

    应用 # 1.计算绝对差总和:|0-1|^2+|1-1|^2+|2-1|^2+|3-1|^2=6 # 2.求平均: 6/4 =1.5 import torch import torch.nn as n ...

最新文章

  1. 突然就懵了!面试官问我:线程池中多余的线程是如何回收的?
  2. R语言names函数获取或者设置数据对象名称实战
  3. FreeModbus离散量输入
  4. Robotframework中的日志级别
  5. VS2005(C#)里读取及改变App.config里appSettings节的值
  6. FFmpeg之获取yuv分量(二十二)
  7. 代码实现Autolayout
  8. VS 2010 for SharePoint
  9. 使用 EPUB 制作数字图书
  10. Kettle 数据迁移
  11. lync前段服务器证书安装,Lync Server 2013企业版部署测试六:前端服务器安装Lync Server系统...
  12. Oracle数据库锁表查询与解锁处理详解
  13. Linux的FTP安装、使用和配置(FTP客户端管理工具)
  14. 解决 python plt画柱状图(棒状图)时横坐标刻度线不在中间而在右边
  15. Axure原型设计相关资源网站(不断更新中……)
  16. Keil5.15使用GCC编译器编译STM32工程
  17. 可以在finally代码块中处理返回值么?
  18. LeetCode Algorithm 225. 用队列实现栈
  19. 6 海思Hi3518E的ISP及其3A
  20. 高级程序员、研发Leader、技术总监、首席架构师、CTO的职责

热门文章

  1. 网页制作流程--(项目案例)学成在线
  2. webexam php,Laravel View Composer - 当 include 一个模板时,自动获取其所需的变量
  3. boost install
  4. SecureCRT和SecureFx的使用
  5. 数据中台(01)- 全面了解数据中台
  6. kobe生涯数据 数据预处理
  7. 蚂蚁森林师生执念种上万棵树
  8. HTML5视频直播默认静音,HTML5教程 如何实现播放视频中暂停、关闭声音等操作
  9. Java调用Mysql
  10. 【正点原子STM32连载】第五十三章 DSP测试实验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1