DEV Community

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

Posted on • Edited on

empty and empty_like in PyTorch

Buy Me a Coffee

*My post explains empty_strided().

empty() can create the 0D or more D tensor of the zero or more floating-point numbers(Default), integers, complex numbers or boolean values from uninitialized memory which are called uninitialized data as shown below:

*Memos:

  • empty() can be used with torch 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()).
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • 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.
  • Uninitialized memory has data but the data is unknown.
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]]])
Enter fullscreen mode Exit fullscreen mode

empty_like() 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 uninitialized 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-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
    • If it's None, it's inferred from input.
    • device= must be used.
    • My post explains device argument.
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • Uninitialized memory has data but the data is unknown.
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]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)