zeros() and zeros_like() in PyTorch
Tags: pytorch, zeros, zeroslike, function
Published: 2024-07-09 14:49:35

*[My post](https://dev.to/hyperkai/ones-and-oneslike-in-pytorch-930) explains [ones()](https://pytorch.org/docs/stable/generated/torch.ones.html) and [ones_like()](https://pytorch.org/docs/stable/generated/torch.ones_like.html).

[zeros()](https://pytorch.org/docs/stable/generated/torch.zeros.html) can create the 1D or more D tensor of zero or more `0.`, `0`, `0.+0.j` or `False` as shown below:

*Memos:
- `zeros()` 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` or `list` of `int`).
- 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` of [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:
  - `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.zeros(size=(0,))
torch.zeros(0)
# tensor([])

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

torch.zeros(size=(3, 2))
torch.zeros(3, 2)
# tensor([[0., 0.], [0., 0.], [0., 0.]])

torch.zeros(size=(3, 2, 4))
torch.zeros(3, 2, 4)
# tensor([[[0., 0., 0., 0.], [0., 0., 0., 0.]],
#         [[0., 0., 0., 0.], [0., 0., 0., 0.]],
#         [[0., 0., 0., 0.], [0., 0., 0., 0.]]])

torch.zeros(size=(3, 2, 4), dtype=torch.int64)
torch.zeros(3, 2, 4, dtype=torch.int64)
# tensor([[[0, 0, 0, 0], [0, 0, 0, 0]],
#         [[0, 0, 0, 0], [0, 0, 0, 0]],
#         [[0, 0, 0, 0], [0, 0, 0, 0]]])

torch.zeros(size=(3, 2, 4), dtype=torch.complex64)
torch.zeros(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
#         [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
#         [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#          [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]]])

torch.zeros(size=(3, 2, 4), dtype=torch.bool)
torch.zeros(3, 2, 4, dtype=torch.bool)
# tensor([[[False, False, False, False],
#          [False, False, False, False]],
#         [[False, False, False, False],
#          [False, False, False, False]],
#         [[False, False, False, False],
#          [False, False, False, False]]])
```

[zeros_like()](https://pytorch.org/docs/stable/generated/torch.zeros_like.html) can replace the zero or more floating-point numbers, integers, complex numbers or boolean values of a 0D or more D tensor with zero or more `0.`, `0`, `0.+0.j` or `False` as shown below:

*Memos:
- `zeros_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, `dtype` 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:
  - `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.zeros_like(input=my_tensor)
# tensor(0.)

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

torch.zeros_like(input=my_tensor)
# tensor([0., 0., 0.])

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

torch.zeros_like(input=my_tensor)
# tensor([[0., 0., 0.], [0., 0., 0.]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.zeros_like(input=my_tensor)
# tensor([[[0., 0., 0.], [0., 0., 0.]],
#         [[0., 0., 0.], [0., 0., 0.]]])

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

my_tensor = torch.tensor([[[True, False, True], [False, True, False]], 
                          [[False, True, False], [True, False, True]]])
torch.zeros_like(input=my_tensor)
# tensor([[[False, False, False], [False, False, False]],
#         [[False, False, False], [False, False, False]]])
```