randn() and randn_like() in PyTorch
Tags: pytorch, randn, randnlike, function
Published: 2024-07-10 13:43:30

*Memos:
- You can use [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html) with [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/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains `manual_seed()`.
- [My post](https://dev.to/hyperkai/rand-and-randlike-in-pytorch-1p3k) explains [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/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).

[randn()](https://pytorch.org/docs/stable/generated/torch.randn.html) can create the 1D or more D tensor of the zero or more random floating-point numbers(Default) or complex numbers most of the time about between `2` and `-2` from standard normal distribution as shown below:

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

torch.randn(size=(3,))
torch.randn(3)
# tensor([-0.6635,  1.6257, -2.0568])

torch.randn(size=(3, 2))
torch.randn(3, 2)
# tensor([[-1.9382, 1.1300], [0.4138, 0.8232], [-0.9810, -0.1410]])

torch.randn(size=(3, 2, 4))
torch.randn(3, 2, 4)
# tensor([[[-0.1585, 0.6327, -0.2487, -0.0963],
#          [-0.2869,  2.4429,  0.5098, -1.1569]],
#         [[ 0.2823,  1.0742,  1.0531, -0.2384],
#          [-0.2275, -2.6246,  0.3347, -1.6514]],
#         [[ 0.2934, -1.2568,  0.1798,  1.1989],
#          [ 0.5166, -1.2155,  0.5101, -0.7396]]])

torch.randn(size=(3, 2, 4), dtype=torch.complex64)
torch.randn(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.5911+0.4230j, -0.1695-0.5487j,
#           -0.5253-1.3477j, 0.2331+1.9928j],
#          [1.1815+0.8113j, 0.1307+2.0229j,
#           0.5588+0.6502j, 0.8352-1.1519j]],
#         [[1.5789+0.5048j, -1.2516-1.0842j,
#           0.6233+0.7139j, 0.0393-0.4259j],
#          [0.1236+0.9666j, 0.2755+0.1572j,
#           -0.5766-0.0394j, -0.3715-0.2530j]],
#         [[0.6339-1.1397j, -0.1932-0.8051j,
#           1.3219-0.4141j, 0.0786+0.1172j],
#          [-0.9245+0.9382j, -1.0984+0.4884j,
#           -0.3895-0.0102j, -0.0171+0.9639j]]])
```

[randn_like()](https://pytorch.org/docs/stable/generated/torch.randn_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 most of the time about between `2` and `-2` from standard normal distribution as shown below:

*Memos:
- `randn_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.randn_like(input=my_tensor)
# tensor(-2.3177)

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

torch.randn_like(input=my_tensor)
# tensor([-0.4706, -0.0940, 1.7397])

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

torch.randn_like(input=my_tensor)
# tensor([[-0.4152, 0.8599, -2.4599],
#         [-1.1088, -0.4828, 2.3003]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.randn_like(input=my_tensor)
# tensor([[[-1.0316, 1.5852, -1.1012], [-2.3114, 0.5584, 2.3333]],
#         [[-0.5730, -0.3688, 0.0671], [1.2493, -0.0072, 0.1905]]])

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.randn_like(input=my_tensor)
# tensor([[[-0.3193-0.2821j, -0.8942+0.7886j, 0.2968-0.0608j],
#          [-0.5996+0.8685j, 0.7314-0.5378j, 1.7339-0.8352j]],
#         [[-0.0787-0.1357j, -1.4519-0.0295j, 0.1233-0.1794j],
#          [ 0.4109-0.2422j, 0.3973+0.3363j, -1.0303-1.4719j]]])
```