How to concatenate using PyTorch [Examples]


Author: Bashir Alam
Reviewer: Deepak Prasad

Diving into the world of PyTorch, you quickly realize that bringing different pieces of data together, much like fitting puzzle pieces side by side, is a crucial skill. This is where the concept of tensor concatenation comes into play. It's essentially about joining various tensors along a certain dimension, a bit like how you might zip different sections of a tent together, making sure each one aligns perfectly for the structure to hold up. This isn't just about making sure your data looks neat and tidy; it's fundamental for ensuring everything lines up correctly, whether you're batching up data for processing or piecing together the elements of a model. And when it comes to pulling off this feat, the function is your go-to tool.


Example 1: Concatenating Vectors

Suppose you have two 1-dimensional tensors (vectors) and you want to concatenate them.

import torch

# Create two vectors
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])

# Concatenate along dimension 0
result =, b), dim=0)


tensor([1, 2, 3, 4, 5, 6])


Example 2: Concatenating 2D Tensors (Matrices)

If you have two 2-dimensional tensors (matrices) and you want to concatenate them along different dimensions:

# Create two matrices
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[5, 6], [7, 8]])

# Concatenate along dimension 0 (stack rows)
result_dim0 =, y), dim=0)
print("Concatenated along dimension 0:")

# Concatenate along dimension 1 (stack columns)
result_dim1 =, y), dim=1)
print("\nConcatenated along dimension 1:")


Concatenated along dimension 0:
tensor([[1, 2],
[3, 4],
[5, 6],
[7, 8]])

Concatenated along dimension 1:
tensor([[1, 2, 5, 6],
[3, 4, 7, 8]])


Example 3: Concatenating 3D Tensors

For tensors with three dimensions, you can choose any of the three dimensions to concatenate:

# Create two 3D tensors
u = torch.randn(2, 2, 3)  # Random numbers in a 2x2x3 tensor
v = torch.randn(2, 2, 3)  # Random numbers in a 2x2x3 tensor

# Concatenate along the third dimension
result_dim2 =, v), dim=2)


torch.Size([2, 2, 6])

In this example, concatenating along the third dimension (dim=2) results in each element of the 2x2 matrix being a vector of length 6, as each corresponding 3-length vector from u and v is concatenated.



In this tutorial we covered the concept of tensor concatenation in PyTorch using function using different examples. Starting with the simplest form of concatenating vectors, we moved to more complex structures like 2D and 3D tensors. Concatenation can be performed along any dimension of the tensors involved, provided the shapes are compatible along the other dimensions. This operation is not just limited to stacking tensors in various configurations but also crucial for the proper alignment and efficient processing of data within deep learning models.


Views: 66
Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment