Conv1d#

class Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, conv_mode='cross_correlation', compute_mode='default', padding_mode='zeros', **kwargs)[源代码]#

对输入张量进行一维卷积

例如, 给定输入的大小 \((N, C_{\text{in}}, H)\), 该层会通过下述过程生成大小为 \((N, C_{\text{out}}, H_{\text{out}})\) 的输出:

\[\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \sum_{k = 0}^{C_{\text{in}} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k)\]

其中 \(\star\) 是有效的1D互相关运算; \(N\) 是批大小; \(C\) 表示通道数; \(H\) 是以像素为单位输入平面的高度。

groups == in_channelsout_channels == K * in_channels ,其中 K 是正整数,该操作也被称为深度方向卷积(depthwise convolution)。

In other words, for an input of size \((N, C_{in}, H_{in})\), a depthwise convolution with a depthwise multiplier K, can be constructed by arguments \((in\_channels=C_{in}, out\_channels=C_{in} \times K, ..., groups=C_{in})\).

参数:
  • in_channels (int) – 输入数据中的通道数。

  • out_channels (int) – 输出数据中的通道数。

  • kernel_size (int) – 空间维度上的权重大小。

  • stride (int) – stride of the 1D convolution operation.

  • padding (int) – size of the paddings added to the input on both sides of its spatial dimensions. Default: 0

  • dilation (int) – dilation of the 1D convolution operation. Default: 1

  • groups (int) – number of groups to divide input and output channels into, so as to perform a “grouped convolution”. When groups is not 1, in_channels and out_channels must be divisible by groups, and the shape of weight should be (groups, out_channel // groups, in_channels // groups, kernel_size). Default: 1

  • bias (bool) – whether to add a bias onto the result of convolution. Default: True

  • conv_mode (str) – Supports cross_correlation. Default: cross_correlation

  • compute_mode (str) – When set to “default”, no special requirements will be placed on the precision of intermediate results. When set to “float32”, “float32” would be used for accumulator and intermediate result, but only effective when input and output are of float16 dtype.

  • padding_mode (str) – “zeros”, “reflect” 或者 “replicate”。默认值:”zeros”。更多信息参考 Pad

备注

  • weight 的shape通常是 (out_channels, in_channels, kernel_size) , 如果 groups 不是 1, shape 将变为 (groups, out_channels // groups, in_channels // groups, kernel_size)

  • bias 通常shape为 (1, out_channels, 1)

实际案例

>>> import numpy as np
>>> m = M.Conv1d(in_channels=3, out_channels=1, kernel_size=3)
>>> inp = mge.tensor(np.arange(0, 24).astype("float32").reshape(2, 3, 4))
>>> oup = m(inp)
>>> oup.numpy().shape
(2, 1, 2)