PyTorch Stack vs Cat Explained for Beginners

Introduction

Concatenation with cat()

As you can see that with cat() function the tensors are concatenated along the existing axis.

Concatenation with stack()

Here using the stack() function, the concatenation happens along a new axis, in this example along axis=0.

In [10]:

torch.stack(
    (t1,t2,t3,t4)
    ,dim=0
)

 

In fact, we can derive the same results of stack() using cat() function as well but with an extra step. We can add a new axis to the tensor and then concatenate them with cat().

  • Palash Sharma

    I am Palash Sharma, an undergraduate student who loves to explore and garner in-depth knowledge in the fields like Artificial Intelligence and Machine Learning. I am captivated by the wonders these fields have produced with their novel implementations. With this, I have a desire to share my knowledge with others in all my capacity.

    View all posts

Follow Us

Leave a Reply

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