empty(), empty_like() and empty_strided in PyTorch
Tags: pytorch, empty, emptylike, emptystrided
Published: 2024-04-08 21:35:49

[empty()](https://pytorch.org/docs/stable/generated/torch.empty.html) can create a 1D or more D tensor filled with the zero or more floating-point numbers(Default), integers, complex numbers or boolean values in memory which are called **uninitialized data** as shown below:

*Memos:
- `empty()` can be used only from [torch](https://pytorch.org/docs/stable/torch.html) but not from a tensor.
- The default type is `float32`.
- You can use [torch.Tensor()](https://pytorch.org/docs/stable/tensors.html) or [torch.FloatTensor()](https://pytorch.org/docs/stable/tensors.html) like `torch.Tensor(3, 2, 4)` or `torch.FloatTensor(3, 2, 4)` because they can do the same job as `empty()`. *`torch.Tensor()` is the alias of `torch.FloatTensor()` by default.

```python
import torch

torch.empty(0)
torch.empty((0,))
# tensor([])

torch.empty(3)
torch.empty((3,))
# tensor([-1.3610e+13, 4.4916e-41, -1.3610e+13])

torch.empty(3, 2)
torch.empty((3, 2))
# tensor([[-1.3610e+13, 4.4916e-41],
#         [5.7850e-23, 3.1100e-41],
#         [4.4842e-44, 0.0000e+00]])

torch.empty(3, 2, 4)
torch.empty((3, 2, 4))
# tensor([[[3.8848e-23, 3.1100e-41, 0.0000e+00, 0.0000e+00],
#          [3.3892e-23, 3.1100e-41, 3.0224e-26, 3.1100e-41]],
#         [[-6.0464e-34, 4.4914e-41, 0.0000e+00, 0.0000e+00],
#          [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
#         [[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
#          [0.0000e+00, 0.0000e+00, 1.4013e-45, 0.0000e+00]]])

torch.empty((3, 2, 4), dtype=torch.int64)
# tensor([[[136263006428688, 96270204571280, 1, 96270203986320],
#          [0, 0, 96270208839376, 96270118417696]],
#         [[136257315028352, 0, 0, 0], 
#          [0, 0, 0, 1]],
#         [[0, 0, 0, 0],
#          [0, 1, 352951805673479, 2542620672001]]])

torch.empty((3, 2, 4), dtype=torch.complex64)
# tensor([[[1.4167e-07+4.4458e-41j, 1.4167e-07+4.4458e-41j,
#           4.4842e-44+0.0000e+00j, 1.5695e-43+0.0000e+00j],
#          [-1.4883e+19+3.1404e-41j, 0.0000e+00+0.0000e+00j,
#           1.4013e-45+0.0000e+00j, -4.9888e-15+3.1409e-41j]],
#         [[-2.4481e+37+4.4456e-41j, -4.9888e-15+3.1409e-41j,
#           9.1477e-41+0.0000e+00j, 8.9683e-44+0.0000e+00j],
#          [3.5873e-43+0.0000e+00j, -2.6273e+37+4.4456e-41j,
#           0.0000e+00+0.0000e+00j, 0.0000e+00+0.0000e+00j]],
#         [[0.0000e+00+0.0000e+00j, 2.4803e-43+0.0000e+00j,
#           -4.6535e-15+3.1409e-41j, -3.2145e-15+3.1409e-41j],
#          [0.0000e+00+0.0000e+00j,  1.4013e-45+0.0000e+00j,
#           -1.7014e+38+1.1515e-40j,  4.5919e-41+8.2957e-43j]]])

torch.empty((3, 2, 4), dtype=torch.bool)
# tensor([[[True, True, True, True],
#          [True, False, False, False]],
#         [[True, True, True, True],
#          [True, True, False, False]],
#         [[False, True, False, False],
#          [False, False, False, False]]])
```

[empty_like()](https://pytorch.org/docs/stable/generated/torch.empty_like.html) can replace the zero or more numbers of 0D or more D tensor with zero or more floating-point numbers, integers, complex numbers or boolean values in memory which are called **uninitialized data** as shown below. *`empty_like()` can be used only from `torch` but not from a tensor:

```python
import torch

my_tensor = torch.tensor(7.)
torch.empty_like(my_tensor)
# tensor(-1.3610e+13)

my_tensor = torch.tensor([7., 4., 5.])
torch.empty_like(my_tensor)
# tensor([2.8244e+23, 4.4787e-41, -5.7316e-07])

my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])
torch.empty_like(my_tensor)
# tensor([[-4.7415e-07, 3.1221e-41, -6.4098e-07],
#         [3.1221e-41, 1.1210e-43, 0.0000e+00]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.empty_like(my_tensor)
# tensor([[[-6.6094e-07, 3.1221e-41, -3.9661e-07],
#          [3.1221e-41, 8.9683e-44, 0.0000e+00]],
#         [[1.1210e-43, 0.0000e+00, -8.9451e+02],
#          [3.1228e-41, 1.7282e-04, 1.2471e+16]]])

my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
                          [[6, 0, 1], [5, 9, 4]]])
torch.empty_like(my_tensor)
# tensor([[[137273168313840, 95694909291296, 1],
#          [95694912519088, 95694842532640, 0]],
#         [[95694862074384, 95694820258896, 137269160918960],
#          [0, 0, 0]]])

my_tensor = torch.tensor([[[7+4j, 4+2j, 5+3j], [2+5j, 8+1j, 3+9j]],
                          [[6+9j, 0+3j, 1+8j], [5+3j, 9+4j, 4+6j]]])
torch.empty_like(my_tensor)
# tensor([[[6.7127e-07+1.7183e-04j,
#           1.6519e-04+1.0187e-11j,
#           2.0661e+20+6.8629e-07j],
#          [1.8077e-43+0.0000e+00j,
#           -4.3084e-07+3.1221e-41j,
#           -3.8936e-07+3.1221e-41j]],
#         [[4.4842e-44+0.0000e+00j,
#           4.4842e-44+0.0000e+00j,
#           -8.7266e+02+3.1228e-41j],
#          [2.8026e-45+0.0000e+00j,
#           4.2039e-45+0.0000e+00j,
#           9.1084e-44+0.0000e+00j]]])

my_tensor = torch.tensor([[[True, False, True], [False, True, False]], 
                          [[True, False, True], [False, True, False]]])
torch.empty_like(my_tensor)
# tensor([[[True, True, True],
#          [True, True, False]],
#         [[False, False, True],
#          [True, True, True]]])
```

[empty_strided()](https://pytorch.org/docs/stable/generated/torch.empty_strided.html) can create the 1D or more D tensor which has `stride` and is filled with the zero or more floating-point numbers(Default), integers, complex numbers or boolean values in memory which are called **uninitialized data** as shown below:

*Memos:
- `empty_strided()` can be used only from `torch` but not from a tensor.
- The default type is `float32`.
- The 1st argument is `size` which must be a tuple or list.
- The 2nd argument is `stride` which must be a tuple or list.
- The number of `size` and `stride` must be the same.

```python
import torch

torch.empty_strided((0,), (1,))
# tensor([])

torch.empty_strided((3,), (1,))
# tensor([1.4167e-07, 4.4458e-41, -5.4069e-15])

torch.empty_strided((3, 2), (1, 2))
# tensor([[-5.5753e-15, -8.1744e-18],
#         [3.1409e-41, 3.1409e-41],
#         [-8.1744e-18, 1.1210e-43]])

torch.empty_strided((3, 2, 4), (1, 2, 3))
# tensor([[[2.8245e+23, 4.4787e-41, 0.0000e+00, 4.4787e-41],
#          [2.8245e+23, 0.0000e+00, 0.0000e+00, 7.2148e+22]],
#         [[4.4787e-41, 0.0000e+00, 0.0000e+00, 7.7052e+31],
#          [4.4787e-41, 0.0000e+00, 4.4787e-41, 1.5766e-19]],
#         [[2.8245e+23, 0.0000e+00, 0.0000e+00, 7.2148e+22],
#          [0.0000e+00, 0.0000e+00, 7.7052e+31, 1.0256e-08]]])

torch.empty_strided((3, 2, 4), (1, 2, 3), dtype=torch.int64)
# tensor([[[96270211431424, 0, 0, 1],
#          [136257312186800, 0, 0, 0]],
#         [[96270118417696, 0, 0, 0],
#          [0, 0, 1, 0]],
#         [[136257312186800, 0, 0, 0],
#          [0, 0, 0, 0]]])

torch.empty_strided((3, 2, 4), (1, 2, 3), dtype=torch.complex64)
# tensor([[[4.4842e-43+0.0000e+00j, -8.1620e-18+3.1409e-41j,
#           1.5695e-43+0.0000e+00j, 0.0000e+00+1.7045e-05j],
#          [-8.1774e-18+3.1409e-41j, 0.0000e+00+0.0000e+00j,
#           -1.2345e+19+3.1404e-41j, 0.0000e+00+2.5589e-01j]],
#         [[6.8664e-44+0.0000e+00j, 1.4013e-45+0.0000e+00j,
#           1.1210e-43+0.0000e+00j, 0.0000e+00+1.7045e-05j],
#          [-8.1620e-18+3.1409e-41j, 1.5695e-43+0.0000e+00j,
#           0.0000e+00+1.7045e-05j, 0.0000e+00+1.5961e-05j]],
#         [[-8.1774e-18+3.1409e-41j, 0.0000e+00+0.0000e+00j,
#           -1.2345e+19+3.1404e-41j, 0.0000e+00+2.5589e-01j],
#          [1.4013e-45+0.0000e+00j, 1.1210e-43+0.0000e+00j,
#           0.0000e+00+1.7045e-05j,  0.0000e+00+1.7045e-05j]]])

torch.empty_strided((3, 2, 4), (1, 2, 3), dtype=torch.bool)
# tensor([[[False, True, False, True],
#          [True, True, True, True]],
#         [[True, True, False, True],
#          [True, False, True, True]],
#         [[True, True, True, True],
#          [True, False, True, True]]])
```