zeros(), zeros_like(), ones() and ones_like() in PyTorch
Tags: pytorch, zeros, ones, zeroslike
Published: 2024-04-09 23:13:25

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

*Memos:
- `zeros()` can be used only from [torch](https://pytorch.org/docs/stable/torch.html) but not from a tensor.
- The default type is `float32`.

```python
import torch

torch.zeros(0)
torch.zeros((0,))
# tensor([])

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

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

torch.zeros(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(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(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(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 0D or more D tensor with zero or more `0.`, `0`, `0.+0.j` or `False` as shown below. *`zeros_like()` can be used only from `torch` but not from a tensor:

```python
import torch

my_tensor = torch.tensor(7.)
torch.zeros_like(my_tensor)
# tensor(0.)

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

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

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

[ones()](https://pytorch.org/docs/stable/generated/torch.ones.html) can create a 1D or more D tensor filled with zero or more `1.`, `1`, `1.+0.j` or `True` as shown below:

*Memos:
- `ones()` can be used only from `torch` but not from a tensor.
- The default type is `float32`.

```python
import torch

torch.ones(0)
torch.ones((0,))
# tensor([])

torch.ones(3)
torch.ones((3,))
# tensor([1., 1., 1.])

torch.ones(3, 2)
torch.ones((3, 2))
# tensor([[1., 1.], [1., 1.],　[1., 1.]])

torch.ones(3, 2, 4)
torch.ones((3, 2, 4))
# tensor([[[1., 1., 1., 1.], [1., 1., 1., 1.]],
#         [[1., 1., 1., 1.], [1., 1., 1., 1.]],
#         [[1., 1., 1., 1.], [1., 1., 1., 1.]]])

torch.ones(3, 2, 4, dtype=torch.int64)
torch.ones((3, 2, 4), dtype=torch.int64)
# tensor([[[1, 1, 1, 1], [1, 1, 1, 1]],
#         [[1, 1, 1, 1], [1, 1, 1, 1]],
#         [[1, 1, 1, 1], [1, 1, 1, 1]]])

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

torch.ones(3, 2, 4, dtype=torch.bool)
torch.ones((3, 2, 4), dtype=torch.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]]])
```

[ones_like()](https://pytorch.org/docs/stable/generated/torch.ones_like.html) can replace the zero or more integers, floating-point numbers, integers, complex numbers or boolean values of 0D or more D tensor with zero or more `1.`, `1`, `1.+0.j` or `True` as shown below. *`ones_like()` can be used only from `torch` but not from a tensor:

```python
import torch

my_tensor = torch.tensor(7.)
torch.ones_like(my_tensor)
# tensor(1.)

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

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

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

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

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

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