pytorch

PyTorch – cat()

PyTorch is an open-source framework for the Python programming language.

A tensor is a multidimensional array that is used to store data. So to use a tensor, we have to import the torch module.

To create a tensor the method used is tensor().

Syntax:

torch.tensor(data)

Where data is a multi-dimensional array.

torch.cat()

To concatenate two or more tensors by a row or column, use torch.cat().

Syntax:

torch.cat((tensor_object1,tensor_object2,………..),dim)

Parameters:

  1. It takes two or more tensors as its first parameter.
  2. If dim=0, the tensors are concatenated column-wise. If dim=1, the tensors are concatenated row-wise.

Example 1:

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

#import torch module
import torch
 
#create 2 tensors
data1 = torch.tensor([10,20,30,40,50])  
data2 = torch.tensor([1,2,3,4,5])
#display
print("Actual Tensors: ")
print(data1)
print(data2)
 
#Concatenate two tensors
print("Concatenated Tensor: ",torch.cat((data1,data2)))

Output:

Actual Tensors:
tensor([10, 20, 30, 40, 50])
tensor([1, 2, 3, 4, 5])
Concatenated Tensor:  tensor([10, 20, 30, 40, 50,  1,  2,  3,  4,  5])

Two tensors are concatenated horizontally (row-wise) since the tensors are of type 1 dimensional.

Example 2:

In this example, we will create five one-dimensional tensors and concatenate them row-wise using torch.cat().

#import torch module
import torch
 
 
#create 5 tensors
data1 = torch.tensor([10,20,40,50])  
data2 = torch.tensor([2,3,4,5])
data3 = torch.tensor([12,45,67,89])  
data4 = torch.tensor([100,32,45,67])
data5 = torch.tensor([120,456,1,1])  
 
#display
print("Actual Tensors: ")
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
 
#concatenate two tensors
print("Concatenated Tensor: ",torch.cat((data1,data2,data3,data4,data5)))

Output:

Actual Tensors:
tensor([10, 20, 40, 50])
tensor([2, 3, 4, 5])
tensor([12, 45, 67, 89])
tensor([100,  32,  45,  67])
tensor([120, 456,   1,   1])
Concatenated Tensor:  tensor([ 10,  20,  40,  50,   2,   3,   4,   5,  12,  45,  67,  89, 100,  32,
         45,  67, 120, 456,   1,   1])

Five tensors are concatenated horizontally (row-wise) since the tensors are of type 1 dimensional..

Example 3:

In this example, we will create five two-dimensional tensors and concatenate them via rows using torch.cat().

#import torch module
import torch
 
 
#create 5 tensors with 2 Dimensions each
data1 = torch.tensor([[10,20,40,50],[1,2,3,4]])  
data2 = torch.tensor([[2,3,4,5],[20,70,89,0]])
data3 = torch.tensor([[12,4,5,6],[56,34,56,787]])  
data4 = torch.tensor([[100,1,2,3],[67,87,6,78]])
data5 = torch.tensor([[120,33,56,78],[45,56,78,6]])  
 
#display
print("Actual Tensors: ")
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
 
#Concatenate  tensors via rows
print("Concatenated  Tensor: ",torch.cat((data1,data2,data3,data4,data5),dim=1))

Output:

Actual Tensors:
tensor([[10, 20, 40, 50],
        [ 1,  2,  3,  4]])
tensor([[ 2,  3,  4,  5],
        [20, 70, 89,  0]])
tensor([[ 12,   4,   5,   6],
        [ 56,  34,  56, 787]])
tensor([[100,   1,   2,   3],
        [ 67,  87,   6,  78]])
tensor([[120,  33,  56,  78],
        [ 45,  56,  78,   6]])
Concatenated  Tensor:  tensor([[ 10,  20,  40,  50,   2,   3,   4,   5,  12,   4,   5,   6, 100,   1,
           2,   3, 120,  33,  56,  78],
        [  1,   2,   3,   4,  20,  70,  89,   0,  56,  34,  56, 787,  67,  87,
           6,  78,  45,  56,  78,   6]])

