rand() and rand_like() in PyTorch
Tags: pytorch, rand, randlike, function
Published: 2024-07-10 13:40:24

*Memos:
- You can use [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html) with [rand()](https://pytorch.org/docs/stable/generated/torch.rand.html) and [rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html). *[My post](https://dev.to/hyperkai/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains `manual_seed()`.
- [My post](https://dev.to/hyperkai/randn-and-randnlike-in-pytorch-8bb) explains [randn()](https://pytorch.org/docs/stable/generated/torch.randn.html) and [randn_like()](https://pytorch.org/docs/stable/generated/torch.randn_like.html).
- [My post](https://dev.to/hyperkai/randint-and-randperm-in-pytorch-8gm) explains [randint()](https://pytorch.org/docs/stable/generated/torch.randint.html) and [randperm()](https://pytorch.org/docs/stable/generated/torch.randperm.html).
- [My post](https://dev.to/hyperkai/normal-in-pytorch-2jb) explains [normal()](https://pytorch.org/docs/stable/generated/torch.normal.html).

[rand()](https://pytorch.org/docs/stable/generated/torch.rand.html) can create the 1D or more D tensor of the zero or more random floating-point numbers(Default) or complex numbers between `0` and `1`(0<=x<1) as shown below:

*Memos:
- `rand()` 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).
  - `float` or `complex` can be used.
  - `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.rand(size=(0,))
torch.rand(0)
# tensor([])

torch.rand(size=(3,))
torch.rand(3)
# tensor([0.9544, 0.8922, 0.3293])

torch.rand(size=(3, 2))
torch.rand(3, 2)
# tensor([[0.4453, 0.1646], [0.0770, 0.6157], [0.3791, 0.9814]])

torch.rand(size=(3, 2, 4))
torch.rand(3, 2, 4)
# tensor([[[0.6002, 0.9127, 0.1574, 0.0520],
#          [0.6030, 0.8130, 0.8485, 0.0871]],
#         [[0.7439, 0.3891, 0.0515, 0.9340],
#          [0.8608, 0.5153, 0.5754, 0.7751]],
#         [[0.9068, 0.6578, 0.6610, 0.5676],
#          [0.3567, 0.2954, 0.5270, 0.8195]]])

torch.rand(size=(3, 2, 4), dtype=torch.complex64)
torch.rand(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.5424+0.6970j, 0.7606+0.3126j,
#           0.9649+0.6356j, 0.3805+0.3811j],
#          [0.7923+0.3020j, 0.9168+0.1565j,
#           0.9722+0.7607j, 0.2757+0.8831j]],
#         [[0.7804+0.0009j, 0.4435+0.9249j, 
#           0.2160+0.5390j, 0.9973+0.4470j],
#          [0.2328+0.5351j, 0.0051+0.3250j,
#           0.5077+0.4365j, 0.4540+0.2164j]],
#         [[0.7434+0.1432j, 0.0507+0.7610j,
#           0.0449+0.6918j, 0.8486+0.5216j],
#          [0.2609+0.1117j, 0.3226+0.3598j,
#           0.9357+0.8471j, 0.1643+0.6220j]]])
```

[rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html) can replace the zero or more floating-point numbers or complex numbers of a 0D or more D tensor with the zero or more random floating-point numbers or complex numbers between `0` and `1`(0<=x<1) as shown below:

*Memos:
- `rand_like()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `input`(Required-Type:`tensor` of `float` or `complex`).
- 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`.
  - `float` or `complex` can be used.
  - `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.rand_like(input=my_tensor)
# tensor(0.3634)

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

torch.rand_like(input=my_tensor)
# tensor([0.9000, 0.1314, 0.5240])

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

torch.rand_like(input=my_tensor)
# tensor([[0.7508, 0.5857, 0.1658],
#         [0.3327, 0.1836, 0.3776]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]], 
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.rand_like(input=my_tensor)
# tensor([[[0.8467, 0.3134, 0.9486], [0.3134, 0.1809, 0.1740]],
#         [[0.2624, 0.3929, 0.2138], [0.2034, 0.9147, 0.3421]]])

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.rand_like(input=my_tensor)
# tensor([[[0.9179+0.7615j, 0.6811+0.3994j, 0.6854+0.3131j],
#          [0.7352+0.0108j, 0.0999+0.4509j, 0.9586+0.1530j]],
#         [[0.5253+0.7685j, 0.5731+0.0358j, 0.9499+0.8943j],
#          [0.7742+0.3421j, 0.0419+0.8636j, 0.1014+0.8507j]]])
```