How to use PyTorch Cat function

The PyTorch cat function is used to concatenate the given order of seq tensors in the given dimension. In detail, we will discuss the cat function using PyTorch in Python.

And additionally, we will cover different examples related to the PyTorch Cat function. And we will cover these topics.

  • What is PyTorch cat function
  • PyTorch cat function example
  • How to use PyTorch cat function using dimension as -1
  • PyTorch cat function using dimension as 0
  • How to use PyTorch  3D  tensor  are  concatenate(cat)  along  0  and -1 dimension
  • PyTorch cat vs stack

PyTorch cat function

In this section, we will learn about the Pytorch cat function in python.

The PyTorch cat function is used to concatenate the given order of seq tensors in the given dimension and the tensors must either have the same shape.

Syntax:

Syntax of the PyTorch cat function:

torch.cat(tensors, dim=0, out=None)

Parameters:

The following are the parameters of the PyTorch cat function:

  • tensors: The tensor is a parameter of any python sequence of tensors of the same type and the non-empty tensors provided must have the same shape, except in the cat dimension.
  • dim=0: The dim is used as a dimension over which the tensors are concatenated.
  • out=None: The out is defined as an output tensor and the default value of out is None.

So, with this, we understood the Pytorch cat function by using a torch.cat() function.

Read: Create PyTorch Empty Tensor

PyTorch cat function example

In this section, we will learn how we can implement the PyTorch cat function with the help of an example in python.

The torch.cat() function is used to concatenate two or more tensors along the existing axis.

Code:

In the following code, we will import the required library such as import torch.

  • c = torch.randn(4, 6): Here we are declaring the variable by using the torch.randn() function.
  • a=torch.cat((c, c, c), 0): Here we are calling the torch.cat() function.
  • print(a) is used to print the variable a by using the print() function.
# Import the torch library
import torch
# Declaring the variable
c = torch.randn(4, 6)
print(c)
# Calling the cat() function
a=torch.cat((c, c, c), 0)
b=torch.cat((c, c, c), 1)
# Print the output
print(a)
print(b)

Output:

READ:  How to Create model in Django

After running the above code, we get the following output in which we can see that the PyTorch cat values are printed on the screen.

PyTorch cat function example
PyTorch cat function example

So, with this, we understood about how to implement the Pytorch cat function with the help of an example.

Read: PyTorch Flatten + 8 Examples

How to use PyTorch  cat  function  using  dimension as -1

In this section, we will learn about the PyTorch cat function using dimension as -1 in python.

Here we are using the torch.cat() function that concatenates the two or more tensors row-wise by using dim as -1.

Code:

In the following code, we will import the necessary library such as import torch.

  • cattens1 = torch.Tensor([[12, 13, 14], [15, 16, 17]]): Here we are declaring the cattensor variable by using torch.tensor() function.
  • print(“Cattensor1 \n”, cattens1): Here we are printing the first tensor by using the print() function.
  • cattensor = torch.cat((cattens1, cattens2), -1): Here we are calling the the torch.cat() function.
  • print(“Concatenate the tensors in the -1 dimension \n”, cattensor): Here we are printing the concatenate tensor in the -1 dimension.
# import torch library
import torch
  
# Declaring the tensors
cattens1 = torch.Tensor([[12, 13, 14], [15, 16, 17]])
cattens2 = torch.Tensor([[18,19, 20], [21, 22, 23]])
  
# print first tensors
print("Cattensor1 \n", cattens1)
  
# print second tensor
print("Cattensor2 \n", cattens2)
  
# Calling the torch.cat() function and join tensor in -1 dimension
cattensor = torch.cat((cattens1, cattens2), -1)
print("Concatenate the tensors in the -1 dimension \n", cattensor)

Output:

After running the above code, we get the following output in which we can see that the PyTorch cat function using dimension as -1 values are printed on the screen.

PyTorch cat function using dimension as -1
PyTorch cat function using dimension as -1

So, with this, we understood about the PyTorch cat function using dimension as -1.

Read: PyTorch View Tutorial

PyTorch cat function using dimension as 0

In this section, we will learn about the PyTorch cat function using dimension as 0 in python.

Here we are using the torch.cat() function that concatenates the two or more tensors column-wise by using dimension as 0.

Code:

In the following code, we will import the required library such as import torch.

  • c1 = torch.tensor([2,4,6,8]): Here we are declaring the variable by using torch.tensor() function.
  • print(“cat1:\n”, c1) is used to print the tensor that we have to create above with the help of the print() function.
  • c=torch.cat((c1,c2,c3,c4),dim=0 ): Here we are calling the torch.cat() function for concatenate the tensors.
  • print(c) is used to print the output by using the print() function.
# Import library
import torch
# Declaring the variable
c1 = torch.tensor([2,4,6,8])
c2 = torch.tensor([3,6,9,12])
c3 = torch.tensor([4,8,12,16])
c4 = torch.tensor([5,10,15,20])
# Print the above created tensors
print("cat1:\n", c1)
print("cat2:\n", c2)
print("cat3:\n", c3)
print("cat4:\n", c4)
# Calling the torch.cat() function
c=torch.cat(
    (c1,c2,c3,c4)
    ,dim=0
)
# Print the output
print(c)

