Cat PyTorch function explained with examples

An open-source framework for the Python programming language named PyTorch is crucial in machine-learning duties. The provided order of seq tensors in the given dimension is concatenated using the PyTorch cat function. This masterpiece delves into great detail on the Python PyTorch cat function.

Data is stored in multidimensional arrays called tensors. Therefore, we must import the torch module to use a tensor. tensor() to build a tensor.

The syntax is as follows:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>torch.tensor(data)</pre>
</div>

Where the data in the syntax illustration from the case above refers to an array with several dimensions.

Cat PyTorch function

The Python Pytorch cat function is covered in this section. The specified order of seq tensors in the given dimension is concatenated using the PyTorch cat function; the tensors must all have the same form.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>torch.cat()</pre>
</div>

Use torch.cat() to combine two or more tensors into a row or column. Thus, to represent the torch.cat(), use the following syntax:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>torch.cat((tensor_object1,tensor_object2,………..),dim)</pre>
</div>

Exploring the parameters

  • Tensors: The tensor is a parameter of any Python sequence of tensors of the same type, and the provided non-empty tensors must all be the same shape except for the cat dimension.
  • dim=0: The tensors are concatenated over the dim, which is utilized as a dimension.
  • out=None: The default value of out, described as an output tensor, is None.

As its initial parameter, it accepts two or more tensors. The tensors are concatenated column-wise if dim=0. On the other hand, tensors are concatenated row-wise if dim=1.

Using the cat function in PyTorch

In this section, we will learn how to implement the PyTorch cat function with a Python example. Use the torch to concatenate two or more tensors along the current axis.cat() function.

We shall import the necessary library in the following code, such as import torch.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>var_ct = torch.randn(7, 9)</pre>
</div>

Here, we’re utilizing the torch.randn() function to declare the variable.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>var_a=torch.cat((var_ct, var_ct, var_ct), 0)</pre>
</div>

Here, the torch.cat() function is being called. The print() method is primarily responsible for printing the value of the variable var_a. The complete representation of this concept is as follows:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#begin by importing the library: torch
import torch
# Declaring the variable
var_ct = torch.randn(7, 9)

print(var_ct)
# initiating the function, cat()
var_a=torch.cat((var_ct, var_ct, var_ct), 0)
var_b=torch.cat((var_ct, var_ct, var_ct), 1)
# Print the output
print(var_a)
print(var_b)</pre>
</div>

Multiple Sequences of Tensors Concatenated

This example demonstrates combining four distinct tensors into one tensor using cat(). The PyTorch library is first imported, then the desired tensor sequences are created using the tensor function.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>var_t1 = torch.tensor([1,2,3,4])
var_t2 = torch.tensor([5,6,7,8])
var_t3 = torch.tensor([9,10,11,12])
var_t4 = torch.tensor([13,14,15,16])</pre>
</div>

We now combine them, and as we can see, the four distinct tensors have been combined to create a single tensor along axis 0, the only available one.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>torch.cat(
    (var_t1,var_t2,var_t3,var_t4)
    ,dim=0
)</pre>
</div>

PyTorch cat function using dimension as 0

This part teaches us how to use Python’s PyTorch cat function with a dimension set to 0. Here, we’re utilizing the torch.cat() method joins two or more tensors together by concatenating them column-wise with a dimension of 0.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># library importation
import torch

# variable declaration
var_1_c = torch.tensor([2,4,6,8])
var_2_c = torch.tensor([3,6,9,12])
var_3_c = torch.tensor([4,8,12,16])
var_4_c = torch.tensor([5,10,15,20])

# view the tensors we've created above
print("var 1:\n", var_1_c)
print("var 2:\n", var_2_c)
print("var 3:\n", var_3_c)
print("var 4:\n", var_4_c)

# Calling the torch.cat() function
var_c=torch.cat(
    (var_1_c,var_2_c,var_3_c,var_4_c)
    ,dim=0
)
# showing the output
print(var_c)</pre>
</div>

How to use the Python cat function with the dimension set to -1

This section will discover how to use Python’s PyTorch cat function with a dimension set to -1. Here, we’re utilizing the torch.cat() method uses dim as -1 to concatenate two or more tensors row-wise.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># begin by importing the library- torch
import torch
  
# Tensor's declaration
var_1_tens = torch.Tensor([[12, 13, 14], [15, 16, 17]])
var_2_tens = torch.Tensor([[18,19, 20], [21, 22, 23]])
  
# print first tensors
print("Tensor One: \n", var_1_tens)
  
# print second tensor
print("Tensor Two \n", var_2_tens )
  
# Calling the torch.cat() function and join tensor in -1 dimension
var_tensor = torch.cat((var_1_tens, var_2_tens), -1)
print("Tensors concatenation in the -1 dimension \n", var_tensor)
</pre>
</div>

Concatenate (3D tensor) along the 0 and -1 dimensions in PyTorch

