There is a time, we all need to squeeze a tensor in PyTorch. And here, I will explain how to squeeze a tensor in PyTorch. We will also discuss, how to squeeze a 5D tensor in PyTorch.
Recently, I was working in PyTorch, where we got a requirement for Squeeze() in PyTorch. So, in this blog, I will also explain, how to squeeze a tensor in PyTorch.
So, we will cover these topics in this Python tutorial.
- What is the Squeeze()
- How to squeeze a tensor in PyTorch
- How to squeeze a 5D tensor in PyTorch
- How to squeeze a tensor in 0 dimensions in PyTorch
- How to squeeze a tensor in 1 dimension in PyTorch
- How to squeeze a tensor in 2 dimensions in PyTorch
What is the Squeeze()
Squeeze a tensor we are using the torch.squeeze() function. When we squeeze a tensor, the squeeze method gives back a new tensor of all the dimensions but detaches the size 1.
For example: If the input tensor has the shape ( P * 1 * Q * 1 * R * 1 ) and we get out tensor after applying the squeeze method will be the shape of ( P * Q * R).
With the help of this example, you can see that the Squeeze() method gives back the new tensor of all the dimensions but it removes the size 1.
Syntax:
The syntax of the squeeze() method:
torch.squeeze(input, dim = None, out= None)
Parameters:
- input: The input parameter is used as an input tensor.
- dim = None: The dim is an optional integer value if given the input is squeezed in this dimension.
- out: The out is used as an output tensor, it is an optional key argument.
So, with this, we understand What is Squeeze() method in PyTorch.
Read: Cross Entropy Loss PyTorch
How to squeeze a tensor in PyTorch
In this section, we will learn about How to squeeze a tensor in PyTorch with the help of an example.
The squeeze method is used to return a tensor with all the dimensions of the input of size one removed.
Example:
In the following example, we are importing the torch library and then describe the input variable after that we are printing the tensor size of an input variable and then we are using the squeeze method.
# Import torch library
import torch
# Describing the variable
p = torch.zeros(4, 1, 4, 1, 4)
# Print the tensor size
print(p.size())
# Describing the variable
q = torch.squeeze(p)
# Print the tensor size
print(q.size())
# Describing the variable
q = torch.squeeze(p, 0)
# Print the tensor size
print(q.size())
# Describing the variable
q = torch.squeeze(p, 1)
# Print the tensor size
print(q.size())
Output:
After running the above code we get the following output in which we can see that after using the Squeeze() method we get the new tensor of all the dimensions but it removes the size 1.
This is how we understand how to squeeze a tensor in PyTorch.
Read: PyTorch nn linear + Examples
How to squeeze a 5D tensor in PyTorch
In this section, we will learn about how to squeeze a 5D tensor in PyTorch.
Here we squeezed a 5D tensor by using a torch.squeeze() method and the input tensor has two dimensions that have size 1.
Example:
In the following example, we are importing the torch library and then describe the input tensor variable after that we are printing the tensor size of an input variable and then we are using the squeeze method after that we are printing the size after the squeeze tensor.
# importing torch library
import torch
# Describing the input tensor
inp = torch.randn(5,1,4,1,6)
# print the input tensor
print("Input tensor Size:\n",inp.size())
# Squeeze the tensor
out = torch.squeeze(inp)
# print the size after squeeze tensor
print("Size after squeeze:\n",out.size())
Output:
After running the above code we get the following output in which we can see that squeezed a 5d tensor by using the Squeeze() method and we get the input tensor has two dimensions that have size 1.
So, with this, we understand how to squeeze a 5d tensor in PyTorch.
Read: PyTorch Batch Normalization
How to squeeze a tensor in 0 dimensions in PyTorch
In this section, we will learn How to squeeze a tensor in 0 dimensions in PyTorch.
Here we squeezed a tensor in 0 dimensions by using a torch.squeeze() method and we get the new tensor of all the dimensions but it removes the size 1.
Code:
In the following code, we are importing the torch library and then we are describing the input tensor by using a torch.randn() function after that we are using the squeeze() function of the tensor in dimension 0. And then print the size of the tensor after squeeze.
# importing torch
import torch
# Describing the input tensor
inp = torch.randn(5,1,4,1,6)
print("Dimension of input tensor:", inp.dim())
print("Input tensor Size:\n",inp.size())
# squeeze the tensor in dimension 0
out = torch.squeeze(inp,dim=0)
print("Size of tensor after squeeze with dim=0:\n",
out.size())
Output:
In this output, you can see that the squeeze method returns a new tensor dimension of size one.
In this way, we have learned about how to squeeze a tensor in PyTorch.
Read: PyTorch Pretrained Model
How to squeeze a tensor in 1 dimension in PyTorch
In this section, we will learn how to squeeze a tensor in 1 dimension in PyTorch.
Here we are squeezing a tensor in 1 dimension in PyTorch. It returns a new tensor with every dimension of the input tensor.
Code:
In the following example, we are importing the torch library and then describe the input tensor variable after that we are printing the tensor size of an input variable and then we are using the squeeze method of tensor in dimension 1. After that, we are printing the size after the squeeze tensor.
# importing torch
import torch
# creating the input tensor
inp_tensor = torch.randn(4,1,3,1,5)
print("Dimension of input tensor:", inp_tensor.dim())
print("Input tensor Size:\n",inp_tensor.size())
# squeeze the tensor in dimension 1
out_tensor = torch.squeeze(inp_tensor,dim=1)
print("Size of tensor after squeeze with dim=1:\n",
out_tensor.size())
Output:
After running the above code, we get the following output in which we can see that we squeeze in dimension 1, only this dimension is removed in the output tensor.
So, with this, we understand how to squeeze a tensor in 1 dimension in PyTorch.
Read: PyTorch Binary Cross Entropy
How to squeeze a tensor in 2 dimension in PyTorch
In this section, we will learn how to squeeze a tensor in 2 dimensions in PyTorch.
Here we are squeezing a tensor in 2 dimensions in PyTorch. In the squeeze in dimension 2, there is no change in the shape of the output tensor.
Code:
In the following example, we are importing the torch library and then describe the input tensor variable after that we are printing the tensor size of an input variable and then we are using the squeeze method of tensor in dimension 2. After that, we are printing the size after the squeeze tensor.
# importing torch
import torch
# creating the input tensor
inp = torch.randn(6,1,5,1,7)
print("Dimension of input tensor:", inp.dim())
print("Input tensor Size:\n",inp.size())
# squeeze the tensor in dimension 2
out = torch.squeeze(input,dim=2)
print("Size after squeeze with dim=2:\n",
out.size())
Output:
After running the above code, we get the following output in which we can see that we squeeze in dimension 2, and there is nothing change in the output tensor.
Conclusion
In this tutorial, we have learned how to squeeze a tensor in PyTorch, and also we have covered how to squeeze a 5D tensor in PyTorch. And we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- What is the Squeeze()
- How to squeeze a tensor in PyTorch
- How to squeeze a 5D tensor in PyTorch
- How to squeeze a tensor in 0 dimensions in PyTorch
- How to squeeze a tensor in 1 dimension in PyTorch
- How to squeeze a tensor in 2 dimension in PyTorch
Also, take a look at some more Python PyTorch tutorials.
- Create PyTorch Empty Tensor
- PyTorch Stack Tutorial + Examples
- How to use PyTorch Cat function
- How to use PyTorch Polar
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.