empty(), empty_like() and empty_strided in PyTorch

Rmag Breaking News

empty() can create a 1D or more D tensor filled with the zero or more numbers in memory which is called uninitialized data.

*Memos:

You can use torch.Tensor() or torch.FloatTensor() 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.

empty() can be used only from torch but not from a tensor

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

empty_like() can replace the zero or more numbers of 0D or more D tensor with zero or more numbers in memory which is called uninitialized data as shown below. *empty_like() can be used only from torch but not from a tensor:

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([0.0000e+00, 7.0065e-45, 7.7052e+31])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
[[4., 1., 1.], [9., 6., 2.]]])
torch.empty_like(my_tensor)
# tensor([[[1.2041e+30, 3.1107e-41, 0.0000e+00],
# [0.0000e+00, 1.4013e-45, 0.0000e+00]],
# [[6.1794e-24, 3.1100e-41, -3.6752e-05],
# [4.4916e-41, 6.1794e-24, 3.1100e-41]]])

my_tensor = torch.tensor(7)
torch.empty_like(my_tensor) # tensor(23272202853)

my_tensor = torch.tensor([7, 4, 5])
torch.empty_like(my_tensor)
# tensor([137670164876784, 95322899633680, 0])

my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
[[4, 1, 1], [9, 6, 2]]])
torch.empty_like(my_tensor)
# tensor([[[95322943260832, 0, 95322943854288],
# [95322857908688, 137664578738736, 1]],
# [[12, 0, 0],
# [0, 0, 1]]])

empty_strided() can create the 1D or more D tensor which has stride and is filled with the zero or more numbers in memory which is called uninitialized data as shown below:

*Memos:

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.

empty_strided() can be used only from torch but not from a tensor.

import torch

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

torch.empty_strided((3,), (1,))
# tensor([4.9338e-22, 4.5326e-41, -6.5039e+36])

torch.empty_strided((3, 2), (1, 2))
# tensor([[-5.4307e+36, -7.7633e+36],
# [ 3.1081e-41, 3.1081e-41],
# [-7.7633e+36, 8.9683e-44]])

torch.empty_strided((3, 2, 4), (1, 2, 3))
# tensor([[[-1.5065e-25, 2.3510e-38, 2.0000e+00, 3.1081e-41],
[0.0000e+00, 3.1081e-41, 5.2373e+36, 3.1081e-41]],
[[3.1077e-41, 1.5246e+31, 0.0000e+00, 4.4369e+36],
[2.3510e-38, 2.0000e+00, 3.1081e-41, 4.8201e+36]],
[[0.0000e+00, 3.1081e-41, 5.2373e+36, 3.1081e-41],
[1.5246e+31, 0.0000e+00, 4.4369e+36, 3.1081e-41]]])

Leave a Reply

Your email address will not be published. Required fields are marked *