This part teaches us how to concatenate along 0 and -1 dimensions in Python and about the PyTorch 3D tensor. In this case, we’re utilizing the torch.cat() method joins two or more tensors row- and column-wise using the dimensions 0 and -1.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># begin by importing the required library
import torch

# tensors' creation
var_1_c = torch.Tensor([[2,4],[3,6]])
var_2_c = torch.Tensor([[4,8],[5,10]])
var_3_c = torch.Tensor([[6,12],[7,14]])

# print above created tensors
print("var 1:\n", var_1_c)
print("var 2:\n", var_2_c)
print("var 3:\n", var_3_c )

# viewing the tensor in the dimension 0
print("Concatenation of the tensors in the dimensions 0")
# Calling of the torch.cat() function
var_c = torch.cat((var_1_c, var_2_c, var_3_c ), 0)
print("c:\n", var_c)

print("Concatenate the tensors in the -1 dimension")
var_c = torch.cat((var_1_c ,var_2_c ,var_3_c ), -1)
print("c:\n", var_c)</pre>
</div>

Example: Generate two one-dimensional tensors

In this example, the torch will generate two one-dimensional tensors and concatenate via rows.cat()

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#importation of the torch module
import torch
 
# start by creating two tensors initially
var_one_data = torch.tensor([10,20,30,40,50])  
var_two_data = torch.tensor([1,2,3,4,5])

print("Show the real Tensors: ")
print(var_one_data)
print(var_two_data)
 
#Concatenation of the two tensors
print("Tensor concatenation: ",torch.cat((var_one_data,var_two_data)))</pre>
</div>

Since the tensors are of type 1 dimensional, they are concatenated horizontally (row-wise).

Example : Generate four one-dimensional tensors

Here, we’ll use torch.cat() to generate four one-dimensional tensors and concatenate them row-wise.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#start by importing the torch module
import torch
 
 
#create four tensors
var_one_data = torch.tensor([10,20,40,50])  
var_two_data  = torch.tensor([12,45,67,89])  
var_three_data  = torch.tensor([100,32,45,67])
var_four_data  = torch.tensor([120,456,1,1])  
 
#display
print("Actual Tensors: ")
print(var_one_data )
print(var_two_data)
print(var_three_data)
print(var_four_data)
 
#concatenation of two tensors
print("Concatenation of the  Tensor: ",torch.cat((var_one_data,var_two_data,var_three_data,var_four_data)))</pre>
</div>

Since the tensors are of type 1 dimensional, five are concatenated horizontally (row-wise).

Example: Generate five two-dimensional tensors

In this example, we’ll use torch.cat() to generate five two-dimensional tensors and concatenate them in rows.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#begin by importing the torch module
import torch
 
 
#creation of tensors with the two Dimensions each

var_one_data = torch.tensor([[10,20,40,50],[1,2,3,4]])  
var_two_data = torch.tensor([[2,3,4,5],[20,70,89,0]])
var_three_data = torch.tensor([[12,4,5,6],[56,34,56,787]])  
var_four_data = torch.tensor([[100,1,2,3],[67,87,6,78]])
var_five_data = torch.tensor([[120,33,56,78],[45,56,78,6]])  
 
print("The actual tensors: ")
print(var_one_data)
print(var_two_data)
print(var_three_data)
print(var_four_data)
print(var_five_data)
 
#Concatenation of the  tensors via rows
print("Concatenation of the  Tensor: ",torch.cat((var_one_data,var_two_data,var_three_data,var_four_data,var_five_data),dim=1))</pre>
</div>

As we specified dim=1, five tensors are concatenated horizontally (row-wise).

Example: Generate five two-dimensional tensors

In this example, we’ll use torch.cat() to generate five two-dimensional tensors and concatenate them through columns.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># begin by importing the torch module
import torch
 
 
#creation of five tensors with 2 Dimensions each
var_one_data = torch.tensor([[10,20,40,50],[1,2,3,4]])  
var_two_data = torch.tensor([[2,3,4,5],[20,70,89,0]])
var_three_data = torch.tensor([[12,4,5,6],[56,34,56,787]])  
var_four_data = torch.tensor([[100,1,2,3],[67,87,6,78]])
var_five_data = torch.tensor([[120,33,56,78],[45,56,78,6]])  
 
print("Actual Tensors: ")
print(var_one_data)
print(var_two_data)
print(var_three_data)
print(var_four_data)
print(var_five_data)
 
#The Concatenation of the tensors through columns
print("Resultant Tensor concatenated: ",torch.cat((var_one_data,var_two_data,var_three_data,var_four_data,var_five_data),dim=0))
</pre>
</div>

As we supplied dim=0, five tensors are concatenated vertically (column-wise).

Utilize a CPU

We must develop a tensor with a cpu() function if we want to execute a cat() function on the CPU. It will operate on a computer with a CPU. Further, we can now use the cpu() method to create a tensor. The syntax is as follows:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>torch.tensor(data).cpu()</pre>
</div>

