Pytorch-conv1d

CLASS


torch.nn.Conv1d(in_channels: int, out_channels: int, kernel_size: Union[T, Tuple[T]], stride: Union[T, Tuple[T]] = 1, padding: Union[T, Tuple[T]] = 0, dilation: Union[T, Tuple[T]] = 1, groups: int = 1, bias: bool = True, padding_mode: str = ‘zeros’)

参数


  • 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 or tuple, optional) – Zero-padding added to both sides of the input. Default: 0
  • padding_mode (string*,* optional) – 'zeros', 'reflect', 'replicate' or 'circular'. Default: 'zeros'
  • dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
  • groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
  • bias (bool, optional) – If True, adds a learnable bias to the output. Default: True

Shape


  • 输入: ( N , C i n , L i n ) (N,C_{in},L_{in}) N,Cin,Lin N N N 是 batch size, C i n C_{in} Cin 是输入的通道数量, L i n L_{in} Lin 输入的信号序列的长度。

  • 输出: ( N , C o u t , L o u t ) (N,C_{out},L_{out}) (N,Cout,Lout) 其中:
    L o u t = ⌊ L i n + 2 × p a d d i n g − d i l a t i o n × ( k e r n e l _ s i z e − 1 ) − 1 s t r i d e − 1 ⌋ L_{out}=\lfloor \frac{L_{in}+2\times padding-dilation\times (kernel\_size-1)-1}{stride}-1 \rfloor Lout=strideLin+2×paddingdilation×(kernel_size1)11
    N N N 是 batch size, C o u t C_{out} Cout 是输入的通道数量, L o u t L_{out} Lout 输出的信号序列的长度。

Variables


conv1d.weight(Tensor) 模型的需要学习的权重的形状大小为: ( o u t _ c h a n n e l s , i n _ c h a n n e l s g r o u p s , k e r n e l _ s i z e ) (out\_channels,\frac{in\_channels}{groups},kernel\_size) out_channels,groupsin_channels,kernel_size. 该权重的值的话是从均匀分布 U ( − k , k ) \mathcal{U}(-\sqrt{k},\sqrt{k}) U(k ,k )采样得到的,其中 k = g r o u p s C i n × k e r n e l _ s i z e k=\frac{groups}{C_{in}\times kernel\_size} k=Cin×kernel_sizegroups.

conv1d.bias(Tensor) 模型的需要学习的偏置的形状大小为 o u t _ c h a n n e l s out\_channels out_channels. 若 bias是True 的话,那么其值是从均匀分布 U ( − k , k ) \mathcal{U}(-\sqrt{k},\sqrt{k}) U(k ,k )采样得到的,其中 k = g r o u p s C i n × k e r n e l _ s i z e k=\frac{groups}{C_{in}\times kernel\_size} k=Cin×kernel_sizegroups.

自我理解


conv1d 可能是针对一维信号的卷积。

如下便可以理解为一个一维信号,通道数量C = 4, 信号序列长度L=6。

111111
222222
333333
444444

然后 batch size N 为多个这样的一维信号,相当于一组这样的信号去做一个minibatch的训练。

假设现有一个这样的信号:

tensor = torch.FloatTensor([[[1,1,1,1,1,1],[2,2,2,2,2,2],[3,3,3,3,3,3],[4,4,4,4,4,4]], [[1,1,1,1,1,1],[2,2,2,2,2,2],[3,3,3,3,3,3],[4,4,4,4,4,4]]])  # shape=torch.Size([2, 4, 6])

这个输入信号的通道数量C = 4, 信号序列长度L=6,batch size N=2。其实就是如上表格的信号。

接着构建一个如下的一个Conv1d的卷积层,输入信号的通道数量 C i n = 4 C_{in}=4 Cin=4,输出信号的通道数量 C o u t = 40 C_{out}=40 Cout=40, 卷积核的kernel_size=1.

m = nn.Conv1d(4, 40, 1, stride=1)  # bias.shape=40  weight.shape=torch.Size([40, 4, 1])

然后,调用创建的卷积层。

output = m(tensor)  # shape=torch.Size([2, 40, 6])

在这里插入图片描述
如果m = nn.Conv1d(4, 40, 1, stride=1) 的卷积核的kernel_size=2时,根据上图卷积的区域就会变成 4 × 2 4\times 2 4×2,然后在横向的维度移动。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值