empty() and empty_like() in PyTorch
Tags: pytorch, empty, emptylike, function
Published: 2024-07-17 12:02:33

[Buy Me a Coffee](ko-fi.com/superkai)☕

*[My post](https://dev.to/hyperkai/emptystrided-in-pytorch-36f7) explains [empty_strided()](https://pytorch.org/docs/stable/generated/torch.empty_strided.html).

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

*Memos:
- `empty()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st or more arguments with `torch` are `size`(Required-Type:`int`, `tuple` of `int`, `list` of `int` or [size()](https://pytorch.org/docs/stable/generated/torch.Tensor.size.html)).
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, [get_default_dtype()](https://pytorch.org/docs/stable/generated/torch.get_default_dtype.html) is used. *[My post](https://dev.to/hyperkai/setdefaultdtype-setdefaultdevice-and-setprintoptions-in-pytorch-55g8) explains `get_default_dtype()` and [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_tensor_type.html).
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - If `device` is not given, [get_default_device()](https://pytorch.org/docs/stable/generated/torch.get_default_device.html) is used. *[My post](https://dev.to/hyperkai/setdefaultdtype-setdefaultdevice-and-setprintoptions-in-pytorch-55g8) explains `get_default_device()` and [set_default_device()](https://pytorch.org/docs/stable/generated/torch.set_default_device.html).
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- 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(size=())
torch.empty(size=torch.tensor(8).size())
# tensor(3.6404e-27)

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

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

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

torch.empty(size=(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(size=(3, 2, 4), dtype=torch.int64)
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(size=(3, 2, 4), dtype=torch.complex64)
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(size=(3, 2, 4), dtype=torch.bool)
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 a 0D or more D tensor with the zero or more floating-point numbers, integers, complex numbers or boolean values from memory which are called **uninitialized data** as shown below:

*Memos:
- `empty_like()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, it is inferred from `input`.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - If `device` is not given, it is inferred from `input`.
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.

```python
import torch

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

my_tensor = torch.tensor([7., 4., 5.])
torch.empty_like(input=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(input=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(input=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(input=my_tensor)
# tensor([[[137273168313840, 95694909291296, 1],
#          [95694912519088, 95694842532640, 0]],
#         [[95694862074384, 95694820258896, 137269160918960],
#          [0, 0, 0]]])

my_tensor = torch.tensor([[[7.+4.j, 4.+2.j, 5.+3.j],
                           [2.+5.j, 8.+1.j, 3.+9.j]],
                          [[6.+9.j, 0.+3.j, 1.+8.j],
                           [5.+3.j, 9.+4.j, 4.+6.j]]])
torch.empty_like(input=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(input=my_tensor)
# tensor([[[True, True, True],
#          [True, True, False]],
#         [[False, False, True],
#          [True, True, True]]])
```