Example: Generate two one-dimensional tensors

In this demonstration, two one-dimensional tensors will be generated on the CPU and then concatenated via rows using the torch.cat().

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#starting the import torch module
import torch
 
#creation of two tensors
var_one_data = torch.tensor([10,20,30,40,50]).cpu()  
var_two_data = torch.tensor([1,2,3,4,5]).cpu()  

print("The real Tensors: ")
print(var_one_data)
print(var_two_data)
 
#Concatenation of the two tensors created above
print("Concatenating the Tensor: ",torch.cat((var_one_data,var_two_data)))</pre>
</div>

Since the tensors are type 1, they are concatenated horizontally (row-wise).

Example: use the torch.cat() to concatenate five one-dimensional tensors

In this demonstration, we’ll use the torch.cat() to concatenate five one-dimensional tensors row-wise on the CPU.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#start by importing the torch module
import torch
 
#creation of five new tensors
var_one_data = torch.tensor([10,20,40,50]).cpu()  
var_two_data = torch.tensor([2,3,4,5]).cpu()
var_three_data = torch.tensor([12,45,67,89]).cpu()  
var_four_data = torch.tensor([100,32,45,67]).cpu()  
var_five_data = torch.tensor([120,456,1,1]).cpu()  
 
print("Actual Tensors: ")
print(var_one_data)
print(var_two_data)
print(var_three_data)
print(var_four_data)
print(var_five_data)
 
#concatenation of the two tensors
print("Resultant concatenated Tensor: ",torch.cat((var_one_data,var_two_data,var_three_data,var_four_data,var_five_data)))</pre>
</div>

Since the tensors are of type 1 dimensional, five tensors are concatenated horizontally (row-wise).

Example: use the torch.cat() to concatenate five two-dimensional tensors

In this demonstration, we’ll use the torch.cat() to concatenate five two-dimensional tensors created on the CPU using rows.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#start by importing the torch module
import torch
 
 
#creating five tensors whose individual dimensions are 2
var_one_data = torch.tensor([[10,20,40,50],[1,2,3,4]]).cpu()  
var_two_data = torch.tensor([[2,3,4,5],[20,70,89,0]]).cpu()
var_three_data = torch.tensor([[12,4,5,6],[56,34,56,787]]).cpu()  
var_four_data = torch.tensor([[100,1,2,3],[67,87,6,78]]).cpu()  
var_five_data = torch.tensor([[120,33,56,78],[45,56,78,6]]).cpu()  
 
#display
print("Actual Tensors: ")
print(var_one_data)
print(var_two_data)
print(var_three_data)
print(var_four_data)
print(var_five_data)
 
#using rows to concatenate  tensors
print("Concatenated  Tensor: ",torch.cat((var_one_data,var_two_data,var_three_data,var_four_data,var_five_data),dim=1))
</pre>
</div>

As we specified dim=1, five tensors are concatenated horizontally (row0-wise).

Example: use the CPU to produce five two-dimensional tensors

In this demonstration, we’ll use the CPU to produce five two-dimensional tensors using the torch.cat() to concatenate them via columns.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#start by importing the torch module
import torch
 
 
#creation of five tensors with 2 Dimensions each
var_one_data = torch.tensor([[10,20,40,50],[1,2,3,4]]).cpu()  
var_two_data = torch.tensor([[2,3,4,5],[20,70,89,0]]).cpu()
var_three_data = torch.tensor([[12,4,5,6],[56,34,56,787]]).cpu()  
var_four_data = torch.tensor([[100,1,2,3],[67,87,6,78]]).cpu()  
var_five_data = torch.tensor([[120,33,56,78],[45,56,78,6]]).cpu()  
 
print("The real tensors: ")
print(var_one_data)
print(var_two_data)
print(var_three_data)
print(var_four_data)
print(var_five_data)
 
#Using columns in concatenation of the  tensors
print("Concatenated  Tensor: ",torch.cat((var_one_data,var_two_data,var_three_data,var_four_data,var_five_data),dim=0))
</pre>
</div>

As we supplied dim=0, five tensors are concatenated vertically (column-wise).

Conclusion

We explored using PyTorch’s cat() function to concatenate two or more tensors both horizontally and vertically in this piece. Tensors are concatenated row-wise if dim=1 and column-wise if dim=0, respectively. Additionally, we implemented various examples to concatenate one and two-dimensional tensors in this post, as well as cat() on the CPU using the cpu() function.

A series of tensors in the same dimension can be concatenated using PyTorch’s Cat() function. To prevent empty tensors on non-concatenating dimensions, we must ensure that the tensors used for concatenating have the same shape. Also, note that the cat() function closely resembles the stack() function. Further, tensors are concatenated along an existing axis in the concat() method, but a new axis (that is non-existent for each tensor) is created in the stack() function.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *