DEV Community

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

Posted on • Updated on

full() and full_like() in PyTorch

*My post explains device().

full() can create a 1D or more D tensor filled with zero or more elements as shown below:

*Memos:

  • full() can be used with torch but not with a tensor.
  • The 1st argument(tuple of int, list of int or torch.Size) is size(Required).
  • The 2nd argument(int, float, complex or bool) is fill_value(Required).
  • There is dtype argument(torch.dtype) (Optional) with torch. *Memos:
  • There is device argument(int, str or torch.device) (Optional) with torch. *Memos:
    • If device is not given, the device of set_default_device() is used.
    • cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, ort, xla, lazy, vulkan, mps, meta, hpu, mtia or privateuseone can be set to device.
    • Setting 0 uses GPU(CUDA).
    • device= must be used.
import torch

torch.full(size=(3,), fill_value=5)
torch.full(size=torch.tensor([0, 1, 2]).size(), fill_value=5)
# tensor([5, 5, 5])

torch.full(size=(3, 2), fill_value=5)
# tensor([[5, 5], [5, 5], [5, 5]])

torch.full(size=(3, 2, 4), fill_value=5,
           dtype=torch.int64, device='cuda:0')
torch.full(size=(3, 2, 4), fill_value=5,
           dtype=torch.int64, device='cuda')
torch.full(size=(3, 2, 4), fill_value=5,
           dtype=torch.int64, device=0)
torch.full(size=(3, 2, 4), fill_value=5,
           dtype=int, device=torch.device('cuda:0'))
torch.full(size=(3, 2, 4), fill_value=5,
           dtype=int, device=torch.device(type='cuda'))
torch.full(size=(3, 2, 4), fill_value=5,
           dtype=int, device=torch.device(type='cuda', index=0))
# tensor([[[5, 5, 5, 5], [5, 5, 5, 5]],
#         [[5, 5, 5, 5], [5, 5, 5, 5]],
#         [[5, 5, 5, 5], [5, 5, 5, 5]]])

torch.full(size=(3, 2, 4), fill_value=5., dtype=torch.float64)
torch.full(size=(3, 2, 4), fill_value=5., dtype=float)
# tensor([[[5., 5., 5., 5.], [5., 5., 5., 5.]],
#         [[5., 5., 5., 5.], [5., 5., 5., 5.]],
#         [[5., 5., 5., 5.], [5., 5., 5., 5.]]], dtype=torch.float64)

torch.full(size=(3, 2, 4), fill_value=5.+6.j, dtype=torch.complex64)
# tensor([[[5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j],
#          [5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j]],
#         [[5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j],
#          [5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j]],
#         [[5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j],
#          [5.+6.j, 5.+6.j, 5.+6.j, 5.+6.j]]])

torch.full(size=(3, 2, 4), fill_value=True, dtype=torch.bool)
torch.full(size=(3, 2, 4), fill_value=True, dtype=bool)
# tensor([[[True, True, True, True],
#          [True, True, True, True]],
#         [[True, True, True, True],
#          [True, True, True, True]],
#         [[True, True, True, True],
#          [True, True, True, True]]])

torch.full(size=(0,), fill_value=5)
torch.full(size=(0,), fill_value=5, device='cpu')
torch.full(size=(0,), fill_value=5, device=torch.device(device='cpu'))
torch.full(size=(0,), fill_value=5, device=torch.device(type='cpu'))
# tensor([], dtype=torch.int64)

torch.full(size=(0,), fill_value=5, device='cuda:0')
torch.full(size=(0,), fill_value=5, device='cuda')
torch.full(size=(0,), fill_value=5, device=0)
torch.full(size=(0,), fill_value=5, device=torch.device(device='cuda:0'))
torch.full(size=(0,), fill_value=5, device=torch.device(type='cuda'))
torch.full(size=(0,), fill_value=5, device=torch.device(type='cuda', index=0))
# tensor([], dtype=torch.int64)
Enter fullscreen mode Exit fullscreen mode

full_like() can replace the zero or more elements of a 0D or more D tensor with zero or more elements as shown below:

*Memos:

  • full_like() can be used with torch but not with a tensor.
  • The 2nd argument(int, float, complex or bool) is fill_value(Required).
  • There is dtype argument(torch.dtype) (Optional-Default:None) with torch. *Memos:
  • There is device argument(int, str or torch.device) (Optional) with torch. *Memos:
    • If device is not given, the device of set_default_device() is used.
    • cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, ort, xla, lazy, vulkan, mps, meta, hpu, mtia or privateuseone can be set to device.
    • Setting 0 uses GPU(CUDA).
    • device= must be used.
import torch

my_tensor = torch.tensor(7)

torch.full_like(input=my_tensor, fill_value=5)
torch.full_like(input=my_tensor, fill_value=5, 
                device=torch.device(device='cpu'))
torch.full_like(input=my_tensor, fill_value=5, 
                device=torch.device(type='cpu'))
# tensor(5)

my_tensor = torch.tensor([7, 4, 5])

torch.full_like(input=my_tensor, fill_value=5)
# tensor([5, 5, 5])

my_tensor = torch.tensor([[7, 4, 5],
                          [2, 8, 3]])
torch.full_like(input=my_tensor, fill_value=5)
# tensor([[5, 5, 5], [5, 5, 5]])

my_tensor = torch.tensor([[[7, 4, 5], [2, 8, 3]],
                          [[6, 0, 1], [5, 9, 4]]])
torch.full_like(input=my_tensor, fill_value=5,
                dtype=torch.int64, device='cuda:0')
torch.full_like(input=my_tensor, fill_value=5,
                dtype=torch.int64, device='cuda')
torch.full_like(input=my_tensor, fill_value=5,
                dtype=torch.int64, device=0)
torch.full_like(input=my_tensor, fill_value=5,
                dtype=int, device=torch.device('cuda:0'))
torch.full_like(input=my_tensor, fill_value=5,
                dtype=int, device=torch.device(type='cuda'))
torch.full_like(input=my_tensor, fill_value=5,
                dtype=int, device=torch.device(type='cuda', index=0))
# tensor([[[5, 5, 5], [5, 5, 5]],
#         [[5, 5, 5], [5, 5, 5]]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.full_like(input=my_tensor, fill_value=5., dtype=torch.float64)
torch.full_like(input=my_tensor, fill_value=5., dtype=float)
# tensor([[[5., 5., 5.], [5., 5., 5.]],
#         [[5., 5., 5.], [5., 5., 5.]]])

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.full_like(input=my_tensor, fill_value=5.+3.j, 
                dtype=torch.complex64)
# tensor([[[5.+3.j, 5.+3.j, 5.+3.j],
#          [5.+3.j, 5.+3.j, 5.+3.j]],
#         [[5.+3.j, 5.+3.j, 5.+3.j],
#          [5.+3.j, 5.+3.j, 5.+3.j]]])

my_tensor = torch.tensor([[[True, False, True],
                           [False, True, False]], 
                          [[True, False, True],
                           [False, True, False]]])
torch.full_like(input=my_tensor, fill_value=False, dtype=torch.bool)
torch.full_like(input=my_tensor, fill_value=False, dtype=bool)
# tensor([[[False, False, False],
#          [False, False, False]],
#         [[False, False, False],
#          [False, False, False]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)