atleast_1d(), atleast_2d() and atleast_3d() in PyTorch
Tags: pytorch, atleast1d, atleast2d, atleast3d
Published: 2024-04-01 12:22:49

[atleast_1d()](https://pytorch.org/docs/stable/generated/torch.atleast_1d.html) can only make one or more 0D tensors one or more 1D tensors as shown below. *`atleast_1d()` can be called only from [torch](https://pytorch.org/docs/stable/torch.html) but not from a tensor:

```python
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]]]]))
```

[atleast_2d()](https://pytorch.org/docs/stable/generated/torch.atleast_2d.html) 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:

```python
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]]]]))
```

[atleast_3d()](https://pytorch.org/docs/stable/generated/torch.atleast_3d.html) 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:

```python
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]]]]))
```