conv2d

paddle.nn.functional. conv2d ( x, weight, bias=None, stride=1, padding=0, dilation=1, groups=1, data_format='NCHW', name=None ) [source]

The convolution2D layer calculates the output based on the input, filter and strides, paddings, dilations, groups parameters. Input and Output are in NCHW or NHWC format, where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. Filter is in MCHW format, where M is the number of output image channels, C is the number of input image channels, H is the height of the filter, and W is the width of the filter. If the groups is greater than 1, C will equal the number of input image channels divided by the groups. Please refer to UFLDL’s convolution for more details. 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 NCHW or NHWC format.

  • \(W\): Filter value, a tensor with MCHW format.

  • \(\\ast\): Convolution operation.

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

  • \(\\sigma\): Activation function.

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

Example

  • Input:

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

    Filter shape: \((C_{out}, C_{in}, H_f, W_f)\)

  • Output:

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

Where

\[\begin{split}H_{out}&= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]} + 1 \\\\ W_{out}&= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]} + 1\end{split}\]
Parameters
  • x (Tensor) – The input is 4-D Tensor with shape [N, C, H, W], the data type of input is float16 or float32 or float64.

  • weight (Tensor) – The convolution kernel with shape [M, C/g, kH, kW], where M is the number of output channels, g is the number of groups, kH is the filter’s height, kW is the filter’s width.

  • bias (Tensor, optional) – The bias with shape [M,].

  • stride (int|list|tuple, optional) – The stride size. It means the stride in convolution. If stride is a list/tuple, it must contain two integers, (stride_height, stride_width). Otherwise, stride_height = stride_width = stride. Default: stride = 1.

  • padding (string|int|list|tuple, optional) – The padding size. It means the number of zero-paddings on both sides for each dimension.If padding is a string, either ‘VALID’ or ‘SAME’ which is the padding algorithm. If padding size is a tuple or list, it could be in three forms: [pad_height, pad_width] or [pad_height_top, pad_height_bottom, pad_width_left, pad_width_right], and when data_format is “NCHW”, padding can be in the form [[0,0], [0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]. when data_format is “NHWC”, padding can be in the form [[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]. Default: padding = 0.

  • dilation (int|list|tuple, optional) – The dilation size. It means the spacing between the kernel points. If dilation is a list/tuple, it must contain two integers, (dilation_height, dilation_width). Otherwise, dilation_height = dilation_width = dilation. Default: dilation = 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: groups=1.

  • data_format (str, optional) – Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from: “NCHW”, “NHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width].

  • name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

Returns

A Tensor representing the conv2d result, whose data type is the same with input.

Examples

>>> import paddle
>>> import paddle.nn.functional as F

>>> x_var = paddle.randn((2, 3, 8, 8), dtype='float32')
>>> w_var = paddle.randn((6, 3, 3, 3), dtype='float32')

>>> y_var = F.conv2d(x_var, w_var)

>>> print(y_var.shape)
[2, 6, 6, 6]