Apply a 2D Transposed Convolution Operation in PyTorch
Last Updated :
24 Apr, 2025
Transposed convolution, also known as fractionally-strided convolution, is a technique used in convolutional neural networks (CNNs) for the upsampling layer that increases the spatial resolution of an image. 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 Stride = 1
In this article, we will discuss how to apply a 2D transposed convolution operation in PyTorch.
Before diving into the implementation of transposed convolution in PyTorch, let’s first understand the basic concepts related to the topic.
- Convolution: Convolution is a mathematical operation that applies a filter to an image to extract features. In CNNs, a convolutional layer applies a set of filters to an input image to extract features.
- Transposed Convolution: Transposed convolution, also known as fractionally-strided convolution or deconvolution, is the reverse operation of convolution. It is used to increase the spatial resolution of an image by expanding the feature maps produced by a convolutional layer.
- Stride: Stride is the number of pixels by which the filter moves in the image. A larger stride means fewer feature maps, while a smaller stride results in more feature maps.
- Padding: Padding is the number of pixels added to the edges of an image to preserve its spatial size after convolution.
- Output Shape: The output shape of a transposed convolution operation depends on the input shape, the kernel size, the stride, and the padding.
In a transposed convolutional layer, the input is a feature map of size [Tex]I_h \times I_w [/Tex], where [Tex]I_h [/Tex] and [Tex]I_w [/Tex] are the height and width of the input and the kernel size is [Tex]K_h \times K_w [/Tex], where [Tex]K_h [/Tex] and [Tex]K_w [/Tex] are the height and width of the kernel.
If the stride shape is [Tex](s_h,s_w) [/Tex] and 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
[Tex]O_h = (I_h -1) \times s_h + K_h -2p \\ O_w = (I_w -1) \times s_w + K_h -2p[/Tex]
where [Tex]O_h [/Tex] and [Tex]O_w [/Tex] 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 2, and zero padding (or no padding). The input image and the kernel for the transposed convolutional layer would be as follows:
[Tex]Input = \begin{bmatrix} 0 & 1\\ 2 & 3 \end{bmatrix}[/Tex]
[Tex]Kernel = \begin{bmatrix} 4 & 1\\ 2 & 3 \end{bmatrix}[/Tex]
The output will be:

Transposed Convolutional with stride 2
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 = 2, padding = 0 means valid padding.
- Set the customer kernel weight by using Transpose.weight.data
- Apply Transpose convolution on input data.
Python3
import torch
from torch import nn
Input = torch.tensor([[ 0.0 , 1.0 ], [ 2.0 , 3.0 ]])
Kernel = torch.tensor([[ 4.0 , 1.0 ], [ 2.0 , 3.0 ]])
Input = Input .reshape( 1 , 1 , 2 , 2 )
Kernel = Kernel.reshape( 1 , 1 , 2 , 2 )
Transpose = nn.ConvTranspose2d(in_channels = 1 ,
out_channels = 1 ,
kernel_size = 2 ,
stride = 2 ,
padding = 0 ,
bias = False )
Transpose.weight.data = Kernel
Transpose( Input )
|
Output:
tensor([[[[ 0., 0., 4., 1.],
[ 0., 0., 2., 3.],
[ 8., 2., 12., 3.],
[ 4., 6., 6., 9.]]]], grad_fn=<ConvolutionBackward0>)
The output shape can be calculated as :
[Tex]\begin{aligned}O_h &= (I_h -1) \times s_h + K_h -2p \\ &= (2-1)\times 2 + 2 -2\times0 \\ &= 1\times 2 + 2-0 \\ &=4\end{aligned} \\ \begin{aligned}O_w &= (I_w -1) \times s_w + K_w -2p \\ &= (2-1)\times 2 + 2 -2\times0 \\ &= 1\times 2 + 2-0 \\ &=4\end{aligned}[/Tex]
Example:2
Let’s create a tensor of shape (1,1,4,4) with torch.randn() and apply transpose convolution with torch.nn.ConvTranspose2d with pytorch with kernel size(3,3) and stride(2,2) and padding (1,1).
Python
import torch
import torch.nn as nn
input_image = torch.randn( 1 , 1 , 4 , 4 )
print ( 'Input Shape:' ,input_image.shape)
kernel_size = ( 3 , 3 )
stride = ( 2 , 2 )
padding = ( 1 , 1 )
transposed_conv = nn.ConvTranspose2d(in_channels = 1 ,
out_channels = 1 ,
kernel_size = kernel_size,
stride = stride,
padding = padding)
output = transposed_conv(input_image)
print ( "output \n" , output)
print ( "\n output Shape" , output.shape)
|
Output:
Input Shape: torch.Size([1, 1, 4, 4])
output
tensor([[[[ 0.2094, 0.3711, 0.1221, 0.0517, 0.4600, 0.0966, 0.4605],
[ 0.1893, 0.2858, 0.2451, 1.0030, 0.7390, -0.6206, -0.0103],
[ 0.1951, 0.2099, 0.2970, -0.1894, 0.7507, 0.6869, -0.1451],
[-0.2257, 0.8582, 0.3090, 0.5730, 0.4639, 0.2012, 0.2094],
[-0.2951, 0.1390, 0.3026, 0.2176, 0.3044, 0.1649, 0.3625],
[ 0.3149, 0.3095, 0.5061, -0.0233, 0.2429, 0.6422, 0.4626],
[ 0.5492, 0.0399, 0.5359, 0.3251, 0.2207, 0.0652, 0.4598]]]],
grad_fn=<ConvolutionBackward0>)
output Shape torch.Size([1, 1, 7, 7])
The output shape can be calculated as :
[Tex]\begin{aligned}O_h &= (I_h -1) \times s_h + K_h -2p \\ &= (4-1)\times 2 + 3 -2\times1 \\ &= 3\times 2 + 3-2 \\ &= 6 + 3 -1 \\ &=7\end{aligned} \\ \begin{aligned}O_w &= (I_w -1) \times s_w + K_w -2p \\ &= (4-1)\times 2 + 3 -2\times1 \\ &= 3\times 2 + 3-2 \\ &= 6 + 3 -1 \\ &=7\end{aligned}[/Tex]
Example 3:
Let’s apply the transpose convolution on a real image here we will read the image with PIL library PIL.image() function and convert it into PyTorch tensor with torchvision.transforms.ToTensor() and then applying custom kernel. if here we will not define the kernel the output will refresh every time on running the because of random kernel. here kernel size is 2, stride = 2 and padding = 1.
Input Image
.jpg)
Input Image
Python
from PIL import Image
import torch
from torch import nn
from torchvision import transforms
img = Image. open ( 'Ganesh.jpg' )
img = transforms.ToTensor()(img)
print ( "Input image size:" , img.size())
img = img.unsqueeze( 0 )
print ( 'unsqueeze Image size' ,img.shape)
Kernel = torch.tensor([
[[[ 1.0 , 0.1 ],[ 0.1 , 0.2 ]],[[ 0.1 , 0.2 ],[ 0.2 , 0.3 ]],[[ 0 , 0.1 ],[ 0.2 , 0.3 ]]],
[[[ 1.0 , 0.1 ],[ 0.1 , 0.2 ]],[[ 0.1 , 0.2 ],[ 0.2 , 0.3 ]],[[ 0 , 0.1 ],[ 0.2 , 0.3 ]]],
[[[ 1.0 , 0.1 ],[ 0.1 , 0.2 ]],[[ 0.1 , 0.2 ],[ 0.2 , 0.3 ]],[[ 0 , 0.1 ],[ 0.2 , 0.3 ]]],
])
print ( 'Kernel Size :' ,Kernel.shape)
Transpose = nn.ConvTranspose2d(in_channels = 3 ,
out_channels = 2 ,
kernel_size = 2 ,
stride = 2 ,
padding = 1 ,
bias = False )
Transpose.weight.data = Kernel
img2 = Transpose(img)
img2 = img2.squeeze( 0 )
print ( "Output image size:" ,img2.size())
img2 = transforms.ToPILImage()(img2)
img2
|
Output:
Input image size: torch.Size([3, 394, 358])
unsqueeze Image size torch.Size([1, 3, 394, 358])
Kernel Size : torch.Size([3, 3, 2, 2])
Output image size: torch.Size([3, 786, 714])

