Conv1D

class paddle.nn. Conv1D ( in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, padding_mode='zeros', weight_attr=None, bias_attr=None, data_format='NCL' ) [source]

This interface is used to construct a callable object of the Conv1D class. For more details, refer to code examples. The convolution1D layer calculates the output based on the input, filter and stride, padding, dilation, groups parameters. Input and Output are in NCL format or NLC format, where N is batch size, C is the number of the feature map, L is the length of the feature map. Filter’s shape is [MCK] , where M is the number of output feature map, C is the number of input feature map, K is the size of the kernel. If the groups is greater than 1, C will equal the number of input feature map divided by the groups. If bias attribution and activation type are provided, bias is added to the output of the convolution, and the corresponding activation function is applied to the final result.

For each input \(X\) , the equation is:

\[Out = \sigma (W \ast X + b)\]

Where:

  • \(X\): Input value, a Tensor with ‘NCL’ format or ‘NLC’ format.

  • \(W\): Filter value, a Tensor with shape [MCK] .

  • \(\ast\): Convolution operation.

  • \(b\): Bias value, a 1-D Tensor with shape [M].

  • \(\sigma\): Activation function.

  • \(Out\): Output value, the shape of \(Out\) and \(X\) may be different.

Example

  • Input:

    Input shape: \((N, C_{in}, L_{in})\)

    Kernel shape: \((C_{out}, C_{in}, K)\)

  • Output:

    Output shape: \((N, C_{out}, L_{out})\)

Where

\[L_{out}&= \frac{(L_{in} + 2 * padding - (dilation * (L_f - 1) + 1))}{stride} + 1\]
Parameters
  • in_channels (int) – The number of channels in the input image.

  • out_channels (int) – The number of filter. It is as same as the output feature map.

  • kernel_size (int|tuple|list) – The filter size. If kernel_size is a tuple/list, it must contain one integer, (kernel_size).

  • stride (int|tuple|list, optional) – The stride size. If stride is a tuple/list, it must contain one integer, (stride_size). Default: 1.

  • padding (int|str|tuple|list, optional) – The size of zeros to be padded. It must be in one of the following forms. 1. a string in [‘valid’, ‘same’]. 2. an int, which means the feature map is zero paded by size of padding on both sides. 3. a list[int] or tuple[int] whose length is 1, which means the feature map is zero paded by size of padding[0] on both sides. The default value is 0.

  • dilation (int|tuple|list, optional) – The dilation size. If dilation is a tuple/list, it must contain one integer, (dilation_size). Default: 1.

  • groups (int, optional) – The groups number of the conv2d Layer. According to grouped convolution in Alex Krizhevsky’s Deep CNN paper: when group=2, the first half of the filters is only connected to the first half of the input channels, while the second half of the filters is only connected to the second half of the input channels. Default: 1.

  • padding_mode (str, optional) – Four modes: ‘zeros’, ‘reflect’, ‘replicate’, ‘circular’. When in ‘zeros’ mode, this op uses zeros to pad the input tensor. When in ‘reflect’ mode, uses reflection of the input boundaries to pad the input tensor. When in ‘replicate’ mode, uses input boundaries to pad the input tensor. When in ‘circular’ mode, uses circular input to pad the input tensor. Default is ‘zeros’.

  • weight_attr (ParamAttr, optional) – The parameter attribute for learnable weights(Parameter) of conv1d. If it is set to None or one attribute of ParamAttr, conv1d will create ParamAttr as param_attr. If the Initializer of the param_attr is not set, the parameter is initialized with \(Normal(0.0, std)\), and the \(std\) is \((\frac{2.0 }{filter\_elem\_num})^{0.5}\). Default: None.

  • bias_attr (ParamAttr or bool, optional) – The attribute for the bias of conv1d. If it is set to False, no bias will be added to the output units. If it is set to None or one attribute of ParamAttr, conv1d will create ParamAttr as bias_attr. If the Initializer of the bias_attr is not set, the bias is initialized zero. Default: None.

Attribute:

weight (Parameter): the learnable weights of filter of this layer.

bias (Parameter or None): the learnable bias of this layer.

Shape:
  • x: 3-D tensor with shape: (batch, in_channels, length) or (batch, length, in_channels).

  • weight: 3-D tensor with shape: (out_channels, in_channels, kernel_size)

  • bias: 1-D tensor with shape: (out_channels)

  • output: 3-D tensor with same shape as input x.

Examples

>>> import paddle
>>> from paddle.nn import Conv1D

>>> x = paddle.to_tensor([[[4, 8, 1, 9],
... [7, 2, 0, 9],
... [6, 9, 2, 6]]], dtype="float32")
>>> w = paddle.to_tensor([[[9, 3, 4],
... [0, 0, 7],
... [2, 5, 6]],
... [[0, 3, 4],
... [2, 9, 7],
... [5, 6, 8]]], dtype="float32")

>>> conv = Conv1D(3, 2, 3)
>>> conv.weight.set_value(w)
>>> y = conv(x)
>>> print(y)
Tensor(shape=[1, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[[133., 238.],
[160., 211.]]])
forward ( x )

forward

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments