Monday, June 15, 2026
HomeLanguagesHow to Pad the Input Tensor Boundaries With a Constant Value in...

How to Pad the Input Tensor Boundaries With a Constant Value in PyTorch?

In this article, we will discuss How to Pad the Input Tensor Boundaries With a Constant Value in PyTorch. We can pad the input tensor boundaries with a constant value by using torch.nn.ConstantPad2D() method.

torch.nn.ConstantPad2D() method 

We can pad the boundaries of 3D and 4D tensors and the shape of the input tensor is [C, H, W] and [N, C, H, W] respectively, where N represents the mini-batch size, C represents the number of channels, and  H, W represents the height and width respectively. This method accepts the size of padding and a constant value as input. The boundaries may be the same or different from all sides (left, right, top, bottom). we can increase the height and width of a padded tensor by using top+bottom and left+right respectively. The below syntax is used to pad the input tensor boundaries with a Constant Value.

Syntax: torch.nn.ConstantPad2d(pad, value)

Parameter:

  • pad (int, tuple): This is size of padding. The size of padding is an integer or a tuple.
  • value: This is constant value. 

Return: This method returns a new tensor with boundaries.

Example 1:

In this example, we will see how to add the same padding sizes to all sides.

Python3




# Import required library
import torch
import torch.nn as nn
 
# define a tensor
tens = torch.tensor([[[21, 22], [23, 24]]])
print("\n Input Tensor: \n", tens)
 
# give padding size same for all sides
pad = nn.ConstantPad2d(2, 9)
output = pad(tens)
 
# display result
print("\n After Pad Input Tensor: \n", output)


Output:

How to Pad the Input Tensor Boundaries With a Constant Value in PyTorch?

 

Example 2:

In this example, we will see how to add unique padding sizes to all sides (left, right, top, bottom).

Python3




# Import required library
import torch
import torch.nn as nn
 
# define a tensor
tens = torch.tensor([[[11, 12], [13, 14]]])
print("\n Input Tensor: \n", tens)
 
# add unique padding sizes to all sides
# (left, right, top, bottom)
pad = nn.ConstantPad2d((1, 2, 3, 4), 8)
output = pad(tens)
 
# display result
print("\n After Pad Input Tensor:\n", output)


Output:

How to Pad the Input Tensor Boundaries With a Constant Value in PyTorch?

 

Example 3:

In this example, we will see how to pad the boundaries of a batch of tensors.

Python3




# Import required library
import torch
import torch.nn as nn
 
# define a batch of tensor
tens = torch.tensor([[[11, 12], [13, 14]],
                     [[21, 22], [23, 24]]])
 
print("\n Input Tensor: \n", tens)
 
# add unique padding sizes to all sides
# (left, right, top, bottom)
pad = nn.ConstantPad2d(1, 8)
output = pad(tens)
 
# display result
print("\n After Pad Input Tensor:\n", output)


Output:

How to Pad the Input Tensor Boundaries With a Constant Value in PyTorch?

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS