DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

hstack(), vstack(), dstack() and column_stack() in PyTorch

*My post explains stack() and cat().

hstack() can concatenate 2 or more tensors in sequence horizontally (column wise) as shown below:

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].
Enter fullscreen mode Exit fullscreen mode

*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 but not from a tensor.

vstack() can concatenate 2 or more tensors in sequence vertically (row wise) as shown below:

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].
Enter fullscreen mode Exit fullscreen mode

*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() are the same because row_stack() is the alias of vstack().
  • vstack() can be called only from torch but not from a tensor.

dstack() can concatenate 2 or more tensors in sequence depthwise (along third axis) as shown below:

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].
Enter fullscreen mode Exit fullscreen mode

*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() can concatenate 2 or more tensors horizontally as shown below:

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].
Enter fullscreen mode Exit fullscreen mode

*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.

Top comments (0)