What is Transposed Convolutional Layer?
Last Updated :
26 Apr, 2025
A transposed convolutional layer is an upsampling layer that generates the output feature map greater than the input feature map. It is similar to a deconvolutional layer. A deconvolutional layer reverses the layer to a standard convolutional layer. If the output of the standard convolution layer is deconvolved with the deconvolutional layer then the output will be the same as the original value, While in transposed convolutional value will not be the same, it can reverse to the same dimension,
Transposed convolutional layers are used in a variety of tasks, including image generation, image super-resolution, and image segmentation. They are particularly useful for tasks that involve upsampling the input data, such as converting a low-resolution image to a high-resolution one or generating an image from a set of noise vectors.
The operation of a transposed convolutional layer is similar to that of a normal convolutional layer, except that it performs the convolution operation in the opposite direction. Instead of sliding the kernel over the input and performing element-wise multiplication and summation, a transposed convolutional layer slides the input over the kernel and performs element-wise multiplication and summation. This results in an output that is larger than the input, and the size of the output can be controlled by the stride and padding parameters of the layer.
Transposed Convolutional with stride 2
In a transposed convolutional layer, the input is a feature map of size I_h \times I_w , where I_h and I_w are the height and width of the input and the kernel size is K_h \times K_w , where K_h and K_w are the height and width of the kernel.
If the stride shape is (s_h,s_w) and the padding is p, The stride of the transposed convolutional layer determines the step size for the input indices p and q, and the padding determines the number of pixels to add to the edges of the input before performing the convolution. Then the output of the transposed convolutional layer will be
O_h = (I_h -1) \times s_h + K_h -2p \\ O_w = (I_w -1) \times s_w + K_h -2p
where O_h and O_w are the height and width of the output.
Example 1:
Suppose we have a grayscale image of size 2 X 2, and we want to upsample it using a transposed convolutional layer with a kernel size of 2 x 2, a stride of 1, and zero padding (or no padding). The input image and the kernel for the transposed convolutional layer would be as follows:
Input = \begin{bmatrix} 0 & 1\\ 2 & 3 \end{bmatrix}
Kernel = \begin{bmatrix} 4 & 1\\ 2 & 3 \end{bmatrix}
The output will be:
Transposed Convolutional Stride = 1Method 1: Manually with TensorFlow
Code Explanations:
- Import necessary libraries (TensorFlow and NumPy)
- Define Input tensor and custom kernel
- Apply Transpose convolution with kernel size =2, stride = 1.
- Write the custom functions for transpose convolution
- Apply Transpose convolution on input data.
Python3
# Import Tensorflow
import tensorflow as tf
import numpy as np
# Input
Input = tf.constant([[0.0, 1.0], [2.0, 3.0]], dtype=tf.float32)
# Kernel
Kernel = tf.constant([[4.0, 1.0], [2.0, 3.0]], dtype=tf.float32)
# Define the transpose convolution function
def trans_conv(Input, Kernel):
h, w = Kernel.shape
Y = np.zeros((Input.shape[0] + h - 1, Input.shape[1] + w - 1))
for i in range(Input.shape[0]):
for j in range(Input.shape[1]):
Y[i: i + h, j: j + w] += Input[i, j] * Kernel
return tf.constant(Y)
# OUTPUT
trans_conv(Input, Kernel)
Output:
<tf.Tensor: shape=(3, 3), dtype=float64, numpy=
array([[ 0., 4., 1.],
[ 8., 16., 6.],
[ 4., 12., 9.]])>
The output shape can be calculated as :
\begin{aligned}O_h &= (I_h -1) \times s_h + K_h -2p \\ &= (2-1)\times 1 + 2 -2\times0 \\ &= 1\times 1 + 2-0 \\ &=3\end{aligned} \\ \begin{aligned}O_w &= (I_w -1) \times s_w + K_w -2p \\ &= (2-1)\times 1 + 2 -2\times0 \\ &= 1\times 1 + 2-0 \\ &=3\end{aligned}
Method 2: With PyTorch:
Code Explanations:
- Import necessary libraries (torch and nn from torch)
- Define Input tensor and custom kernel
- Redefine the shape in 4 dimensions because PyTorch takes 4D shapes in inputs.
- Apply Transpose convolution with input and output channel =1,1, kernel size =2, stride = 1, padding = 0 means valid padding.
- Set the customer kernel weight by using Transpose.weight.data
- Apply Transpose convolution on input data.
Python3
# Import the necessary module
import torch
from torch import nn
# Input
Input = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
#Kernel
Kernel = torch.tensor([[4.0, 1.0], [2.0, 3.0]])
# Redefine the shape in 4 dimension
Input = Input.reshape(1, 1, 2, 2)
Kernel = Kernel.reshape(1, 1, 2, 2)
# Transpose convolution Layer
Transpose = nn.ConvTranspose2d(in_channels =1,
out_channels =1,
kernel_size=2,
stride = 1,
padding=0,
bias=False)
# Initialize Kernel
Transpose.weight.data = Kernel
# Output value
Transpose(Input)
Output:
tensor([[[[ 0., 4., 1.],
[ 8., 16., 6.],
[ 4., 12., 9.]]]], grad_fn=<ConvolutionBackward0>)
Transposed convolutional layers are often used in conjunction with other types of layers, such as pooling layers and fully connected layers, to build deep convolutional networks for various tasks.
Example 2: Valid Padding
In valid padding, no extra layer of zeros will be added.
Python3
import tensorflow as tf
# Define the input tensor
input_tensor = tf.constant([[
[[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]],
[[17, 18, 19], [21, 22, 23], [25, 26, 27], [29, 30, 31]],
[[33, 34, 35], [37, 38, 39], [41, 42, 43], [45, 46, 47]],
[[49, 50, 51], [53, 54, 55], [57, 58, 59], [61, 62, 63]]
]], dtype=tf.float32)
# Define the transposed convolutional layer
transposed_conv_layer = tf.keras.layers.Conv2DTranspose(
filters=1, kernel_size=3, strides=2, padding='valid')
# Apply the transposed convolutional layer to the input tensor
output = transposed_conv_layer(input_tensor)
# Print the output shape
print(output.shape)
Output:
(1, 9, 9, 1)
Example 3: Same Padding
In same padding, an Extra layer of zeros (known as the padding layer) will be added.
Python3
import tensorflow as tf
# Define the input tensor
input_tensor = tf.constant([[
[[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]],
[[17, 18, 19], [21, 22, 23], [25, 26, 27], [29, 30, 31]],
[[33, 34, 35], [37, 38, 39], [41, 42, 43], [45, 46, 47]],
[[49, 50, 51], [53, 54, 55], [57, 58, 59], [61, 62, 63]]
]], dtype=tf.float32)
# Define the transposed convolutional layer
transposed_conv_layer = tf.keras.layers.Conv2DTranspose(
filters=1, kernel_size=3, strides=2, padding='same')
# Apply the transposed convolutional layer to the input tensor
output = transposed_conv_layer(input_tensor)
# Print the output shape
print(output.shape)
Output:
(1, 8, 8, 1)
Similar Reads
Convolutional Layers in TensorFlow
Convolutional layers are the foundation of Convolutional Neural Networks (CNNs), which excel at processing spatial data such as images, time-series data, and volumetric data. These layers apply convolutional filters to extract meaningful features like edges, textures, and patterns. List of Convoluti
2 min read
What are Convolution Layers?
Convolution layers are fundamental components of convolutional neural networks (CNNs), which have revolutionized the field of computer vision and image processing. These layers are designed to automatically and adaptively learn spatial hierarchies of features from input images, enabling tasks such a
4 min read
What is a 1D Convolutional Layer in Deep Learning?
Answer: A 1D Convolutional Layer in Deep Learning applies a convolution operation over one-dimensional sequence data, commonly used for analyzing temporal signals or text.A 1D Convolutional Layer (Conv1D) in deep learning is specifically designed for processing one-dimensional (1D) sequence data. Th
2 min read
Types of padding in convolution layer
Let's discuss padding and its types in convolution layers. In convolution layer we have kernels and to make the final filter more informative we use padding in image matrix or any kind of input array. We have three types of padding that are as follows. Padding Full : Let's assume a kernel as a slidi
5 min read
Dilated Convolution
Prerequisite: Convolutional Neural Networks Dilated Convolution: It is a technique that expands the kernel (input) by inserting holes between its consecutive elements. In simpler terms, it is the same as convolution but it involves pixel skipping, so as to cover a larger area of the input. Dilated
5 min read
Tensorflow.js tf.layers.conv2d() Function
Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.
3 min read
Tensorflow.js tf.layers.conv3d() Function
Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.
3 min read
Tensorflow.js tf.layers.conv1d() Function
Tensorflow.js is a javascript library developed by Google to run and train machine learning models in the browser or in Node.js. Tensorflow.js tf.layers.conv1d() function is used to create convolution layer. It is used to applied 1d convolution to the input data. The convolutional layer is used to m
4 min read
What is Fractional Convolution?
By extending the meaning of convolution beyond integer values, fractional convolution offers a more adaptable method for performing mathematical operations and signal processing. Fractional convolution offers a greater degree more granularity in the combination process by allowing for intermediate s
12 min read
Tensorflow.js tf.layers.convLstm2d() Function
Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The tf.layers.convLstm2d() function is used for creating a ConvRNN2D layer which consists of one ConvLSTM2DCell a
5 min read