Five tensors are concatenated horizontally (row-wise) as we specified dim=1.

Example 4:

In this example, we will create five two-dimensional tensors and concatenate them via columns using torch.cat().

#import torch module
import torch
 
 
#create 5 tensors with 2 Dimensions each
data1 = torch.tensor([[10,20,40,50],[1,2,3,4]])  
data2 = torch.tensor([[2,3,4,5],[20,70,89,0]])
data3 = torch.tensor([[12,4,5,6],[56,34,56,787]])  
data4 = torch.tensor([[100,1,2,3],[67,87,6,78]])
data5 = torch.tensor([[120,33,56,78],[45,56,78,6]])  
 
#display
print("Actual Tensors: ")
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
 
#Concatenate  tensors via columns
print("Concatenated  Tensor: ",torch.cat((data1,data2,data3,data4,data5),dim=0))

Output:

Actual Tensors:
tensor([[10, 20, 40, 50],
        [ 1,  2,  3,  4]])
tensor([[ 2,  3,  4,  5],
        [20, 70, 89,  0]])
tensor([[ 12,   4,   5,   6],
        [ 56,  34,  56, 787]])
tensor([[100,   1,   2,   3],
        [ 67,  87,   6,  78]])
tensor([[120,  33,  56,  78],
        [ 45,  56,  78,   6]])
Concatenated  Tensor:  tensor([[ 10,  20,  40,  50],
        [  1,   2,   3,   4],
        [  2,   3,   4,   5],
        [ 20,  70,  89,   0],
        [ 12,   4,   5,   6],
        [ 56,  34,  56, 787],
        [100,   1,   2,   3],
        [ 67,  87,   6,  78],
        [120,  33,  56,  78],
        [ 45,  56,  78,   6]])

Five tensors are concatenated vertically (column-wise) as we specified dim=0.

Work with CPU

If you want to run a cat() function on the CPU, then we have to create a tensor with a cpu() function. This will run on a CPU machine.

At this time, when we are creating a tensor, we can use the cpu() function.

Syntax:
torch.tensor(data).cpu()

Example 1:

In this example, we will create two one-dimensional tensors on the CPU and concatenate them via rows using torch.cat().

#import torch module
import torch
 
#create 2 tensors
data1 = torch.tensor([10,20,30,40,50]).cpu()  
data2 = torch.tensor([1,2,3,4,5]).cpu()  
#display
print("Actual Tensors: ")
print(data1)
print(data2)
 
#Concatenate two tensors
print("Concatenated Tensor: ",torch.cat((data1,data2)))

Output:

Actual Tensors:
tensor([10, 20, 30, 40, 50])
tensor([1, 2, 3, 4, 5])
Concatenated Tensor:  tensor([10, 20, 30, 40, 50,  1,  2,  3,  4,  5])

Two tensors are concatenated horizontally (row-wise) since the tensors are of type 1 dimensional.

Example 2:

In this example, we will create five one-dimensional tensors on the CPU and concatenate row-wise using torch.cat().

#import torch module
import torch
 
#create 5 tensors
data1 = torch.tensor([10,20,40,50]).cpu()  
data2 = torch.tensor([2,3,4,5]).cpu()
data3 = torch.tensor([12,45,67,89]).cpu()  
data4 = torch.tensor([100,32,45,67]).cpu()  
data5 = torch.tensor([120,456,1,1]).cpu()  
 
#display
print("Actual Tensors: ")
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
 
#concatenate two tensors
print("Concatenated Tensor: ",torch.cat((data1,data2,data3,data4,data5)))

Output:

Actual Tensors:
tensor([10, 20, 40, 50])
tensor([2, 3, 4, 5])
tensor([12, 45, 67, 89])
tensor([100,  32,  45,  67])
tensor([120, 456,   1,   1])
Concatenated Tensor:  tensor([ 10,  20,  40,  50,   2,   3,   4,   5,  12,  45,  67,  89, 100,  32,
         45,  67, 120, 456,   1,   1])

