# conv2d_transpose¶

The convolution2D transpose layer calculates the output based on the input, filter, and dilations, strides, paddings. Input(Input) and output(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. Parameters(dilations, strides, paddings) are two elements. These two elements represent height and width, respectively. The details of convolution transpose layer, please refer to the following explanation and references therein. 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. See more detail in api_nn_conv_ConvTranspose2d .

For each input $$X$$, the equation is:

$Out = \sigma (W \ast X + b)$

Where:

• $$X$$: Input value, a 4-D Tensor with NCHW or NHWC format.

• $$W$$: Filter value, a 4-D Tensor with MCHW format.

• $$\\ast$$: Convolution operation.

• $$b$$: Bias value, a 2-D Tensor with shape [M, 1].

• $$\\sigma$$: Activation function.

• $$Out$$: Output value, a 4-D Tensor with data format ‘NCHW’ or ‘NHWC’, the shape of $$Out$$ and $$X$$ may be different.

Example

• Input:

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

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

• Output:

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

Where

$\begin{split}H^\prime_{out} &= (H_{in} - 1) * strides - pad_height_top - pad_height_bottom + dilations * (H_f - 1) + 1 \\\\ W^\prime_{out} &= (W_{in} - 1) * strides - pad_width_left - pad_width_right + dilations * (W_f - 1) + 1 \\\\ H_{out} &\in [ H^\prime_{out}, H^\prime_{out} + strides ] \\\\ W_{out} &\in [ W^\prime_{out}, W^\prime_{out} + strides ]\end{split}$

Note

The conv2d_transpose can be seen as the backward of the conv2d. For conv2d, when stride > 1, conv2d maps multiple input shape to the same output shape, so for conv2d_transpose, when stride > 1, input shape maps multiple output shape. If output_size is None, $$H_{out} = H^\prime_{out}, W_{out} = W^\prime_{out}$$; else, the $$H_{out}$$ of the output size must between $$H^\prime_{out}$$ and $$H^\prime_{out} + strides$$, and the $$W_{out}$$ of the output size must between $$W^\prime_{out}$$ and $$W^\prime_{out} + strides$$.

Parameters
• x (Tensor) – 4-D Tensor with [N, C, H, W] or [N, H, W, C] format, whose data type is float32 or float64.

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

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

• stride (int|list|tuple, optional) – The stride size. It means the stride in transposed 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.

• output_padding (int|list|tuple, optional) – Additional size added to one side of each dimension in the output shape. Default: 0.

• groups (int, optional) – The groups number of the Conv2D transpose layer. Inspired by grouped convolution in Alex Krizhevsky’s Deep CNN paper, in which 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.

• 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.

• output_size (int|tuple|list, optional) – The output image size. If output size is a tuple/list, it must contain two integers, (image_height, image_width). None if use filter_size(shape of weight), padding, and stride to calculate output_size.

• 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_transpose, whose data type is the same with input and shape is (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels). The tensor variable storing transposed convolution result.

Raises
• ValueError – If data_format is not “NCHW” or “NHWC”.

• ValueError – If padding is a string, but not “SAME” or “VALID”.

• ValueError – If padding is a list/tuple, but the element corresponding to the input’s batch size is not 0 or the element corresponding to the input’s channel is not 0.

• ValueError – If output_size and kernel_size are None at the same time.

• ShapeError – If the input is not 4-D Tensor.

• ShapeError – If the input’s dimension size and filter’s dimension size not equal.

• ShapeError – If the dimension size of input minus the size of stride is not 2.

• ShapeError – If the number of input channels is not equal to filter’s channels.

• ShapeError – If the size of output_size is not equal to that of stride.

Examples

import paddle