hstack(), vstack(), dstack() and column_stack() in PyTorch
Tags: pytorch, catconcatenate, hstack, vstack
Published: 2024-04-01 05:17:57

*[My post](https://dev.to/hyperkai/stack-and-cat-in-pytorch-4903) explains [stack()](https://pytorch.org/docs/stable/generated/torch.stack.html) and [cat()](https://pytorch.org/docs/stable/generated/torch.cat.html).

[hstack()](https://pytorch.org/docs/stable/generated/torch.hstack.html) can concatenate 2 or more tensors in sequence horizontally (column wise) as shown below: 

```python
import torch

tensor1 = torch.tensor(2) # The size is [].
tensor2 = torch.tensor(7) # The size is [].
tensor3 = torch.tensor(4) # The size is [].
torch.hstack((tensor1, tensor2, tensor3))
# tensor([2, 7, 4])
# The size is [3].

tensor1 = torch.tensor([2, 7, 4]) # The size is [3].
tensor2 = torch.tensor([8, 3, 2]) # The size is [3].
tensor3 = torch.tensor([5, 0, 8]) # The size is [3].
torch.hstack((tensor1, tensor2, tensor3))
# tensor([2, 7, 4, 8, 3, 2, 5, 0, 8])
# The size is [9].

tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # The size is [2, 3].
tensor2 = torch.tensor([[5, 0, 8], [3, 6, 1]]) # The size is [2, 3].
tensor3 = torch.tensor([[9, 4, 7], [1, 0, 5]]) # The size is [2, 3].
torch.hstack((tensor1, tensor2, tensor3))
# tensor([[2, 7, 4, 5, 0, 8, 9, 4, 7],
#         [8, 3, 2, 3, 6, 1, 1, 0, 5]])
# The size is [2, 9].

tensor1 = torch.tensor([[[2, 7, 4], [8, 3, 2]],
                        [[5, 0, 8], [3, 6, 1]]])
                       # The size is [2, 2, 3].
tensor2 = torch.tensor([[[9, 4, 7], [1, 0, 5]],
                        [[6, 7, 4], [2, 1, 9]]])
                       # The size is [2, 2, 3].
tensor3 = torch.tensor([[[1, 6, 3], [9, 6, 0]],
                        [[0, 8, 7], [3, 5, 2]]])
                       # The size is [2, 2, 3].
torch.hstack((tensor1, tensor2, tensor3))
# tensor([[[2, 7, 4], [8, 3, 2], [9, 4, 7],
#          [1, 0, 5], [1, 6, 3], [9, 6, 0]],
#         [[5, 0, 8], [3, 6, 1], [6, 7, 4],
#          [2, 1, 9], [0, 8, 7], [3, 5, 2]]])
# The size is [2, 6, 3].
```

*Memos:
- `hstack()` can concatenate 0D or more D tensors.
- With `hstack()`, concatenation is possible with some tensors of different size but it is not possible with some tensors of different size.
- If at least one tensor contains at least one floating-point number, the result is the tensor of floating-point numbers.
- `hstack()` can be called only from [torch](https://pytorch.org/docs/stable/torch.html) but not from a tensor.

[vstack()](https://pytorch.org/docs/stable/generated/torch.vstack.html) can concatenate 2 or more tensors in sequence vertically (row wise) as shown below:

```python
import torch

tensor1 = torch.tensor(2) # The size is [].
tensor2 = torch.tensor(7) # The size is [].
tensor3 = torch.tensor(4) # The size is [].
torch.vstack((tensor1, tensor2, tensor3))
# tensor([[2], 
#         [7], 
#         [4]])
# The size is [3, 1].

tensor1 = torch.tensor([2, 7, 4]) # The size is [3].
tensor2 = torch.tensor([8, 3, 2]) # The size is [3].
tensor3 = torch.tensor([5, 0, 8]) # The size is [3].
torch.vstack((tensor1, tensor2, tensor3))
# tensor([[2, 7, 4],
#         [8, 3, 2], 
#         [5, 0, 8]])
# The size is [3, 3].

tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # The size is [2, 3].
tensor2 = torch.tensor([[5, 0, 8], [3, 6, 1]]) # The size is [2, 3].
tensor3 = torch.tensor([[9, 4, 7], [1, 0, 5]]) # The size is [2, 3].
torch.vstack((tensor1, tensor2, tensor3))
# tensor([[2, 7, 4], 
#         [8, 3, 2], 
#         [5, 0, 8], 
#         [3, 6, 1], 
#         [9, 4, 7], 
#         [1, 0, 5]])
# The size is [6, 3].

tensor1 = torch.tensor([[[2, 7, 4], [8, 3, 2]],
                        [[5, 0, 8], [3, 6, 1]]])
                       # The size is [2, 2, 3].
tensor2 = torch.tensor([[[9, 4, 7], [1, 0, 5]],
                        [[6, 7, 4], [2, 1, 9]]])
                       # The size is [2, 2, 3].
tensor3 = torch.tensor([[[1, 6, 3], [9, 6, 0]],
                        [[0, 8, 7], [3, 5, 2]]])
                       # The size is [2, 2, 3].
torch.vstack((tensor1, tensor2, tensor3))
# tensor([[[2, 7, 4], [8, 3, 2]],
#         [[5, 0, 8], [3, 6, 1]],
#         [[9, 4, 7], [1, 0, 5]],
#         [[6, 7, 4], [2, 1, 9]],
#         [[1, 6, 3], [9, 6, 0]],
#         [[0, 8, 7], [3, 5, 2]]])
# The size is [6, 2, 3].
```

*Memos:
- `vstack()` can concatenate 0D or more D tensors.
- With `vstack()`, concatenation is possible with some tensors of different size but it is not possible with some tensors of different size.
- If at least one tensor contains at least one floating-point number, the result is the tensor of floating-point numbers.
- `vstack()` and [row_stack()](https://pytorch.org/docs/stable/generated/torch.row_stack.html) are the same because `row_stack()` is the alias of `vstack()`.
- `vstack()` can be called only from `torch` but not from a tensor.

[dstack()](https://pytorch.org/docs/stable/generated/torch.dstack.html) can concatenate 2 or more tensors in sequence depthwise (along third axis) as shown below: 

```python
import torch

tensor1 = torch.tensor(2) # The size is [].
tensor2 = torch.tensor(7) # The size is [].
tensor3 = torch.tensor(4) # The size is [].
torch.dstack((tensor1, tensor2, tensor3))
# tensor([[[2, 7, 4]]])
# The size is [1, 1, 3].

tensor1 = torch.tensor([2, 7, 4]) # The size is [3].
tensor2 = torch.tensor([8, 3, 2]) # The size is [3].
tensor3 = torch.tensor([5, 0, 8]) # The size is [3].
torch.dstack((tensor1, tensor2, tensor3))
# tensor([[[2, 8, 5], [7, 3, 0], [4, 2, 8]]])
# The size is [1, 3, 3].

tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # The size is [2, 3].
tensor2 = torch.tensor([[5, 0, 8], [3, 6, 1]]) # The size is [2, 3].
tensor3 = torch.tensor([[9, 4, 7], [1, 0, 5]]) # The size is [2, 3].
torch.dstack((tensor1, tensor2, tensor3))
# tensor([[[2, 5, 9], [7, 0, 4], [4, 8, 7]],
#         [[8, 3, 1], [3, 6, 0], [2, 1, 5]]])
# The size is [2, 3, 3].

tensor1 = torch.tensor([[[2, 7, 4], [8, 3, 2]],
                        [[5, 0, 8], [3, 6, 1]]])
                       # The size is [2, 2, 3].
tensor2 = torch.tensor([[[9, 4, 7], [1, 0, 5]],
                        [[6, 7, 4], [2, 1, 9]]])
                       # The size is [2, 2, 3].
tensor3 = torch.tensor([[[1, 6, 3], [9, 6, 0]],
                        [[0, 8, 7], [3, 5, 2]]])
                       # The size is [2, 2, 3].
torch.dstack((tensor1, tensor2, tensor3))
# tensor([[[2, 7, 4, 9, 4, 7, 1, 6, 3],
#          [8, 3, 2, 1, 0, 5, 9, 6, 0]],
#         [[5, 0, 8, 6, 7, 4, 0, 8, 7],
#          [3, 6, 1, 2, 1, 9, 3, 5, 2]]])
# The size is [2, 2, 9].
```

*Memos:
- `dstack()` can concatenate 0D or more D tensors.
- The size of tensors must be the same.
- If at least one tensor contains at least one floating-point number, the result is the tensor of floating-point numbers.
- `dstack()` can be called only from `torch` but not from a tensor.

[column_stack()](https://pytorch.org/docs/stable/generated/torch.column_stack.html) can concatenate 2 or more tensors horizontally as shown below: 

```python
import torch

tensor1 = torch.tensor(2) # The size is [].
tensor2 = torch.tensor(7) # The size is [].
tensor3 = torch.tensor(4) # The size is [].
torch.column_stack((tensor1, tensor2, tensor3))
# tensor([2, 7, 4])
# The size is [3].

tensor1 = torch.tensor([2, 7, 4]) # The size is [3].
tensor2 = torch.tensor([8, 3, 2]) # The size is [3].
tensor3 = torch.tensor([5, 0, 8]) # The size is [3].
torch.column_stack((tensor1, tensor2, tensor3))
# tensor([[2, 8, 5], [7, 3, 0], [4, 2, 8]])
# The size is [3, 3].

tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # The size is [2, 3].
tensor2 = torch.tensor([[5, 0, 8], [3, 6, 1]]) # The size is [2, 3].
tensor3 = torch.tensor([[9, 4, 7], [1, 0, 5]]) # The size is [2, 3].
torch.column_stack((tensor1, tensor2, tensor3))
# tensor([[2, 7, 4, 5, 0, 8, 9, 4, 7],
#         [8, 3, 2, 3, 6, 1, 1, 0, 5]])
# The size is [2, 9].

tensor1 = torch.tensor([[[2, 7, 4], [8, 3, 2]],
                        [[5, 0, 8], [3, 6, 1]]])
                       # The size is [2, 2, 3].
tensor2 = torch.tensor([[[9, 4, 7], [1, 0, 5]],
                        [[6, 7, 4], [2, 1, 9]]])
                       # The size is [2, 2, 3].
tensor3 = torch.tensor([[[1, 6, 3], [9, 6, 0]],
                        [[0, 8, 7], [3, 5, 2]]])
                       # The size is [2, 2, 3].
torch.column_stack((tensor1, tensor2, tensor3))
# tensor([[[2, 7, 4],
#          [8, 3, 2],
#          [9, 4, 7],
#          [1, 0, 5],
#          [1, 6, 3],
#          [9, 6, 0]],
#         [[5, 0, 8],
#          [3, 6, 1],
#          [6, 7, 4],
#          [2, 1, 9],
#          [0, 8, 7],
#          [3, 5, 2]]])
# The size is [2, 6, 3].
```

*Memos:
- `column_stack()` can concatenate 0D or more D tensors.
- The size of tensors must be the same.
- If at least one tensor contains at least one floating-point number, the result is the tensor of floating-point numbers.
- `column_stack()` can be called only from `torch` but not from a tensor.