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)[source]

Applies a 1D convolution over an input tensor.

For instance, given an input of the size \((N, C_{\text{in}}, H)\), this layer generates an output of the size \((N, C_{\text{out}}, H_{\text{out}})\) through the process described as below:

\[\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)\]

where \(\star\) is the valid 1D cross-correlation operator, \(N\) is batch size, \(C\) denotes number of channels, and \(H\) is length of 1D data element.

When groups == in_channels and out_channels == K * in_channels, where K is a positive integer, this operation is also known as depthwise convolution.

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

Parameters
  • in_channels (int) – number of input channels.

  • out_channels (int) – number of output channels.

  • kernel_size (int) – size of weight on spatial dimensions.

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

  • 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. Default: default.

  • padding_mode (str) – “zeros”, “reflect” or “replicate”. Default: “zeros”. Refer to Pad for more information.

Shape:

input: \((N, C_{\text{in}}, H_{\text{in}})\). output: \((N, C_{\text{out}}, H_{\text{out}})\).

Note

  • weight usually has shape (out_channels, in_channels, kernel_size) , if groups is not 1, shape will be (groups, out_channels // groups, in_channels // groups, kernel_size)

  • bias usually has shape (1, out_channels, 1)

Returns

module. The instance of the Conv1d module.

Return type

Return type

Examples

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