# conv1d_transpose¶

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

The 1-D convolution transpose layer calculates the output based on the input, filter, and dilation, stride, padding. Input(Input) and output(Output) are in ‘NCL’ format or ‘NLC’ where N is batch size, C is the number of channels, L is the length of the feature. 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.

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

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

Where:

• $$X$$: Input value, a 3-D Tensor with ‘NCL’ format or ‘NLC’ format.

• $$W$$: Filter value, a 3-D Tensor with ‘MCK’ format.

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

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

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

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

Example

• Input:

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

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

• Output:

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

Where

$\begin{split}L^\prime_{out} &= (L_{in} - 1) * stride - pad_top - pad_bottom + dilation * (L_f - 1) + 1 + output_padding \\\\ L_{out} &\in [ L^\prime_{out}, L^\prime_{out} + stride ]\end{split}$

Note

The conv1d_transpose can be seen as the backward of the conv1d. For conv1d, when stride > 1, conv1d maps multiple input shape to the same output shape, so for conv1d_transpose, when stride > 1, input shape maps multiple output shape. If output_size is None, $$L_{out} = L^\prime_{out}$$; else, the $$L_{out}$$ of the output size must between $$L^\prime_{out}$$ and $$L^\prime_{out} + stride$$. conv1d_transpose can compute the kernel size automatically.

Parameters
• x (Tensor) – 3-D tensor with [N, C, L] or [N, L, C] format, its data type is float32 or float64.

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

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

• stride (int|tuple|list, optional) – The stride size. It means the stride in transposed convolution. If stride is a tuple, it must contain one integer, (stride_size). Default: stride = 1.

• output_padding (int|list|tuple, optional) – The count of zeros to be added to tail of each dimension. If it is a tuple, it must contain one integer. Default: 0.

• groups (int, optional) – The groups number of the conv1d transpose function. 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|tuple|list, optional) – The dilation size. It means the spacing between the kernel points. If dilation is a tuple, it must contain one integer, (dilation_size). Default: dilation = 1.

• output_size (int|tuple|list, optional) – The output image size. If output size is a tuple, it must contain one integer, (feature_length). None if use filter_size, padding, and stride to calculate output_size. If output_size and filter_size are specified at the same time, They should follow the formula above. Default: None. output_size and filter_size should not be None at the same time.

• 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: “NCL”, “NLC”. The default is “NCL”. When it is “NCL”, the data is stored in the order of: [batch_size, input_channels, input_length].

• 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 result of 1-D transpose convolution, whose data type is the same with input. And its shape is (num_batches, channels, length) when data_format is “NCL” and (num_batches, length, channels) when data_format is “NLC”.

Raises
• ValueError – If data_format is a string, but not “NCL” or “NLC”.

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

• ValueError – If padding is a 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 filter_size are None at the same time.

• ValueError – If output_padding is greater than stride.

• ShapeError – If the input is not 3-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 1.

• 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
import numpy as np

# shape: (1, 2, 4)
x=np.array([[[4, 0, 9, 7],
[8, 0, 9, 2,]]]).astype(np.float32)
# shape: (2, 1, 2)
w=np.array([[[7, 0]],
[[4, 2]]]).astype(np.float32)