ones() and ones_like() in PyTorch
Tags: pytorch, ones, oneslike, function
Published: 2024-07-09 14:52:29

*[My post](https://dev.to/hyperkai/zeros-and-zeroslike-in-pytorch-2kml) explains [zeros()](https://pytorch.org/docs/stable/generated/torch.zeros.html) and [zeros_like()](https://pytorch.org/docs/stable/generated/torch.zeros_like.html).

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

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

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

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

torch.ones(size=(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(size=(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(size=(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(size=(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 a 0D or more D tensor with zero or more `1.`, `1`, `1.+0.j` or `True` as shown below:

*Memos:
- `ones_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.ones_like(input=my_tensor)
# tensor(1.)

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

torch.ones_like(input=my_tensor)
# tensor([1., 1., 1.])

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

torch.ones_like(input=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(input=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(input=my_tensor)
# tensor([[[1, 1, 1], [1, 1, 1]],
#         [[1, 1, 1], [1, 1, 1]]])

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