Output Image
The output shape can also be calculated as :
[Tex]\begin{aligned}O_h &= (I_h -1) \times s_h + K_h -2p \\ &= (394-1)\times 2 + 2 -2\times1 \\ &= 393\times 2 + 2-2 \\ &= 786 + 2 -2 \\ &=786\end{aligned} \\ \begin{aligned}O_w &= (I_w -1) \times s_w + K_w -2p \\ &= (358-1)\times 2 + 2 -2\times1 \\ &= 357\times 2 + 2-2 \\ &= 714 + 2 -2 \\ &=714\end{aligned}[/Tex]
Similar Reads
Apply a 2D Convolution Operation in PyTorch
A 2D Convolution operation is a widely used operation in computer vision and deep learning. It is a mathematical operation that applies a filter to an image, producing a filtered output (also called a feature map). In this article, we will look at how to apply a 2D Convolution operation in PyTorch.
8 min read
How to Apply a 2D Average Pooling in PyTorch?
In this article, we will see how to apply a 2D average pooling in PyTorch. AvgPool2d() method AvgPool2d() method of torch.nn module is used to apply 2D average pooling over an input image composed of several input planes in PyTorch. The shape of the input 2D average pooling layer should be [N, C, H,
2 min read
What is Transposed Convolutional Layer?
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
6 min read
Tensor Operations in PyTorch
In this article, we will discuss tensor operations in PyTorch. PyTorch is a scientific package used to perform operations on the given data like tensor in python. A Tensor is a collection of data like a numpy array. We can create a tensor using the tensor function: Syntax: torch.tensor([[[element1,e
5 min read
Apply a 2D Max Pooling in PyTorch
Pooling is a technique used in the CNN model for down-sampling the feature coming from the previous layer and produce the new summarised feature maps. In computer vision reduces the spatial dimensions of an image while retaining important features. The goal of pooling is to reduce the computational
6 min read
How to crop an image at random location in PyTorch
In this article, we will discuss how to pad an image on all sides in PyTorch. Torchvision.transforms.RandomCrop method Cropping is a technique of removal of unwanted outer areas from an image to achieve this we use a method in python that is torchvision.transforms.RandomCrop(). It is used to crop an
2 min read
How to Define a Simple Convolutional Neural Network in PyTorch?
In this article, we are going to see how to Define a Simple Convolutional Neural Network in PyTorch using Python. Convolutional Neural Networks(CNN) is a type of Deep Learning algorithm which is highly instrumental in learning patterns and features in images. CNN has a unique trait which is its abil
5 min read
Building a Convolutional Neural Network using PyTorch
Convolutional Neural Networks (CNNs) are deep learning models used for image processing tasks. They automatically learn spatial hierarchies of features from images through convolutional, pooling and fully connected layers. In this article we'll learn how to build a CNN model using PyTorch. This incl
6 min read
Pytorch Functions - tensor(), fill_diagnol(), append(), index_copy()
This article aims to share some PyTorch functions that will help you a lot in your deep learning and data science journey. Each function will be explained using two write examples and one example where you can't use those functions. So let's get started. PyTorch is an open-source machine learning li
9 min read
Converting a List of Tensors to a Single Tensor in PyTorch
PyTorch, a popular deep learning framework, provides powerful tools for tensor manipulation. One common task in PyTorch is converting a list of tensors into a single tensor. This operation is crucial for various applications, including data preprocessing, model input preparation, and tensor operatio
4 min read