full() and full_like() in PyTorch
Tags: pytorch, full, fulllike, function
Published: 2024-04-24 15:08:39

[full()](https://pytorch.org/docs/stable/generated/torch.full.html) can create the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `full()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st argument is `size`(Required-Type:`tuple` of `int`, `list` of `int` or [size()](https://pytorch.org/docs/stable/generated/torch.Tensor.size.html)).
- The 2nd argument with `torch` is `fill_value`(Required-Type:`int`, `float`, `complex` or `bool`). *The 0D tensor of an element(`int`, `float`, `complex` or `bool`) also works.
- 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, `dtype` is inferred from `fill_value` or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
  - `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:
  - `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.

```python
import torch

torch.full(size=(), fill_value=5)
torch.full(size=(), fill_value=torch.tensor(5))
torch.full(size=torch.tensor(8).size(), fill_value=5)
# tensor(5)

torch.full(size=(3,), fill_value=5)
torch.full(size=(3,), fill_value=torch.tensor(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)
# 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.)
# 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.+6.j)
# 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)
# 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]]])
```

[full_like()](https://pytorch.org/docs/stable/generated/torch.full_like.html) can get the 0D or more D tensor of the zero or more elements replaced with zero or more elements as shown below:

*Memos:
- `full_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`).
- The 2nd argument with `torch` is `fill_value`(Required-Type:`int`, `float`, `complex` or `bool`). *The 0D tensor of an element(`int`, `float`, `complex` or `bool`) also works.
- 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, `dtype` is inferred from `input` tensor.
  - `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, `device` of `input` tensor is used.
  - `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.

```python
import torch

my_tensor = torch.tensor(7)

torch.full_like(input=my_tensor, fill_value=5)
torch.full_like(input=my_tensor, fill_value=torch.tensor(5))
# 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)
# 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.)
# 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)
# 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=True)
# tensor([[[True, True, True],
#         [True, True, True]],
#        [[True, True, True],
#         [True, True, True]]])
```