Five tensors are concatenated horizontally (row-wise) since the tensors are of type 1 dimensional.

Example 3:

In this example, we will create five two-dimensional tensors on the CPU and concatenate them via rows using torch.cat().

#import torch module
import torch
 
 
#create 5 tensors with 2 Dimensions each
data1 = torch.tensor([[10,20,40,50],[1,2,3,4]]).cpu()  
data2 = torch.tensor([[2,3,4,5],[20,70,89,0]]).cpu()
data3 = torch.tensor([[12,4,5,6],[56,34,56,787]]).cpu()  
data4 = torch.tensor([[100,1,2,3],[67,87,6,78]]).cpu()  
data5 = torch.tensor([[120,33,56,78],[45,56,78,6]]).cpu()  
 
#display
print("Actual Tensors: ")
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
 
#Concatenate  tensors via rows
print("Concatenated  Tensor: ",torch.cat((data1,data2,data3,data4,data5),dim=1))

Output:

Actual Tensors:
tensor([[10, 20, 40, 50],
        [ 1,  2,  3,  4]])
tensor([[ 2,  3,  4,  5],
        [20, 70, 89,  0]])
tensor([[ 12,   4,   5,   6],
        [ 56,  34,  56, 787]])
tensor([[100,   1,   2,   3],
        [ 67,  87,   6,  78]])
tensor([[120,  33,  56,  78],
        [ 45,  56,  78,   6]])
Concatenated  Tensor:  tensor([[ 10,  20,  40,  50,   2,   3,   4,   5,  12,   4,   5,   6, 100,   1,
           2,   3, 120,  33,  56,  78],
        [  1,   2,   3,   4,  20,  70,  89,   0,  56,  34,  56, 787,  67,  87,
           6,  78,  45,  56,  78,   6]])

Five tensors are concatenated horizontally (row0-wise) as we specified dim=1.

Example 4:

In this example, we will create five two-dimensional tensors on the CPU and concatenate them via columns using torch.cat().

#import torch module
import torch
 
 
#create 5 tensors with 2 Dimensions each
data1 = torch.tensor([[10,20,40,50],[1,2,3,4]]).cpu()  
data2 = torch.tensor([[2,3,4,5],[20,70,89,0]]).cpu()
data3 = torch.tensor([[12,4,5,6],[56,34,56,787]]).cpu()  
data4 = torch.tensor([[100,1,2,3],[67,87,6,78]]).cpu()  
data5 = torch.tensor([[120,33,56,78],[45,56,78,6]]).cpu()  
 
#display
print("Actual Tensors: ")
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
 
#Concatenate  tensors via columns
print("Concatenated  Tensor: ",torch.cat((data1,data2,data3,data4,data5),dim=0))

Output:

Actual Tensors:
tensor([[10, 20, 40, 50],
        [ 1,  2,  3,  4]])
tensor([[ 2,  3,  4,  5],
        [20, 70, 89,  0]])
tensor([[ 12,   4,   5,   6],
        [ 56,  34,  56, 787]])
tensor([[100,   1,   2,   3],
        [ 67,  87,   6,  78]])
tensor([[120,  33,  56,  78],
        [ 45,  56,  78,   6]])
Concatenated  Tensor:  tensor([[ 10,  20,  40,  50],
        [  1,   2,   3,   4],
        [  2,   3,   4,   5],
        [ 20,  70,  89,   0],
        [ 12,   4,   5,   6],
        [ 56,  34,  56, 787],
        [100,   1,   2,   3],
        [ 67,  87,   6,  78],
        [120,  33,  56,  78],
        [ 45,  56,  78,   6]])

Five tensors are concatenated vertically (column-wise) as we specified dim=0.

Conclusion

We saw how to concatenate two or more tensors horizontally & vertically in PyTorch using the cat() function. If dim=0, the tensors are concatenated column-wise; if dim=1, the tensors are concatenated row-wise. In this article, we implemented several examples to concatenate one and two-dimensional tensors and also implemented cat() on the CPU using the cpu() function.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain