pytorch

How to Join Multiple Tensors in PyTorch?

PyTorch is a deep-learning library that offers various operations for manipulating tensors. Sometimes, users may need to join multiple tensors with one another to create a new tensor. PyTorch provides various methods to join/concatenate tensors along a specified dimension.

This article will exemplify different methods to join multiple tensors in PyTorch.

Method 1: How to Join Multiple Tensors Using “torch.cat()” Function?

The “torch.cat()” function is used to join/concatenate the provided sequence of tensors along the specified dimension. The tensors must have the same shape except in the concatenation dimension. To join multiple tensors using the “torch.cat()” function, check out the provided examples for a better understanding:

Example 1: Join 1D Tensors With “torch.cat()” Function
In the first example, we will create 1D tensors and join them using the “torch.cat()” function. Let’s explore the instructions below.

First, import the “torch” library:

import torch

Then, create the desired 1D tensors. Here, we are creating three simple 1D tensors “Tens1”, “Tens2”, and “Tens3” from the list using the “torch.Tensor()” function:

Tens1 = torch.Tensor([2,4,6,8])
Tens2 = torch.Tensor([1,3,5,7])
Tens3 = torch.Tensor([1,2,3,4])

Next, display the elements of the above-created tensors:

print("Tensor1:", Tens1)
print("Tensor2:", Tens2)
print("Tensor3:", Tens3)

It can be seen that the tensors have been created successfully:

To join the above-created tensors, use the “torch.cat()” method:

T = torch.cat((Tens1,Tens2,Tens3))

print(T)

According to the below output, the tensors have been joined successfully:

Example 2: Join 2D Tensors With “torch.cat()” Function
In the second example, we will create 2D tensors and join them via the “torch.cat()” function.

First, create the desired 2D tensors. Here, we are creating three 2D tensors “Tens1”, “Tens2”, and “Tens3” using the “torch.Tensor()” function:

Tens1 = torch.Tensor([[1,2],[3,4]])
Tens2 = torch.Tensor([[2,4],[6,8]])
Tens3 = torch.Tensor([[1,3],[5,7]])

Then, display the elements of the above-created tensors:

print("Tensor1: \n", Tens1)
print("Tensor2: \n", Tens2)
print("Tensor3: \n", Tens3)

The below output displays 2D tensors:

Now, use the “torch.cat()” method to join the above-created 2D tensors:

T = torch.cat((Tens1,Tens2,Tens3))

print(T)

The below output indicates that the 2D tensors have been joined successfully:

Moreover, users can also join 2D tensors in a specific dimension. Here, we are joining tensors in the “-1” dimension:

T = torch.cat((Tens1,Tens2,Tens3), -1)

print(T)

It can be seen that the tensors have been joined in the specified dimension:

Method 2: How to Join Multiple Tensors Using “torch.stack()” Function?

The “torch.stack()” function is utilized to stack two or more than two tensors by a new dimension. The tensors must have the same type and shape. To join multiple tensors using the “torch.stack()” function, look at the below examples.

Example 1: Join 1D Tensors With “torch.stack()” Function
In the first example, we will create 1D tensors and join them using the “torch.stack()” function. Let’s explore the instructions below.

First, import the “torch” library:

import torch

Then, use the “torch.Tensor()” function to create the desired 1D tensors. Here, we are creating three simple 1D tensors “Tens1”, “Tens2”, and “Tens3” from the list:

Tens1 = torch.Tensor([2,4,6,8])
Tens2 = torch.Tensor([1,3,5,7])
Tens3 = torch.Tensor([1,2,3,4])

After that, display the elements of the above-created tensors:

print("Tensor1:", Tens1)
print("Tensor2:", Tens2)
print("Tensor3:", Tens3)

In the below screenshot, three 1D tensors can be seen:

To join the above-created tensors, use the “torch.stack()” method:

T = torch.stack((Tens1,Tens2,Tens3))

print(T)

The below output indicates that the tensors have been joined:

To join tensors in a particular dimension, users need to specify the dimension. Here, we are joining tensors in the “-1” dimension:

T = torch.stack((Tens1,Tens2,Tens3), -1)

print(T)

This has joined the tensors in the specified dimension i.e., “-1”:

Example 2: Join 2D Tensors With “torch.stack()” Function
In the second example, we will create 2D tensors and join them via the “torch.stack()” function. Let’s follow the instructions below.

First, create the desired 2D tensors. Here, we are creating three 2D tensors “Tens1”, “Tens2”, and “Tens3” using the “torch.Tensor()” function:

Tens1 = torch.Tensor([[1,2],[3,4]])
Tens2 = torch.Tensor([[2,4],[6,8]])
Tens3 = torch.Tensor([[1,3],[5,7]])

Then, display the elements of the above-created tensors:

print("Tensor1: \n", Tens1)
print("Tensor2: \n", Tens2)
print("Tensor3: \n", Tens3)

In the below output, three 2D tensors can be seen:

Now, utilize the “torch.stack()” method to join the above-created 2D tensors:

T = torch.stack((Tens1,Tens2,Tens3))

print(T)

It can be observed that the tensors have been joined successfully:

Furthermore, users can also join 2D tensors in a specific dimension. For instance, we are joining tensors in the “-1” dimension:

T = torch.stack((Tens1,Tens2,Tens3), -1)

print(T)

Subsequently, the tensors have been joined in the specified dimension:

That is all about joining multiple tensors in PyTorch.

Note: You can access our Google Colab Notebook at this link.

Conclusion

To join multiple tensors in PyTorch, first, import the “torch” library and create the desired multiple tensors. After that, use the “torch.cat()” or “torch.stack()” method to join created 1D or 2D tensors and display the results. This article has exemplified the methods to join multiple tensors in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.