PyTorch Stack vs Cat Explained for Beginners

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().

LEAVE A REPLY

Please enter your comment!
Please enter your name here