DEV Community

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

Posted on • Updated on

atleast_1d(), atleast_2d() and atleast_3d() in PyTorch

atleast_1d() can only make one or more 0D tensors one or more 1D tensors as shown below. *atleast_1d() can be called only from torch but not from a tensor:

import torch

tensor0 = torch.tensor(2) # 0D tensor
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor
tensor3 = torch.tensor([[[2, 7, 4], [8, 3, 2]], # 3D tensor
                        [[5, 0, 8], [3, 6, 1]]])
tensor4 = torch.tensor([[[[2, 7, 4], [8, 3, 2]], # 4D tensor
                         [[5, 0, 8], [3, 6, 1]]],
                        [[[9, 4, 7], [1, 0, 5]],
                         [[6, 7, 4], [2, 1, 9]]]])
torch.atleast_1d(tensor0, tensor1, tensor2, tensor3, tensor4)
torch.atleast_1d((tensor0, tensor1, tensor2, tensor3, tensor4))
# (tensor([2]),
#  tensor([2, 7, 4]),
#  tensor([[2, 7, 4], [8, 3, 2]]),
#  tensor([[[2, 7, 4], [8, 3, 2]],
#          [[5, 0, 8], [3, 6, 1]]]),
#  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]]]]))
Enter fullscreen mode Exit fullscreen mode

atleast_2d() can only make one or more 0D or 1D tensors one or more 2D tensors as shown below. *atleast_2d() can be called only from torch but not from a tensor:

import torch

tensor0 = torch.tensor(2) # 0D tensor
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor
tensor3 = torch.tensor([[[2, 7, 4], [8, 3, 2]], # 3D tensor
                        [[5, 0, 8], [3, 6, 1]]])
tensor4 = torch.tensor([[[[2, 7, 4], [8, 3, 2]], # 4D tensor
                         [[5, 0, 8], [3, 6, 1]]],
                        [[[9, 4, 7], [1, 0, 5]],
                         [[6, 7, 4], [2, 1, 9]]]])
torch.atleast_2d(tensor0, tensor1, tensor2, tensor3, tensor4)
torch.atleast_2d((tensor0, tensor1, tensor2, tensor3, tensor4))
# (tensor([[2]]),
#  tensor([[2, 7, 4]]),
#  tensor([[2, 7, 4], [8, 3, 2]]),
#  tensor([[[2, 7, 4], [8, 3, 2]],
#          [[5, 0, 8], [3, 6, 1]]]),
#  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]]]]))
Enter fullscreen mode Exit fullscreen mode

atleast_3d() can only make one or more 0D, 1D or 2D tensors one or more 3D tensors as shown below. *atleast_3d() can be called only from torch but not from a tensor:

import torch

tensor0 = torch.tensor(2) # 0D tensor
tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor
tensor3 = torch.tensor([[[2, 7, 4], [8, 3, 2]], # 3D tensor
                        [[5, 0, 8], [3, 6, 1]]])
tensor4 = torch.tensor([[[[2, 7, 4], [8, 3, 2]], # 4D tensor
                         [[5, 0, 8], [3, 6, 1]]],
                        [[[9, 4, 7], [1, 0, 5]],
                         [[6, 7, 4], [2, 1, 9]]]])
torch.atleast_3d(tensor0, tensor1, tensor2, tensor3, tensor4)
torch.atleast_3d((tensor0, tensor1, tensor2, tensor3, tensor4))
# (tensor([[[2]]]),
#  tensor([[[2], [7], [4]]]),
#  tensor([[[2], [7], [4]],
#          [[8], [3], [2]]]),
#  tensor([[[2, 7, 4], [8, 3, 2]],
#          [[5, 0, 8], [3, 6, 1]]]),
#  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]]]]))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)