mindspore.nn.Conv1d

class mindspore.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init=None, bias_init=None, dtype=mstype.float32)[source]

1D convolution layer.

Applies a 1D convolution over an input tensor which is typically of shape \((N, C_{in}, L_{in})\), where \(N\) is batch size, \(C\) is channel number, \(L\) is input sequence width.

The output is calculated based on formula:

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

where \(bias\) is the output channel bias, \(ccor\) is the cross-correlation, \(weight\) is the convolution kernel value and \(X\) represents the input feature map.

Here are the indices’ meanings:

  • \(i\) corresponds to the batch number, the range is \([0, N-1]\), where \(N\) is the batch size of the input.

  • \(j\) corresponds to the output channel, the range is \([0, C_{out}-1]\), where \(C_{out}\) is the number of output channels, which is also equal to the number of kernels.

  • \(k\) corresponds to the input channel, the range is \([0, C_{in}-1]\), where \(C_{in}\) is the number of input channels, which is also equal to the number of channels in the convolutional kernels.

Therefore, in the above formula, \({bias}(C_{\text{out}_j})\) represents the bias of the \(j\)-th output channel, \({weight}(C_{\text{out}_j}, k)\) represents the slice of the \(j\)-th convolutional kernel in the \(k\)-th channel, and \({X}(N_i, k)\) represents the slice of the \(k\)-th input channel in the \(i\)-th batch of the input feature map.

The shape of the convolutional kernel is given by \((\text{kernel_size})\), where \(\text{kernel_size}\) is the width of the kernel. If we consider the input and output channels as well as the group parameter, the complete kernel shape will be \((C_{out}, C_{in} / \text{group}, \text{kernel_size})\), where group is the number of groups dividing x’s input channel when applying group convolution.

For more details about convolution layer, please refer to Gradient Based Learning Applied to Document Recognition and ConvNets .

Note

On Ascend platform, only group convolution in depthwise convolution scenarios is supported. That is, when group>1, condition in_channels = out_channels = group must be satisfied.

Parameters
  • in_channels (int) – The channel number of the input tensor of the Conv1d layer.

  • out_channels (int) – The channel number of the output tensor of the Conv1d layer.

  • kernel_size (int) – Specifies the width of the 1D convolution kernel.

  • stride (int, optional) – The movement stride of the 1D convolution kernel. Default: 1 .

  • pad_mode (str, optional) –

    Specifies the padding mode with a padding value of 0. It can be set to: "same" , "valid" or "pad" . Default: "same" .

    • "same": Pad the input at the begin and end so that the shape of input and output are the same when stride is set to 1. The amount of padding to is calculated by the operator internally. If the amount is even, it is uniformly distributed around the input, if it is odd, the excess padding is goes to the right side. If this mode is set, padding must be 0.

    • "valid": No padding is applied to the input, and the output returns the maximum possible length. Extra pixels that could not complete a full stride will be discarded. If this mode is set, padding must be 0.

    • "pad": Pad the input with a specified amount. In this mode, the amount of padding at the begin and end is determined by the padding parameter. If this mode is set, padding must be greater than or equal to 0.

  • padding (Union(int, tuple[int], list[int]), optional) – Specifies the amount of padding to apply on both side of input when pad_mode is set to "pad". The paddings of left and right are the same, equal to padding or padding[0] when padding is a tuple of 1 integer. Default: 0 .

  • dilation (Union(int, tuple[int]), optional) – Specifies the dilation rate to use for dilated convolution. It can be a single int or a tuple of 1 integer. Assuming \(dilation=(d0,)\), the convolutional kernel samples the input with a spacing of \(d0-1\) elements in the width direction. The value should be in the ranges [1, L]. Default: 1 .

  • group (int, optional) – Splits filter into groups, in_channels and out_channels must be divisible by group. Default: 1 .

  • has_bias (bool, optional) – Whether the Conv1d layer has a bias parameter. Default: False .

  • weight_init (Union[Tensor, str, Initializer, numbers.Number], optional) – Initialization method of weight parameter. It can be a Tensor, a string, an Initializer or a numbers.Number. When a string is specified, values from 'TruncatedNormal' , 'Normal' , 'Uniform' , 'HeUniform' and 'XavierUniform' distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias 'xavier_uniform' , 'he_uniform' , 'ones' and 'zeros' are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer, for more details. Default: None , weight will be initialized using 'HeUniform'.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number], optional) –

    Initialization method of bias parameter. Available initialization methods are the same as ‘weight_init’. Refer to the values of Initializer, for more details. Default: None , bias will be initialized using 'Uniform'.

  • dtype (mindspore.dtype) – Dtype of Parameters. Default: mstype.float32 .

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, L_{in})\) .

Outputs:

Tensor of shape \((N, C_{out}, L_{out})\).

pad_mode is 'same':

\[L_{out} = \left \lceil{\frac{L_{in}}{\text{stride}}} \right \rceil\]

pad_mode is 'valid':

\[L_{out} = \left \lceil{\frac{L_{in} - \text{dilation} \times (\text{kernel_size} - 1) } {\text{stride}}} \right \rceil\]

pad_mode is 'pad':

\[L_{out} = \left \lfloor{\frac{L_{in} + 2 \times padding - (\text{kernel_size} - 1) \times \text{dilation} - 1 }{\text{stride}} + 1} \right \rfloor\]
Raises
  • TypeError – If in_channels, out_channels, kernel_size, stride, padding or dilation is not an int.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> net = nn.Conv1d(120, 240, 4, has_bias=False, weight_init='normal')
>>> x = Tensor(np.ones([1, 120, 640]), mindspore.float32)
>>> output = net(x).shape
>>> print(output)
(1, 240, 640)