Output:

READ:  How to Iterate Over Tensor In TensorFlow

After running the above code, we get the following output in which we can see that the PyTorch cat function using dimension as 0 values are printed on the screen.

PyTorch cat function using dimension as 0
PyTorch cat function using dimension as 0

This is how we can understand about the PyTorch cat function using dimension as 0.

Read: Introduction to PyTorch Lenet

How to use PyTorch  3D tensor  are  concatenate(cat) along 0 and -1 dimension

In this section, we will learn about the PyTorch 3d tensor and concatenate along 0 and -1 dimensions in python.

Here we are using the torch.cat() function that concatenates the two or more tensors column-wise and row-wise by using dimensions as 0 and -1.

Code:

In the following code, we will import the necessary library such as import torch.

  • c1 = torch.Tensor([[2,4],[3,6]]) is used to create the tensor by using the torch.tensor() function.
  • print(“cat1:\n”, c1) is used to print the above tensor that we created by using the print() function.
  • c = torch.cat((c1,c2,c3), 0): Here we are calling the torch.cat() function along with 0 dimension.
  • print(“Concatenate the tensors in the 0 dimension”) is used to print the concatenate tensors in the 0 dimensions.
  • c = torch.cat((c1,c2,c3), -1): Here we are calling the torch.cat() function along with -1 dimension.
# import required library
import torch

# create the tensors
c1 = torch.Tensor([[2,4],[3,6]])
c2 = torch.Tensor([[4,8],[5,10]])
c3 = torch.Tensor([[6,12],[7,14]])

# print above created tensors
print("cat1:\n", c1)
print("cat2:\n", c2)
print("cat3:\n", c3)

# Print the tensor in the 0 dimension
print("Concatenate the tensors in the 0 dimension")
# Calling the torch.cat() function
c = torch.cat((c1,c2,c3), 0)
print("c:\n", c)

print("Concatenate the tensors in the -1 dimension")
c = torch.cat((c1,c2,c3), -1)
print("c:\n", c)

Output:

In the below output, we can see that the PyTorch 3d tensor is concatenated (cat) along 0 and -1 dimension values printed on the screen.

PyTorch 3D tensor are concatenate(cat) along 0 and -1 dimension
PyTorch 3D tensor are concatenate(cat) along 0 and -1 dimension

So, with this, we understood how the PyTorch 3D tensor are concatenate( cat) along 0 and -1 dimensions.

Read: PyTorch Reshape Tensor

PyTorch cat vs stack

In this section, we will learn about the PyTorch cat vs stack in python.

Cat():

The torch.cat() function is used to concatenate the given order of seq tensors in the given dimension and the tensors must either have the same shape.

READ:  modulenotfounderror: no module named 'matplotlib'

Code:

In the following code, we will import the required library such as import torch.

  • c1 = torch.tensor([2,4,6,8]): Here we are declaring the tensor by using the torch.tensor() function.
  • c=torch.cat((c1,c2,c3,c4),dim=0 ): Here we are calling the cat() function.
  • print(c) is used to print the output by using the print() function.
# Import torch
import torch
# Declaring the tensors
c1 = torch.tensor([2,4,6,8])
c2 = torch.tensor([3,6,9,12])
c3 = torch.tensor([4,8,12,16])
c4 = torch.tensor([5,10,15,20])
print(c1,c2,c3,c4)
# Calling the cat() function
c=torch.cat(
    (c1,c2,c3,c4)
    ,dim=0
)
# Print the output
print(c)

Output:

After running the above code, we get the following output in which we can see that the PyTorch cat values are printed on the screen.

PyTorch cat
PyTorch cat

Stack():

The PyTorch torch.stack() function is used to concatenate the tensor with the same dimension and shape.

Code:

In the following code, we will import the required library such as import torch.

  • s1 = torch.tensor([2,4,6,8]) is used to declaring the tensor by using the torch.tensor() function.
  • s=torch.stack((s1,s2,s3,s4),dim=0 ): Here we are calling the torch.stack() function.
# Import torch
import torch
# Declaring the tensors
s1 = torch.tensor([2,4,6,8])
s2 = torch.tensor([3,6,9,12])
s3 = torch.tensor([4,8,12,16])
s4 = torch.tensor([5,10,15,20])
# Calling stack() function
s=torch.stack(
    (s1,s2,s3,s4)
    ,dim=0
)
# Print the output
print(s)

Output:

After running the above code, we get the following output in which we can see that PyTorch stack() values are printed on the screen.

PyTorch stack
PyTorch stack

This is how we understand the difference between the cat() and stack() functions.

You may also like to read the following PyTorch tutorials in Python.

So, in this tutorial, we discussed the PyTorch cat function and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • What is PyTorch cat function?
  • PyTorch cat function example
  • How to use PyTorch cat function using dimension as -1
  • PyTorch cat function using dimension as 0
  • How to use PyTorch  3D  tensor  are concatenate(cat)  along  0 and -1 dimension
  • PyTorch cat vs stack