# dltranspconv

Deep learning transposed convolution

## Description

The transposed convolution operation upsamples feature maps.

The dltranspconv function applies the deep learning transposed convolution operation to dlarray data. Using dlarray objects makes working with high dimensional data easier by allowing you to label the dimensions. For example, you can label which dimensions correspond to spatial, time, channel, and batch dimensions using the "S", "T", "C", and "B" labels, respectively. For unspecified and other dimensions, use the "U" label. For dlarray object functions that operate over particular dimensions, you can specify the dimension labels by formatting the dlarray object directly, or by using the DataFormat option.

Note

This function applies the deep learning transposed convolution operation to dlarray data. If you want to apply transposed convolution within a layerGraph object or Layer array, use one of the following layers:

example

Y = dltranspconv(X,weights,bias) computes the deep learning transposed convolution of the input X using the filters defined by weights, and adds the constant bias. The input X must be a formatted dlarray. The output Y is a formatted dlarray with the same dimension format as X.

The function, by default, convolves over up to three dimensions of X labeled "S" (spatial). To convolve over dimensions labeled "T" (time), specify weights with a "T" dimension using a formatted dlarray object or by using the WeightsFormat option.

For unformatted input data, use the DataFormat option.

Y = dltranspconv(X,weights,bias,DataFormat=FMT) applies the deep learning transposed convolution operation to the unformatted dlarray object X with format specified by FMT. The output Y is an unformatted dlarray object with dimensions in the same order as X.

example

Y = dltranspconv(___Name=Value) specifies options using one or more name-value pair arguments in addition to the input arguments in previous syntaxes. For example, Stride=3 sets the stride of the convolution operation.

## Examples

collapse all

Create a formatted dlarray object containing a batch of 128 28-by-28 images with 3 channels. Specify the format "SSCB" (spatial, spatial, channel, batch).

miniBatchSize = 128;
inputSize = [28 28];
numChannels = 3;
X = rand(inputSize(1),inputSize(2),numChannels,miniBatchSize);
X = dlarray(X,"SSCB");

View the size and format of the input data.

size(X)
ans = 1×4

28    28     3   128

dims(X)
ans =
'SSCB'

Initialize the weights and bias for 2-D transposed convolution. For the weights, specify 64 3-by-3 filters. For the bias, specify a vector of zeros.

filterSize = [3 3];
numFilters = 64;

weights = rand(filterSize(1),filterSize(2),numFilters,numChannels);
bias = zeros(1,numFilters);

Apply 2-D transposed convolution using the dltranspconv function.

Y = dltranspconv(X,weights,bias);

View the size and format of the output.

size(Y)
ans = 1×4

30    30    64   128

dims(Y)
ans =
'SSCB'

Create a formatted dlarray object containing a batch of 128 28-by-28 images with 16 channels. Specify the format "SSCB" (spatial, spatial, channel, batch).

miniBatchSize = 128;
inputSize = [28 28];
numChannels = 16;
X = rand(inputSize(1),inputSize(2),numChannels,miniBatchSize);
X = dlarray(X,"SSCB");

View the size and format of the input data.

size(X)
ans = 1×4

28    28    16   128

dims(X)
ans =
'SSCB'

Initialize the weights and bias for 2-D grouped transposed convolution. For the weights, specify two groups of 64 3-by-3 filters. For the bias, specify a vector of zeros.

The number of channels per group is given by the number of channels of the input data divided by the number of groups. The size of the bias vector is the number of filters per group multiplied by the number of groups.

filterSize = [3 3];
numFiltersPerGroup = 64;
numGroups = 2;
numChannelsPerGroup = numChannels / numGroups;

weights = rand(filterSize(1),filterSize(2),numFiltersPerGroup,numChannelsPerGroup,numGroups);
bias = zeros(1,numFiltersPerGroup*numGroups);

Apply 2-D grouped transposed convolution using the dltranspconv function.

Y = dltranspconv(X,weights,bias);

View the size and format of the output.

size(Y)
ans = 1×4

30    30   128   128

dims(Y)
ans =
'SSCB'

## Input Arguments

collapse all

Input data, specified as a formatted dlarray, an unformatted dlarray, or a numeric array.

If X is an unformatted dlarray or a numeric array, then you must specify the format using the DataFormat option. If X is a numeric array, then either weights or bias must be a dlarray object.

The function, by default, convolves over up to three dimensions of X labeled "S" (spatial). To convolve over dimensions labeled "T" (time), specify weights with a "T" dimension using a formatted dlarray object or by using the WeightsFormat option.

Filters, specified as a formatted dlarray, an unformatted dlarray, or a numeric array.

The size and format of the weights depends on the type of task. If weights is an unformatted dlarray or a numeric array, then the size and shape of weights depends on the WeightsFormat option.

The following table describes the size and format of the weights for various tasks. You can specify an array with the dimensions in any order using formatted dlarray objects or by using the WeightsFormat option. When the weights has multiple dimensions with the same label (for example, multiple dimensions labeled "S"), then those dimensions must be in ordered as described in this table.

WeightsFormat
1-D transposed convolution"S" (spatial) or "T" (time)Filter size

filterSize-by-numFilters-by-numChannels array, where filterSize is the size of the 1-D filters, numFilters is the number of filters, and numChannels is the number of channels of the input data.

"SCU" (spatial, channel, unspecified)
"C" (channel)Number of channels
"U" (unspecified)Number of filters
1-D grouped transposed convolution"S" (spatial) or "T" (time)Filter size

filterSize-by-numFiltersPerGroup-by-numChannelsPerGroup-by-numGroups array, where filterSize is the size of the 1-D filters, numFiltersPerGroup is the number of filters per group, numChannelsPerGroup is the number of channels per group of the input data, and numGroups is the number groups.

numChannelsPerGroup must equal the number of the channels of the input data divided by numGroups.

"SCUU" (spatial, channel, unspecified, unspecified)
"C" (channel)Number of channels per group
First "U" (unspecified)Number of filters per group
Second "U" (unspecified)Number of groups
2-D transposed convolutionFirst "S" (spatial)Filter height

filterSize(1)-by-filterSize(2)-by-numFilters-by-numChannels array, where filterSize(1) and filterSize(2) are the height and width of the 2-D filters, respectively, numFilters is the number of filters, and numChannels is the number of channels of the input data.

"SSCU" (spatial, spatial, channel, unspecified)
Second "S" (spatial) or "T" (time)Filter width
"C" (channel)Number of channels
"U" (unspecified)Number of filters
2-D grouped transposed convolutionFirst "S" (spatial)Filter height

filterSize(1)-by-filterSize(2)-by-numFiltersPerGroup-by-numChannelsPerGroup-by-numGroups array, where filterSize(1) and filterSize(2) are the height and width of the 2-D filters, respectively, numFiltersPerGroup is the number of filters per group, numChannelsPerGroup is the number of channels per group of the input data, and numGroups is the number of groups.

numChannelsPerGroup must equal the number of the channels of the input data divided by numGroups.

"SSCUU" (spatial, spatial, channel, unspecified, unspecified)
Second "S" (spatial) or "T" (time)Filter width
"C" (channel)Number of channels per group
First "U" (unspecified)Number of filters per group
Second "U" (unspecified)Number of groups
3-D transposed convolutionFirst "S" (spatial)Filter height

filterSize(1)-by-filterSize(2)-by-filterSize(3)-by-numFilters-by-numChannels array, where filterSize(1), filterSize(2), and filterSize(3) are the height, width, and depth of the 3-D filters, respectively, numFilters is the number of filters, and numChannels is the number of channels of the input data.

"SSSCU" (spatial, spatial, spatial, channel, unspecified)
Second "S" (spatial)Filter width
Third "S" (spatial) or "T" (time)Filter depth
"C" (channel)Number of channels
"U" (unspecified)Number of filters

Tip

The function, by default, convolves over up to three dimensions of X labeled "S" (spatial). To convolve over dimensions labeled "T" (time), specify weights with a "T" dimension using a formatted dlarray object or by using the WeightsFormat option.

Bias constant, specified as a formatted or unformatted dlarray vector or dlarray scalar, a numeric vector, or a numeric scalar.

• If bias is a scalar or has only singleton dimensions, the same bias is applied to each entry of the output.

• If bias has a nonsingleton dimension, each element of bias is the bias applied to the corresponding convolutional filter specified by weights. The number of elements of bias must match the number of filters specified by .

If bias is a formatted dlarray, the nonsingleton dimension must be a channel dimension labeled "C".

### Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: Stride=2 sets the stride of each filter to 2.

Dimension order of unformatted input data, specified as a character vector or string scalar FMT that provides a label for each dimension of the data.

When you specify the format of a dlarray object, each character provides a label for each dimension of the data and must be one of the following:

• "S" — Spatial

• "C" — Channel

• "B" — Batch (for example, samples and observations)

• "T" — Time (for example, time steps of sequences)

• "U" — Unspecified

You can specify multiple dimensions labeled "S" or "U". You can use the labels "C", "B", and "T" at most once.

You must specify DataFormat when the input data is not a formatted dlarray.

Data Types: char | string

Dimension order of the weights, specified as a character vector or string scalar that provides a label for each dimension of the weights.

The default value of WeightsFormat depends on the task:

1-D transposed convolution"SCU" (spatial, channel, unspecified)
1-D grouped transposed convolution"SCUU" (spatial, channel, unspecified, unspecified)
2-D transposed convolution"SSCU" (spatial, spatial, channel, unspecified)
2-D grouped transposed convolution"SSCUU" (spatial, spatial, channel, unspecified, unspecified)
3-D transposed convolution"SSSCU" (spatial, spatial, spatial, channel, unspecified)

The supported combinations of dimension labels depends on the type of convolution, for more information, see the weights argument.

Tip

The function, by default, convolves over up to three dimensions of X labeled "S" (spatial). To convolve over dimensions labeled "T" (time), specify weights with a "T" dimension using a formatted dlarray object or by using the WeightsFormat option.

Data Types: char | string

Step size for traversing the input data, specified as a numeric scalar or numeric vector.

To use the same step size for all convolution dimensions, specify the stride as a scalar. To specify a different value for each convolution dimension, specify the stride as a vector with elements ordered corresponding to the dimensions labels in the data format.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Filter dilation factor, specified as specified as a numeric scalar or numeric vector.

To use the dilation factor all convolution dimensions, specify the dilation factor as a scalar. To specify a different value for each convolution dimension, specify the dilation factor as a vector with elements ordered corresponding to the dimensions labels in the data format.

Use the dilation factor to increase the receptive field of the filter (the area of the input that the filter can see) on the input data. Using a dilation factor corresponds to an effective filter size of filterSize + (filterSize-1)*(dilationFactor-1).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Cropping applied to edges of data, specified as one of the following.

• "same" — Cropping is set so that the output size is the same as the input size when the stride is 1. More generally, the output size of each spatial dimension is inputSize*stride, where inputSize is the size of the input along the convolution dimension.

• Numeric scalar — The same cropping value is applied to both ends of the convolution dimensions.

• Numeric vector — A different cropping value is applied along each convolution dimension. Use a vector of size d, where d is the number of convolution dimensions of the input data. The ith element of the vector specifies the cropping applied to the start and the end along the ith convolution dimension.

• Numeric matrix — A different cropping value is applied to the start and end of each convolution dimension. Use a matrix of size 2-by-d, where d is the number of convolution dimensions of the input data. The element (1,d) specifies the cropping applied to the start of convolution dimension d. The element (2,d) specifies the cropping applied to the end of convolution dimension d. For example, in 2-D the format is [top, left; bottom, right].

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

## Output Arguments

collapse all

Feature map, returned as a dlarray. The output Y has the same underlying data type as the input X.

If the input data X is a formatted dlarray, then Y has the same format as X. If the input data is not a formatted dlarray, then Y is an unformatted dlarray or numeric array with the same dimension order as the input data.

The size of the "C" (channel) dimension of Y depends on the size of the weights input. The size of the "C" (channel) dimension of output Y is the product of the size of the dimensions numFiltersPerGroup and numGroups in the weights argument. If weights is a formatted dlarray, this product is the same as the product of the size of the "C" (channel) dimension and the second "U" (unspecified) dimension.

## Algorithms

collapse all

### Transposed Convolution

The standard convolution operation downsamples the input by applying sliding convolutional filters to the input. By flattening the input and output, you can express the convolution operation as $Y=CX+B$ for the convolution matrix C and bias B that can be derived from the layer weights and biases.

Similarly, the transposed convolution operation upsamples the input by applying sliding convolutional filters to the input. To upsample the input instead of downsampling using sliding filters, the layer zero-pads each edge of the input with padding that has the size of the corresponding filter edge size minus 1.

By flattening the input and output, the transposed convolution operation is equivalent to $Y={C}^{\top }X+B$, where C and B denote the convolution and bias matrices for standard convolution derived from the layer weights and biases, respectively. This operation is equivalent to the backward function of a standard convolution layer.

## Version History

Introduced in R2019b